Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/widget-v...
File: widget-conditions.php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
/**
[1] Fix | Delete
* Main class file for the Widget Visibility module.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
use Automattic\Block_Scanner;
[7] Fix | Delete
use Automattic\Jetpack\Assets;
[8] Fix | Delete
[9] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[10] Fix | Delete
exit( 0 );
[11] Fix | Delete
}
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Hide or show legacy widgets conditionally.
[15] Fix | Delete
*
[16] Fix | Delete
* This class has two responsiblities - administrating the conditions in which legacy widgets may be hidden or shown
[17] Fix | Delete
* and hiding/showing the legacy widgets on the front-end of the site, depending upon the evaluation of those conditions.
[18] Fix | Delete
*
[19] Fix | Delete
* Administrating the conditions can be done in one of four different WordPress screens, plus direct use of the API and
[20] Fix | Delete
* is supplemented with a legacy widget preview screen. The four different admin screens are
[21] Fix | Delete
*
[22] Fix | Delete
* Gutenberg widget experience - widget admin (widgets.php + API + legacy widget preview)
[23] Fix | Delete
* Gutenberg widget experience - Customizer (customizer screen/API + API + legacy widget preview)
[24] Fix | Delete
* Classic widget experience - widget admin (widgets.php + admin-ajax XHR requests)
[25] Fix | Delete
* Classic widget experience - Customizer (customizer screen/API)
[26] Fix | Delete
*
[27] Fix | Delete
* An introduction to the API endpoints can be found here: https://make.wordpress.org/core/2021/06/29/rest-api-changes-in-wordpress-5-8/
[28] Fix | Delete
*/
[29] Fix | Delete
class Jetpack_Widget_Conditions {
[30] Fix | Delete
/**
[31] Fix | Delete
* Stores condition for template_redirect action.
[32] Fix | Delete
*
[33] Fix | Delete
* @var bool
[34] Fix | Delete
*/
[35] Fix | Delete
public static $passed_template_redirect = false;
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Class initializer.
[39] Fix | Delete
*/
[40] Fix | Delete
public static function init() {
[41] Fix | Delete
global $pagenow;
[42] Fix | Delete
[43] Fix | Delete
// The Gutenberg based widget experience will show a preview of legacy widgets by including a URL beginning
[44] Fix | Delete
// widgets.php?legacy-widget-preview inside an iframe. Previews don't need widget editing loaded and also don't
[45] Fix | Delete
// want to run the filter - if the widget is filtered out it'll be empty, which would be confusing.
[46] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
[47] Fix | Delete
if ( isset( $_GET['legacy-widget-preview'] ) ) {
[48] Fix | Delete
return;
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
// If action is posted and it's save-widget then it's relevant to widget conditions, otherwise it's something
[52] Fix | Delete
// else and it's not worth registering hooks.
[53] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Missing
[54] Fix | Delete
if ( isset( $_POST['action'] ) && ! isset( $_POST['customize_changeset_uuid'] ) && ! in_array( $_POST['action'], array( 'save-widget', 'update-widget' ), true ) ) {
[55] Fix | Delete
return;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
// API call to *list* the widget types doesn't use editing visibility or display widgets.
[59] Fix | Delete
if ( isset( $_SERVER['REQUEST_URI'] ) && str_contains( $_SERVER['REQUEST_URI'], '/widget-types?' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
[60] Fix | Delete
return;
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
$add_data_assets_to_page = false;
[64] Fix | Delete
$add_html_to_form = false;
[65] Fix | Delete
$handle_widget_updates = false;
[66] Fix | Delete
$add_block_controls = false;
[67] Fix | Delete
[68] Fix | Delete
// Check to see if using the customizer, but not using the preview. The preview should filter out widgets,
[69] Fix | Delete
// the customizer controls in the sidebar should not (so they can be edited).
[70] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
[71] Fix | Delete
$customizer_not_previewer = is_customize_preview() && ! isset( $_GET['customize_changeset_uuid'] );
[72] Fix | Delete
$using_classic_experience = ! wp_use_widgets_block_editor();
[73] Fix | Delete
if ( $using_classic_experience &&
[74] Fix | Delete
( $customizer_not_previewer || 'widgets.php' === $pagenow ||
[75] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Missing
[76] Fix | Delete
( 'admin-ajax.php' === $pagenow && array_key_exists( 'action', $_POST ) && 'save-widget' === $_POST['action'] )
[77] Fix | Delete
)
[78] Fix | Delete
) {
[79] Fix | Delete
$add_data_assets_to_page = true;
[80] Fix | Delete
$add_html_to_form = true;
[81] Fix | Delete
$handle_widget_updates = true;
[82] Fix | Delete
} else {
[83] Fix | Delete
// On a screen that is hosting the API in the gutenberg editing experience.
[84] Fix | Delete
if ( $customizer_not_previewer || 'widgets.php' === $pagenow ) {
[85] Fix | Delete
$add_data_assets_to_page = true;
[86] Fix | Delete
$add_block_controls = true;
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
// Encoding for a particular widget end point.
[90] Fix | Delete
if ( isset( $_SERVER['REQUEST_URI'] ) && 1 === preg_match( '|/widget-types/.*/encode|', $_SERVER['REQUEST_URI'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
[91] Fix | Delete
$add_html_to_form = true;
[92] Fix | Delete
$handle_widget_updates = true;
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
// Batch API is usually saving but could be anything.
[96] Fix | Delete
$current_url = ! empty( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
[97] Fix | Delete
if ( str_contains( $current_url, '/wp-json/batch/v1' ) || 1 === preg_match( '/^\/wp\/v2\/sites\/\d+\/batch\/v1/', $current_url ) ) {
[98] Fix | Delete
$handle_widget_updates = true;
[99] Fix | Delete
$add_html_to_form = true;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
// Saving widgets via non-batch API. This isn't used within WordPress but could be used by third parties in theory.
[103] Fix | Delete
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] && str_contains( $_SERVER['REQUEST_URI'], '/wp/v2/widgets' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
[104] Fix | Delete
$handle_widget_updates = true;
[105] Fix | Delete
$add_html_to_form = true;
[106] Fix | Delete
}
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
if ( $add_html_to_form ) {
[110] Fix | Delete
add_action( 'in_widget_form', array( __CLASS__, 'widget_conditions_admin' ), 10, 3 );
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
if ( $handle_widget_updates ) {
[114] Fix | Delete
add_filter( 'widget_update_callback', array( __CLASS__, 'widget_update' ), 10, 3 );
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
if ( $add_data_assets_to_page ) {
[118] Fix | Delete
add_action( 'sidebar_admin_setup', array( __CLASS__, 'widget_admin_setup' ) );
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
if ( $add_block_controls ) {
[122] Fix | Delete
add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'setup_block_controls' ) );
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
if ( ! $add_html_to_form && ! $handle_widget_updates && ! $add_data_assets_to_page &&
[126] Fix | Delete
! in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ), true )
[127] Fix | Delete
) {
[128] Fix | Delete
// Not hit any known widget admin endpoint, register widget display hooks instead.
[129] Fix | Delete
add_filter( 'widget_display_callback', array( __CLASS__, 'filter_widget' ) );
[130] Fix | Delete
add_filter( 'sidebars_widgets', array( __CLASS__, 'sidebars_widgets' ) );
[131] Fix | Delete
add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) );
[132] Fix | Delete
}
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* Enqueue the block-based widget visibility scripts.
[137] Fix | Delete
*/
[138] Fix | Delete
public static function setup_block_controls() {
[139] Fix | Delete
Assets::register_script(
[140] Fix | Delete
'widget-visibility-editor',
[141] Fix | Delete
'_inc/build/widget-visibility/editor/index.js',
[142] Fix | Delete
JETPACK__PLUGIN_FILE,
[143] Fix | Delete
array(
[144] Fix | Delete
'in_footer' => true,
[145] Fix | Delete
'textdomain' => 'jetpack',
[146] Fix | Delete
)
[147] Fix | Delete
);
[148] Fix | Delete
Assets::enqueue_script( 'widget-visibility-editor' );
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* Add the 'conditions' attribute, where visibility rules are stored, to some blocks.
[153] Fix | Delete
*
[154] Fix | Delete
* We normally add the block attributes in the browser's javascript env only,
[155] Fix | Delete
* but these blocks use a ServerSideRender dynamic preview, so the php env needs
[156] Fix | Delete
* to know about the new attribute, too.
[157] Fix | Delete
*/
[158] Fix | Delete
public static function add_block_attributes_filter() {
[159] Fix | Delete
$blocks = array(
[160] Fix | Delete
// These use <ServerSideRender>.
[161] Fix | Delete
'core/calendar',
[162] Fix | Delete
'core/latest-comments',
[163] Fix | Delete
'core/rss',
[164] Fix | Delete
'core/archives',
[165] Fix | Delete
'core/tag-cloud',
[166] Fix | Delete
'core/page-list',
[167] Fix | Delete
'core/latest-posts',
[168] Fix | Delete
'woocommerce/product-categories',
[169] Fix | Delete
);
[170] Fix | Delete
/**
[171] Fix | Delete
* Filters the list of widget visibility blocks using <ServerSideRender>.
[172] Fix | Delete
*
[173] Fix | Delete
* @since 12.4
[174] Fix | Delete
*
[175] Fix | Delete
* @module widget-visibility
[176] Fix | Delete
*
[177] Fix | Delete
* @param string[] $blocks Array of block names from WordPress core and WooCommerce.
[178] Fix | Delete
*/
[179] Fix | Delete
$blocks_to_add_visibility_conditions = apply_filters( 'jetpack_widget_visibility_server_side_render_blocks', $blocks );
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* Block registration filter callback.
[183] Fix | Delete
*
[184] Fix | Delete
* @param array $settings Array of arguments for registering a block type.
[185] Fix | Delete
* @param string $name Block type name including namespace.
[186] Fix | Delete
* @return array
[187] Fix | Delete
*/
[188] Fix | Delete
$filter_metadata_registration = function ( $settings, $name ) use ( $blocks_to_add_visibility_conditions ) {
[189] Fix | Delete
if ( in_array( $name, $blocks_to_add_visibility_conditions, true ) && ! empty( $settings['attributes'] ) ) {
[190] Fix | Delete
$settings['attributes']['conditions'] = array(
[191] Fix | Delete
'type' => 'object',
[192] Fix | Delete
);
[193] Fix | Delete
}
[194] Fix | Delete
return $settings;
[195] Fix | Delete
};
[196] Fix | Delete
[197] Fix | Delete
add_filter( 'register_block_type_args', $filter_metadata_registration, 10, 2 );
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
/**
[201] Fix | Delete
* Prepare the interface for editing widgets - loading css, javascript & data
[202] Fix | Delete
*/
[203] Fix | Delete
public static function widget_admin_setup() {
[204] Fix | Delete
wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ), array( 'widgets' ), JETPACK__VERSION );
[205] Fix | Delete
wp_style_add_data( 'widget-conditions', 'rtl', 'replace' );
[206] Fix | Delete
wp_enqueue_script(
[207] Fix | Delete
'widget-conditions',
[208] Fix | Delete
Assets::get_file_url_for_environment(
[209] Fix | Delete
'_inc/build/widget-visibility/widget-conditions/widget-conditions.min.js',
[210] Fix | Delete
'modules/widget-visibility/widget-conditions/widget-conditions.js'
[211] Fix | Delete
),
[212] Fix | Delete
array( 'jquery', 'jquery-ui-core' ),
[213] Fix | Delete
JETPACK__VERSION,
[214] Fix | Delete
true
[215] Fix | Delete
);
[216] Fix | Delete
[217] Fix | Delete
// Set up a single copy of all of the data that Widget Visibility needs.
[218] Fix | Delete
// This allows all widget conditions to reuse the same data, keeping page size down
[219] Fix | Delete
// and eliminating the AJAX calls we used to have to use to fetch the minor rule options.
[220] Fix | Delete
$widget_conditions_data = array();
[221] Fix | Delete
[222] Fix | Delete
$widget_conditions_data['category'] = array();
[223] Fix | Delete
$widget_conditions_data['category'][] = array( '', __( 'All category pages', 'jetpack' ) );
[224] Fix | Delete
[225] Fix | Delete
$categories = get_categories(
[226] Fix | Delete
array(
[227] Fix | Delete
/**
[228] Fix | Delete
* Specific a maximum number of categories to query for the Widget visibility UI.
[229] Fix | Delete
*
[230] Fix | Delete
* @module widget-visibility
[231] Fix | Delete
*
[232] Fix | Delete
* @since 9.1.0
[233] Fix | Delete
*
[234] Fix | Delete
* @param int $number Maximum number of categories displayed in the Widget visibility UI.
[235] Fix | Delete
*/
[236] Fix | Delete
'number' => (int) apply_filters( 'jetpack_widget_visibility_max_number_categories', 1000 ),
[237] Fix | Delete
'orderby' => 'count',
[238] Fix | Delete
'order' => 'DESC',
[239] Fix | Delete
)
[240] Fix | Delete
);
[241] Fix | Delete
usort( $categories, array( __CLASS__, 'strcasecmp_name' ) );
[242] Fix | Delete
[243] Fix | Delete
foreach ( $categories as $category ) {
[244] Fix | Delete
$widget_conditions_data['category'][] = array( (string) $category->term_id, $category->name );
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
$widget_conditions_data['loggedin'] = array();
[248] Fix | Delete
$widget_conditions_data['loggedin'][] = array( 'loggedin', __( 'Logged In', 'jetpack' ) );
[249] Fix | Delete
$widget_conditions_data['loggedin'][] = array( 'loggedout', __( 'Logged Out', 'jetpack' ) );
[250] Fix | Delete
[251] Fix | Delete
$widget_conditions_data['author'] = array();
[252] Fix | Delete
$widget_conditions_data['author'][] = array( '', __( 'All author pages', 'jetpack' ) );
[253] Fix | Delete
[254] Fix | Delete
/*
[255] Fix | Delete
* Query for users with publish caps.
[256] Fix | Delete
*/
[257] Fix | Delete
$authors_args = array(
[258] Fix | Delete
'orderby' => 'name',
[259] Fix | Delete
'capability' => array( 'edit_posts' ),
[260] Fix | Delete
'fields' => array( 'ID', 'display_name' ),
[261] Fix | Delete
);
[262] Fix | Delete
[263] Fix | Delete
$authors = get_users( $authors_args );
[264] Fix | Delete
[265] Fix | Delete
foreach ( $authors as $author ) {
[266] Fix | Delete
$widget_conditions_data['author'][] = array( (string) $author->ID, $author->display_name );
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
$widget_conditions_data['role'] = array();
[270] Fix | Delete
[271] Fix | Delete
global $wp_roles;
[272] Fix | Delete
[273] Fix | Delete
foreach ( $wp_roles->roles as $role_key => $role ) {
[274] Fix | Delete
$widget_conditions_data['role'][] = array( (string) $role_key, $role['name'] );
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
$widget_conditions_data['tag'] = array();
[278] Fix | Delete
$widget_conditions_data['tag'][] = array( '', __( 'All tag pages', 'jetpack' ) );
[279] Fix | Delete
[280] Fix | Delete
$tags = get_tags(
[281] Fix | Delete
array(
[282] Fix | Delete
/**
[283] Fix | Delete
* Specific a maximum number of tags to query for the Widget visibility UI.
[284] Fix | Delete
*
[285] Fix | Delete
* @module widget-visibility
[286] Fix | Delete
*
[287] Fix | Delete
* @since 9.1.0
[288] Fix | Delete
*
[289] Fix | Delete
* @param int $number Maximum number of tags displayed in the Widget visibility UI.
[290] Fix | Delete
*/
[291] Fix | Delete
'number' => (int) apply_filters( 'jetpack_widget_visibility_max_number_tags', 1000 ),
[292] Fix | Delete
'orderby' => 'count',
[293] Fix | Delete
'order' => 'DESC',
[294] Fix | Delete
)
[295] Fix | Delete
);
[296] Fix | Delete
usort( $tags, array( __CLASS__, 'strcasecmp_name' ) );
[297] Fix | Delete
[298] Fix | Delete
foreach ( $tags as $tag ) {
[299] Fix | Delete
$widget_conditions_data['tag'][] = array( (string) $tag->term_id, $tag->name );
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
$widget_conditions_data['date'] = array();
[303] Fix | Delete
$widget_conditions_data['date'][] = array( '', __( 'All date archives', 'jetpack' ) );
[304] Fix | Delete
$widget_conditions_data['date'][] = array( 'day', __( 'Daily archives', 'jetpack' ) );
[305] Fix | Delete
$widget_conditions_data['date'][] = array( 'month', __( 'Monthly archives', 'jetpack' ) );
[306] Fix | Delete
$widget_conditions_data['date'][] = array( 'year', __( 'Yearly archives', 'jetpack' ) );
[307] Fix | Delete
[308] Fix | Delete
$widget_conditions_data['page'] = array();
[309] Fix | Delete
$widget_conditions_data['page'][] = array( 'front', __( 'Front page', 'jetpack' ) );
[310] Fix | Delete
$widget_conditions_data['page'][] = array( 'posts', __( 'Posts page', 'jetpack' ) );
[311] Fix | Delete
$widget_conditions_data['page'][] = array( 'archive', __( 'Archive page', 'jetpack' ) );
[312] Fix | Delete
$widget_conditions_data['page'][] = array( '404', __( '404 error page', 'jetpack' ) );
[313] Fix | Delete
$widget_conditions_data['page'][] = array( 'search', __( 'Search results', 'jetpack' ) );
[314] Fix | Delete
[315] Fix | Delete
$post_types = get_post_types( array( 'public' => true ), 'objects' );
[316] Fix | Delete
[317] Fix | Delete
$widget_conditions_post_types = array();
[318] Fix | Delete
$widget_conditions_post_type_archives = array();
[319] Fix | Delete
[320] Fix | Delete
foreach ( $post_types as $post_type ) {
[321] Fix | Delete
$widget_conditions_post_types[] = array( 'post_type-' . $post_type->name, $post_type->labels->singular_name );
[322] Fix | Delete
$widget_conditions_post_type_archives[] = array( 'post_type_archive-' . $post_type->name, $post_type->labels->name );
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
$widget_conditions_data['page'][] = array( __( 'Post type:', 'jetpack' ), $widget_conditions_post_types );
[326] Fix | Delete
[327] Fix | Delete
$widget_conditions_data['page'][] = array( __( 'Post type Archives:', 'jetpack' ), $widget_conditions_post_type_archives );
[328] Fix | Delete
[329] Fix | Delete
$pages = self::get_pages();
[330] Fix | Delete
[331] Fix | Delete
$dropdown_tree_args = array(
[332] Fix | Delete
'depth' => 0,
[333] Fix | Delete
'child_of' => 0,
[334] Fix | Delete
'selected' => 0,
[335] Fix | Delete
'echo' => false,
[336] Fix | Delete
'name' => 'page_id',
[337] Fix | Delete
'id' => '',
[338] Fix | Delete
'class' => '',
[339] Fix | Delete
'show_option_none' => '',
[340] Fix | Delete
'show_option_no_change' => '',
[341] Fix | Delete
'option_none_value' => '',
[342] Fix | Delete
'value_field' => 'ID',
[343] Fix | Delete
);
[344] Fix | Delete
$pages_dropdown = walk_page_dropdown_tree( $pages, 0, $dropdown_tree_args );
[345] Fix | Delete
preg_match_all( '/value=.([0-9]+).[^>]*>([^<]+)</', $pages_dropdown, $page_ids_and_titles, PREG_SET_ORDER );
[346] Fix | Delete
$static_pages = array();
[347] Fix | Delete
[348] Fix | Delete
foreach ( $page_ids_and_titles as $page_id_and_title ) {
[349] Fix | Delete
$static_pages[] = array( (string) $page_id_and_title[1], $page_id_and_title[2] );
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
$widget_conditions_data['page'][] = array( __( 'Static page:', 'jetpack' ), $static_pages );
[353] Fix | Delete
[354] Fix | Delete
$widget_conditions_data['taxonomy'] = array();
[355] Fix | Delete
$widget_conditions_data['taxonomy'][] = array( '', __( 'All taxonomy pages', 'jetpack' ) );
[356] Fix | Delete
[357] Fix | Delete
$taxonomies = get_taxonomies(
[358] Fix | Delete
/**
[359] Fix | Delete
* Filters args passed to get_taxonomies.
[360] Fix | Delete
*
[361] Fix | Delete
* @see https://developer.wordpress.org/reference/functions/get_taxonomies/
[362] Fix | Delete
*
[363] Fix | Delete
* @since 5.3.0
[364] Fix | Delete
*
[365] Fix | Delete
* @module widget-visibility
[366] Fix | Delete
*
[367] Fix | Delete
* @param array $args Widget Visibility taxonomy arguments.
[368] Fix | Delete
*/
[369] Fix | Delete
apply_filters( 'jetpack_widget_visibility_tax_args', array( '_builtin' => false ) ),
[370] Fix | Delete
'objects'
[371] Fix | Delete
);
[372] Fix | Delete
[373] Fix | Delete
usort( $taxonomies, array( __CLASS__, 'strcasecmp_name' ) );
[374] Fix | Delete
[375] Fix | Delete
foreach ( $taxonomies as $taxonomy ) {
[376] Fix | Delete
$taxonomy_terms = get_terms(
[377] Fix | Delete
array( $taxonomy->name ),
[378] Fix | Delete
array(
[379] Fix | Delete
'number' => 250,
[380] Fix | Delete
'hide_empty' => false,
[381] Fix | Delete
)
[382] Fix | Delete
);
[383] Fix | Delete
[384] Fix | Delete
$widget_conditions_terms = array();
[385] Fix | Delete
$widget_conditions_terms[] = array( $taxonomy->name, $taxonomy->labels->all_items );
[386] Fix | Delete
[387] Fix | Delete
foreach ( $taxonomy_terms as $term ) {
[388] Fix | Delete
$widget_conditions_terms[] = array( $taxonomy->name . '_tax_' . $term->term_id, $term->name );
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
$widget_conditions_data['taxonomy'][] = array( $taxonomy->labels->name . ':', $widget_conditions_terms );
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
wp_localize_script( 'widget-conditions', 'widget_conditions_data', $widget_conditions_data );
[395] Fix | Delete
[396] Fix | Delete
// Save a list of the IDs of all pages that have children for dynamically showing the "Include children" checkbox.
[397] Fix | Delete
$all_pages = self::get_pages();
[398] Fix | Delete
$all_parents = array();
[399] Fix | Delete
[400] Fix | Delete
foreach ( $all_pages as $page ) {
[401] Fix | Delete
if ( $page->post_parent ) {
[402] Fix | Delete
$all_parents[ (string) $page->post_parent ] = true;
[403] Fix | Delete
}
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
$front_page_id = get_option( 'page_on_front' );
[407] Fix | Delete
[408] Fix | Delete
if ( isset( $all_parents[ $front_page_id ] ) ) {
[409] Fix | Delete
$all_parents['front'] = true;
[410] Fix | Delete
}
[411] Fix | Delete
[412] Fix | Delete
wp_localize_script( 'widget-conditions', 'widget_conditions_parent_pages', $all_parents );
[413] Fix | Delete
}
[414] Fix | Delete
[415] Fix | Delete
/**
[416] Fix | Delete
* Retrieves a full list of all pages, containing just the IDs, post_parent, post_title, and post_status fields.
[417] Fix | Delete
*
[418] Fix | Delete
* Since the WordPress' `get_pages` function does not allow us to fetch only the fields mentioned
[419] Fix | Delete
* above, we need to introduce a custom method using a direct SQL query fetching those.
[420] Fix | Delete
*
[421] Fix | Delete
* By fetching only those four fields and not populating the object cache for all the pages, we can
[422] Fix | Delete
* improve the performance of the query on sites having a lot of pages.
[423] Fix | Delete
*
[424] Fix | Delete
* @see https://core.trac.wordpress.org/ticket/51469
[425] Fix | Delete
*
[426] Fix | Delete
* @return array List of all pages on the site (stdClass objects containing ID, post_title, post_parent, and post_status only).
[427] Fix | Delete
*/
[428] Fix | Delete
public static function get_pages() {
[429] Fix | Delete
global $wpdb;
[430] Fix | Delete
[431] Fix | Delete
$last_changed = wp_cache_get_last_changed( 'posts' );
[432] Fix | Delete
$cache_key = "get_pages:$last_changed";
[433] Fix | Delete
$pages = wp_cache_get( $cache_key, 'widget_conditions' );
[434] Fix | Delete
if ( false === $pages ) {
[435] Fix | Delete
$pages = $wpdb->get_results( "SELECT {$wpdb->posts}.ID, {$wpdb->posts}.post_parent, {$wpdb->posts}.post_title, {$wpdb->posts}.post_status FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'page' AND {$wpdb->posts}.post_status = 'publish' ORDER BY {$wpdb->posts}.post_title ASC" );
[436] Fix | Delete
wp_cache_set( $cache_key, $pages, 'widget_conditions' );
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
// Copy-pasted from the get_pages function. For usage in the `widget_conditions_get_pages` filter.
[440] Fix | Delete
$parsed_args = array(
[441] Fix | Delete
'child_of' => 0,
[442] Fix | Delete
'sort_order' => 'ASC',
[443] Fix | Delete
'sort_column' => 'post_title',
[444] Fix | Delete
'hierarchical' => 1,
[445] Fix | Delete
'exclude' => array(),
[446] Fix | Delete
'include' => array(),
[447] Fix | Delete
'meta_key' => '',
[448] Fix | Delete
'meta_value' => '',
[449] Fix | Delete
'authors' => '',
[450] Fix | Delete
'parent' => -1,
[451] Fix | Delete
'exclude_tree' => array(),
[452] Fix | Delete
'number' => '',
[453] Fix | Delete
'offset' => 0,
[454] Fix | Delete
'post_type' => 'page',
[455] Fix | Delete
'post_status' => 'publish',
[456] Fix | Delete
);
[457] Fix | Delete
[458] Fix | Delete
/**
[459] Fix | Delete
* Filters the retrieved list of pages.
[460] Fix | Delete
*
[461] Fix | Delete
* @since 9.1.0
[462] Fix | Delete
*
[463] Fix | Delete
* @module widget-visibility
[464] Fix | Delete
*
[465] Fix | Delete
* @param stdClass[] $pages Array of objects containing only the ID, post_parent, post_title, and post_status fields.
[466] Fix | Delete
* @param array $parsed_args Array of get_pages() arguments.
[467] Fix | Delete
*/
[468] Fix | Delete
return apply_filters( 'jetpack_widget_visibility_get_pages', $pages, $parsed_args );
[469] Fix | Delete
}
[470] Fix | Delete
[471] Fix | Delete
/**
[472] Fix | Delete
* Add the widget conditions to each widget in the admin.
[473] Fix | Delete
*
[474] Fix | Delete
* @param WP_Widget $widget Widget to add conditions settings to.
[475] Fix | Delete
* @param null $return unused.
[476] Fix | Delete
* @param array $instance The widget settings.
[477] Fix | Delete
*/
[478] Fix | Delete
public static function widget_conditions_admin( $widget, $return, $instance ) {
[479] Fix | Delete
$conditions = array();
[480] Fix | Delete
[481] Fix | Delete
if ( isset( $instance['conditions'] ) ) {
[482] Fix | Delete
$conditions = $instance['conditions'];
[483] Fix | Delete
}
[484] Fix | Delete
[485] Fix | Delete
if ( ! isset( $conditions['action'] ) ) {
[486] Fix | Delete
$conditions['action'] = 'show';
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
if ( empty( $conditions['rules'] ) ) {
[490] Fix | Delete
$conditions['rules'][] = array(
[491] Fix | Delete
'major' => '',
[492] Fix | Delete
'minor' => '',
[493] Fix | Delete
'has_children' => '',
[494] Fix | Delete
);
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
if ( empty( $conditions['match_all'] ) ) {
[498] Fix | Delete
$conditions['match_all'] = false;
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function