true,
]
);
remove_theme_support( 'core-block-patterns' );
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\on_after_setup_theme' );
/**
* Register widgetized area
*/
function widget_areas_init() {
register_sidebar(
[
'name' => __( 'Primary Sidebar', 'p2020' ),
'id' => 'sidebar-1',
'before_widget' => '',
'before_title' => '
',
]
);
}
add_action( 'widgets_init', __NAMESPACE__ . '\widget_areas_init' );
/**
* Add recommended widgets to sidebar
*/
function enable_default_widgets() {
$widget_no = 3;
$setup_option = get_option( 'p2020_sidebar_setup' );
if ( empty( $setup_option ) ) {
return;
}
if ( 'reset' === $setup_option ) {
$site_widgets = [
'sidebar-1' => [],
];
} else {
$site_widgets = get_option( 'sidebars_widgets' );
$site_widgets['sidebar-1'] = $site_widgets['sidebar-1'] ?? [];
}
// Remove widgets considered redundant in p2020 (o2-filter, pages and search)
$redundant_widgets = [
'search',
'jetpack-search-filters',
'o2-filter-widget',
'pages',
];
$redundant_widgets_regex = '/(?:' . implode( '|', $redundant_widgets ) . ')-[0-9]+/';
$site_widgets['sidebar-1'] = array_filter(
$site_widgets['sidebar-1'],
function ( $widget ) use ( $redundant_widgets_regex ) {
return ! preg_match( $redundant_widgets_regex, $widget );
}
);
// My Team widget (widgets/myteam)
$widget_instance = "p2020-my-team-widget-{$widget_no}";
if ( empty( $site_widgets['sidebar-1'] ) || ! in_array( $widget_instance, $site_widgets['sidebar-1'], true ) ) {
$team_widget_settings = [
$widget_no => [
'title' => __( 'Team', 'p2020' ),
'limit' => 14,
],
];
update_option( 'widget_p2020-my-team-widget', $team_widget_settings );
// Add to head of sidebar-1 widgets
array_unshift( $site_widgets['sidebar-1'], "p2020-my-team-widget-{$widget_no}" );
}
// Save sidebar updates
wp_set_sidebars_widgets( $site_widgets );
// We need to refresh the global var $sidebars_widgets, so that our
// newly added widgets don't get classified as "lost widgets" --
// registered, but not shown.
wp_get_sidebars_widgets();
// Clear sidebar setup flag afterwards
delete_option( 'p2020_sidebar_setup' );
}
add_action( 'widgets_init', __NAMESPACE__ . '\enable_default_widgets' );
/**
* Custom o2 options for p2020
*/
function custom_o2_options( $o2_options ) {
if ( is_search() || is_tag() || isset( $_GET['resolved'] ) ) {
$o2_options['options']['showFrontSidePostBox'] = false;
}
// Set y-offset for j/k keyboard shortcut.
$o2_options['options']['jumpToItemYOffset'] = 120;
return $o2_options;
}
add_filter( 'o2_options', __NAMESPACE__ . '\custom_o2_options' );
/**
* Custom UI strings in O2
*/
function custom_o2_strings( array $o2_options ): array {
$o2_options['strings']['noPosts'] = __( 'Ready to publish your first post? Simply use the editor above.', 'p2020' );
$o2_options['strings']['noPostsMobile'] = __( 'Tap the + button in the top right corner to begin writing your first post.', 'p2020' );
return $o2_options;
}
add_filter( 'o2_options', __NAMESPACE__ . '\custom_o2_strings' );
/**
* P2tenberg setup
*/
function config_p2editor_comment_editor( array $settings ): array {
$settings['alignWide'] = false;
return $settings;
}
add_filter( 'p2editor_comment_editor', __NAMESPACE__ . '\config_p2editor_comment_editor' );
/**
* Append Contributor block to content on single pages
*/
function append_contributors_block( $content ) {
global $post;
$is_page = is_page() || ( $post && 'page' === $post->post_type );
if ( ! $is_page || ! is_main_query() || is_tag() ) {
return $content;
}
if ( ! empty( get_option( 'p2020_hide_page_contributors' ) ) ) {
return $content;
}
require_once get_template_directory() . '/inc/contributors.php';
return $content . get_contributors_block();
}
add_filter( 'the_content', __NAMESPACE__ . '\append_contributors_block' );
// Show the author of the parent comment on comments.
add_filter( 'o2_show_parent_comment_link', '__return_true' );
/**
* Custom CSS for Customizer > Widgets (admin)
*/
function customizer_widgets_styles( $hook ) {
if ( 'widgets.php' !== $hook ) {
return;
}
wp_enqueue_style( 'p2020-customizer-widgets', get_template_directory_uri() . '/widgets/customizer.css', [], '20220224' );
wp_style_add_data( 'p2020-customizer-widgets', 'rtl', 'replace' );
}
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\customizer_widgets_styles' );
/**
* Filter: enqueue scripts, hook actions and filters.
*/
function p2020_filter_init() {
Filter\enqueue_scripts();
Filter\add_hooks();
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\p2020_filter_init' );
/**
* Specify P2-specific p2tenberg settings.
*/
function p2editor_settings( $settings ) {
$disallow_blocks = [
/**
* Opinionated list of blocks that should be disallowed in _P2020 specifically_
*
* Before adding to this ignorelist, consider the following:
* - Core blocks that don't make sense for P2s in general should be disallowed in P2tenberg itself.
* - Non-Core Blocks (e.g. Jetpack, CoBlocks) that don't work with P2tenberg, or blocks that don't work
* for wpcom-specific reasons should be disallowed in p2tenberg-wpcom.
*/
'core/social-links',
'jetpack/layout-grid', /* TODO: Fix CSS issues */
'jetpack/layout-grid-column', /* TODO: Fix CSS issues */
];
// Append disallowed blocks for P2020
$settings['post']['disallowBlocks'] =
array_merge( $settings['post']['disallowBlocks'], $disallow_blocks );
$settings['comment']['disallowBlocks'] =
array_merge( $settings['comment']['disallowBlocks'], $disallow_blocks );
return $settings;
}
// Priority is set lower than default so this would run after p2tenberg defaults
add_filter( 'p2editor_settings', __NAMESPACE__ . '\p2editor_settings', 11 );
// Deregister To-Do CSS
function deregister_todo_style() {
wp_dequeue_style( 'o2-extend-to-do' );
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\deregister_todo_style', 11 );
function p2020_o2_post_fragment( $fragment, $post_id ) {
if ( is_page() ) {
require_once get_template_directory() . '/inc/page-breadcrumbs.php';
$fragment['breadcrumbs'] = get_page_breadcrumbs( $post_id );
}
if ( $fragment['isUnread'] ) {
$fragment['cssClasses'] .= ' p2020-unread-post';
}
return $fragment;
}
add_filter( 'o2_post_fragment', __NAMESPACE__ . '\p2020_o2_post_fragment', 10, 2 );
function common_cocktail_scripts() {
wp_enqueue_script(
'p2020-view-post',
get_template_directory_uri() . '/js/cocktails/view-p2020-post.js',
[ 'o2-cocktail', 'jquery' ],
filemtime( __FILE__ ),
true
);
wp_enqueue_script(
'p2020-view-posts-list',
get_template_directory_uri() . '/js/cocktails/view-p2020-posts-list.js',
[ 'o2-cocktail', 'jquery' ],
filemtime( __FILE__ ),
true
);
if ( is_single() ) {
wp_enqueue_script(
'p2020-single-live-comments',
get_template_directory_uri() . '/js/cocktails/live-comments.js',
[ 'o2-cocktail', 'wp-i18n' ],
filemtime( __FILE__ ),
true
);
wp_set_script_translations( 'p2020-single-live-comments', 'p2020', get_template_directory() . '/languages' );
}
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\common_cocktail_scripts', 11 );
/**
* Allow pages to use tags (i.e. post_tags taxonomy)
*/
function enable_tags_for_pages() {
register_taxonomy_for_object_type( 'post_tag', 'page' );
}
add_action( 'init', __NAMESPACE__ . '\enable_tags_for_pages' );
/**
* Modify the query for the tag archive page to make sure both posts and pages are returned.
*
* @param \WP_Query $wp_query
*/
function query_tags_for_pages( $wp_query ) {
if ( ! is_admin() && $wp_query->is_main_query() && is_tag() ) {
$wp_query->set( 'post_type', [ 'post', 'page' ] );
}
}
add_action( 'pre_get_posts', __NAMESPACE__ . '\query_tags_for_pages' );
/**
* Filter callback.
*
* @param mixed $value
*/
function filter_option_posts_per_page( $value ) {
if ( is_admin() || wp_doing_ajax() ) {
return $value;
}
if ( is_home() || is_archive() || is_search() ) {
$value = Compact_View::should_show_compact_view() ? 10 : 5;
}
return $value;
}
add_action( 'option_posts_per_page', __NAMESPACE__ . '\filter_option_posts_per_page' );
/**
* Action callback for admin_footer.
*
* Hide the "Blog pages show at most" option in the Reading settings.
* We are overriding that value so no reason to show it.
*/
function add_js_for_hide_posts_pr_page_setting() {
?>
__( 'Everyone in this P2', 'p2020' ),
'slug' => 'all',
'avatar_urls' => [
24 => $site_icon,
],
],
];
wp_localize_script( 'p2020-mentions', 'p2020AutocompleteMention', $data );
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\maybe_add_mention_all_script' );
/**
* Load the pages index.
*/
function add_pages_index() {
require_once get_template_directory() . '/inc/pages-index/class-custom-walker-page.php';
require_once get_template_directory() . '/inc/pages-index/class-pages-index.php';
\Automattic\P2\Themes\P2020\Pages_Index\Pages_Index::init();
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\add_pages_index' );
/**
* Load the drafts index.
*/
function add_drafts_index() {
require_once get_template_directory() . '/inc/drafts-index/class-drafts-index.php';
\Automattic\P2\Themes\P2020\Drafts_Index\Drafts_Index::init();
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\add_drafts_index' );
/**
* Allow P2 to preselect the parent page in the editor via
* URL param `parent_post`.
*/
function preselect_parent_page( $data ) {
// Bail early if param is not set.
if ( ! isset( $_GET['parent_post'] ) ) {
return $data;
}
// Bail early if not loading an empty page editor.
if ( 'page' !== $data['post_type'] ||
'auto-draft' !== $data['post_status'] ||
'' !== $data['post_content'] ||
'' !== $data['post_name']
) {
return $data;
}
$data['post_parent'] = wp_unslash( (int) $_GET['parent_post'] );
return $data;
}
add_filter( 'wp_insert_post_data', __NAMESPACE__ . '\preselect_parent_page' );
/**
* Filter callback for o2_post_fragment.
*
* @param array $fragment
* @param integer $post_id
*
* @return array
*/
function maybe_reduce_o2_fragment_size( $fragment, $post_id ) {
if ( Compact_View::should_show_compact_view() ) {
// Compact view is taking care of reducing the fragment size, no need to do it again.
return $fragment;
}
$not_needed = [];
$is_filtering = P2_Core\Filtering\View::get_active_filter();
// Pinned project threads.
if ( ! empty( $fragment['isProjectThread'] ) && ! empty( $fragment['isSticky'] ) && is_home() && ! $is_filtering ) {
$not_needed = [
'contentRaw',
'contentFiltered',
'postActions',
'commentDropdownActions',
'commentFooterActions',
'loginRedirectURL',
];
}
// x-posts.
if ( ! empty( $fragment['is_xpost'] ) ) {
$not_needed = [
'contentRaw',
'commentDropdownActions',
'commentFooterActions',
'loginRedirectURL',
];
}
foreach ( $not_needed as $key ) {
$fragment[ $key ] = '';
}
return $fragment;
}
// Set priority to 30 so this runs quite late.
add_filter( 'o2_post_fragment', __NAMESPACE__ . '\maybe_reduce_o2_fragment_size', 30, 2 );
/**
* Filter callback for o2_fragment_get_fragment_args.
*
* @param array $args
* @param integer $post_id
*
* @return array
*/
function pinned_project_threads_reduce_o2_processing( $args, $post_id ) {
if ( Compact_View::should_show_compact_view() ) {
// Compact view is taking care of reducing the fragment size, no need to do it again.
return $args;
}
$is_filtering = P2_Core\Filtering\View::get_active_filter();
if ( is_home() && ! $is_filtering && has_tag( 'project-thread', $post_id ) && is_sticky( $post_id ) ) {
$args['skip']['post']['contentFiltered'] = true;
$args['skip']['post']['mentions'] = true;
$args['skip']['post']['tags'] = true;
$args['skip']['post']['shortcodes'] = true;
$args['skip']['post']['comments'] = true;
}
return $args;
}
// Set priority to 30 so this runs quite late.
add_filter( 'o2_fragment_get_fragment_args', __NAMESPACE__ . '\pinned_project_threads_reduce_o2_processing', 30, 2 );
/**
* Modify the default category list, ensuring "Uncategorized" is removed.
*
* @param array $categories
*
* @return array
*/
function update_the_category_list( $categories ) {
foreach ( $categories as $key => $category ) {
if ( 'uncategorized' === $category->slug ) {
unset( $categories[ $key ] );
}
}
return $categories;
}
add_filter( 'the_category_list', __NAMESPACE__ . '\update_the_category_list' );
/**
* Ensure that if a post doesn't have any categories,
* the default is set to an empty string.
*
* @param string $categories
*
* @return string
*/
function update_default_category_empty_string( $categories ) {
return 'Uncategorized' === $categories ? '' : $categories;
}
add_filter( 'the_category', __NAMESPACE__ . '\update_default_category_empty_string' );
function add_custom_o2_templates( $templates ) {
$templates[] = 'collapsed-post-view';
return $templates;
}
add_filter( 'o2_custom_templates', __NAMESPACE__ . '\add_custom_o2_templates' );