暫無描述

widget-conditions.php 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. <?php
  2. use Automattic\Jetpack\Assets;
  3. /**
  4. * Hide or show legacy widgets conditionally.
  5. *
  6. * This class has two responsiblities - administrating the conditions in which legacy widgets may be hidden or shown
  7. * and hiding/showing the legacy widgets on the front-end of the site, depending upon the evaluation of those conditions.
  8. *
  9. * Administrating the conditions can be done in one of four different WordPress screens, plus direct use of the API and
  10. * is supplemented with a legacy widget preview screen. The four different admin screens are
  11. *
  12. * Gutenberg widget experience - widget admin (widgets.php + API + legacy widget preview)
  13. * Gutenberg widget experience - Customizer (customizer screen/API + API + legacy widget preview)
  14. * Classic widget experience - widget admin (widgets.php + admin-ajax XHR requests)
  15. * Classic widget experience - Customizer (customizer screen/API)
  16. *
  17. * 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/
  18. */
  19. class Jetpack_Widget_Conditions {
  20. static $passed_template_redirect = false;
  21. public static function init() {
  22. global $pagenow;
  23. // The Gutenberg based widget experience will show a preview of legacy widgets by including a URL beginning
  24. // widgets.php?legacy-widget-preview inside an iframe. Previews don't need widget editing loaded and also don't
  25. // want to run the filter - if the widget is filtered out it'll be empty, which would be confusing.
  26. // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  27. if ( isset( $_GET['legacy-widget-preview'] ) ) {
  28. return;
  29. }
  30. // If action is posted and it's save-widget then it's relevant to widget conditions, otherwise it's something
  31. // else and it's not worth registering hooks.
  32. // phpcs:ignore WordPress.Security.NonceVerification.Missing
  33. if ( isset( $_POST['action'] ) && ! in_array( $_POST['action'], array( 'save-widget', 'update-widget' ), true ) ) {
  34. return;
  35. }
  36. // API call to *list* the widget types doesn't use editing visibility or display widgets.
  37. if ( false !== strpos( $_SERVER['REQUEST_URI'], '/widget-types?' ) ) {
  38. return;
  39. }
  40. $add_data_assets_to_page = false;
  41. $add_html_to_form = false;
  42. $handle_widget_updates = false;
  43. $add_block_controls = false;
  44. $using_classic_experience = ( ! function_exists( 'wp_use_widgets_block_editor' ) || ! wp_use_widgets_block_editor() );
  45. if ( $using_classic_experience &&
  46. (
  47. is_customize_preview() || 'widgets.php' === $pagenow ||
  48. // phpcs:ignore WordPress.Security.NonceVerification.Missing
  49. ( 'admin-ajax.php' === $pagenow && array_key_exists( 'action', $_POST ) && 'save-widget' === $_POST['action'] )
  50. )
  51. ) {
  52. $add_data_assets_to_page = true;
  53. $add_html_to_form = true;
  54. $handle_widget_updates = true;
  55. } else {
  56. // On a screen that is hosting the API in the gutenberg editing experience.
  57. if ( is_customize_preview() || 'widgets.php' === $pagenow ) {
  58. $add_data_assets_to_page = true;
  59. $add_block_controls = true;
  60. }
  61. // Encoding for a particular widget end point.
  62. if ( 1 === preg_match( '|/widget-types/.*/encode|', $_SERVER['REQUEST_URI'] ) ) {
  63. $add_html_to_form = true;
  64. $handle_widget_updates = true;
  65. }
  66. // Batch API is usually saving but could be anything.
  67. if ( false !== strpos( $_SERVER['REQUEST_URI'], '/wp-json/batch/v1' ) ) {
  68. $handle_widget_updates = true;
  69. $add_html_to_form = true;
  70. }
  71. // Saving widgets via non-batch API. This isn't used within WordPress but could be used by third parties in theory.
  72. if ( 'GET' !== $_SERVER['REQUEST_METHOD'] && false !== strpos( $_SERVER['REQUEST_URI'], '/wp/v2/widgets' ) ) {
  73. $handle_widget_updates = true;
  74. $add_html_to_form = true;
  75. }
  76. }
  77. if ( $add_html_to_form ) {
  78. add_action( 'in_widget_form', array( __CLASS__, 'widget_conditions_admin' ), 10, 3 );
  79. }
  80. if ( $handle_widget_updates ) {
  81. add_filter( 'widget_update_callback', array( __CLASS__, 'widget_update' ), 10, 3 );
  82. }
  83. if ( $add_data_assets_to_page ) {
  84. add_action( 'sidebar_admin_setup', array( __CLASS__, 'widget_admin_setup' ) );
  85. }
  86. if ( $add_block_controls ) {
  87. add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'setup_block_controls' ) );
  88. }
  89. if ( ! $add_html_to_form && ! $handle_widget_updates && ! $add_data_assets_to_page &&
  90. ! in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ), true )
  91. ) {
  92. // Not hit any known widget admin endpoint, register widget display hooks instead.
  93. add_filter( 'widget_display_callback', array( __CLASS__, 'filter_widget' ) );
  94. add_filter( 'sidebars_widgets', array( __CLASS__, 'sidebars_widgets' ) );
  95. add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) );
  96. }
  97. }
  98. /**
  99. * Enqueue the block-based widget visibility scripts.
  100. */
  101. public static function setup_block_controls() {
  102. $manifest_path = JETPACK__PLUGIN_DIR . '_inc/build/widget-visibility/editor/index.min.asset.php';
  103. $script_path = plugins_url( '_inc/build/widget-visibility/editor/index.min.js', JETPACK__PLUGIN_FILE );
  104. $script_dependencies = array( 'wp-polyfill' );
  105. if ( file_exists( $manifest_path ) ) {
  106. $asset_manifest = include $manifest_path;
  107. $script_dependencies = $asset_manifest['dependencies'];
  108. }
  109. // Enqueue built script.
  110. wp_enqueue_script(
  111. 'widget-visibility-editor',
  112. $script_path,
  113. $script_dependencies,
  114. JETPACK__VERSION,
  115. true
  116. );
  117. wp_set_script_translations( 'widget-visibility-editor', 'jetpack' );
  118. }
  119. /**
  120. * Add the 'conditions' attribute, where visibility rules are stored, to some blocks.
  121. *
  122. * We normally add the block attributes in the browser's javascript env only,
  123. * but these blocks use a ServerSideRender dynamic preview, so the php env needs
  124. * to know about the new attribute, too.
  125. */
  126. public static function add_block_attributes_filter() {
  127. $blocks_to_add_visibility_conditions = array(
  128. // These use <ServerSideRender>.
  129. 'core/calendar',
  130. 'core/latest-comments',
  131. 'core/rss',
  132. 'core/archives',
  133. 'core/tag-cloud',
  134. 'core/page-list',
  135. 'core/latest-posts',
  136. );
  137. $filter_metadata_registration = function ( $settings, $metadata ) use ( $blocks_to_add_visibility_conditions ) {
  138. if ( in_array( $metadata['name'], $blocks_to_add_visibility_conditions, true ) && ! empty( $settings['attributes'] ) ) {
  139. $settings['attributes']['conditions'] = array(
  140. 'type' => 'object',
  141. );
  142. }
  143. return $settings;
  144. };
  145. add_filter( 'block_type_metadata_settings', $filter_metadata_registration, 10, 2 );
  146. }
  147. /**
  148. * Prepare the interface for editing widgets - loading css, javascript & data
  149. */
  150. public static function widget_admin_setup() {
  151. wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ), array( 'widgets' ), JETPACK__VERSION );
  152. wp_style_add_data( 'widget-conditions', 'rtl', 'replace' );
  153. wp_enqueue_script(
  154. 'widget-conditions',
  155. Assets::get_file_url_for_environment(
  156. '_inc/build/widget-visibility/widget-conditions/widget-conditions.min.js',
  157. 'modules/widget-visibility/widget-conditions/widget-conditions.js'
  158. ),
  159. array( 'jquery', 'jquery-ui-core' ),
  160. JETPACK__VERSION,
  161. true
  162. );
  163. // Set up a single copy of all of the data that Widget Visibility needs.
  164. // This allows all widget conditions to reuse the same data, keeping page size down
  165. // and eliminating the AJAX calls we used to have to use to fetch the minor rule options.
  166. $widget_conditions_data = array();
  167. $widget_conditions_data['category'] = array();
  168. $widget_conditions_data['category'][] = array( '', __( 'All category pages', 'jetpack' ) );
  169. $categories = get_categories(
  170. array(
  171. /**
  172. * Specific a maximum number of categories to query for the Widget visibility UI.
  173. *
  174. * @module widget-visibility
  175. *
  176. * @since 9.1.0
  177. *
  178. * @param int $number Maximum number of categories displayed in the Widget visibility UI.
  179. */
  180. 'number' => (int) apply_filters( 'jetpack_widget_visibility_max_number_categories', 1000 ),
  181. 'orderby' => 'count',
  182. 'order' => 'DESC',
  183. )
  184. );
  185. usort( $categories, array( __CLASS__, 'strcasecmp_name' ) );
  186. foreach ( $categories as $category ) {
  187. $widget_conditions_data['category'][] = array( (string) $category->term_id, $category->name );
  188. }
  189. $widget_conditions_data['loggedin'] = array();
  190. $widget_conditions_data['loggedin'][] = array( 'loggedin', __( 'Logged In', 'jetpack' ) );
  191. $widget_conditions_data['loggedin'][] = array( 'loggedout', __( 'Logged Out', 'jetpack' ) );
  192. $widget_conditions_data['author'] = array();
  193. $widget_conditions_data['author'][] = array( '', __( 'All author pages', 'jetpack' ) );
  194. // Only users with publish caps
  195. $authors = get_users(
  196. array(
  197. 'orderby' => 'name',
  198. 'who' => 'authors',
  199. 'fields' => array( 'ID', 'display_name' ),
  200. )
  201. );
  202. foreach ( $authors as $author ) {
  203. $widget_conditions_data['author'][] = array( (string) $author->ID, $author->display_name );
  204. }
  205. $widget_conditions_data['role'] = array();
  206. global $wp_roles;
  207. foreach ( $wp_roles->roles as $role_key => $role ) {
  208. $widget_conditions_data['role'][] = array( (string) $role_key, $role['name'] );
  209. }
  210. $widget_conditions_data['tag'] = array();
  211. $widget_conditions_data['tag'][] = array( '', __( 'All tag pages', 'jetpack' ) );
  212. $tags = get_tags(
  213. array(
  214. /**
  215. * Specific a maximum number of tags to query for the Widget visibility UI.
  216. *
  217. * @module widget-visibility
  218. *
  219. * @since 9.1.0
  220. *
  221. * @param int $number Maximum number of tags displayed in the Widget visibility UI.
  222. */
  223. 'number' => (int) apply_filters( 'jetpack_widget_visibility_max_number_tags', 1000 ),
  224. 'orderby' => 'count',
  225. 'order' => 'DESC',
  226. )
  227. );
  228. usort( $tags, array( __CLASS__, 'strcasecmp_name' ) );
  229. foreach ( $tags as $tag ) {
  230. $widget_conditions_data['tag'][] = array( (string) $tag->term_id, $tag->name );
  231. }
  232. $widget_conditions_data['date'] = array();
  233. $widget_conditions_data['date'][] = array( '', __( 'All date archives', 'jetpack' ) );
  234. $widget_conditions_data['date'][] = array( 'day', __( 'Daily archives', 'jetpack' ) );
  235. $widget_conditions_data['date'][] = array( 'month', __( 'Monthly archives', 'jetpack' ) );
  236. $widget_conditions_data['date'][] = array( 'year', __( 'Yearly archives', 'jetpack' ) );
  237. $widget_conditions_data['page'] = array();
  238. $widget_conditions_data['page'][] = array( 'front', __( 'Front page', 'jetpack' ) );
  239. $widget_conditions_data['page'][] = array( 'posts', __( 'Posts page', 'jetpack' ) );
  240. $widget_conditions_data['page'][] = array( 'archive', __( 'Archive page', 'jetpack' ) );
  241. $widget_conditions_data['page'][] = array( '404', __( '404 error page', 'jetpack' ) );
  242. $widget_conditions_data['page'][] = array( 'search', __( 'Search results', 'jetpack' ) );
  243. $post_types = get_post_types( array( 'public' => true ), 'objects' );
  244. $widget_conditions_post_types = array();
  245. $widget_conditions_post_type_archives = array();
  246. foreach ( $post_types as $post_type ) {
  247. $widget_conditions_post_types[] = array( 'post_type-' . $post_type->name, $post_type->labels->singular_name );
  248. $widget_conditions_post_type_archives[] = array( 'post_type_archive-' . $post_type->name, $post_type->labels->name );
  249. }
  250. $widget_conditions_data['page'][] = array( __( 'Post type:', 'jetpack' ), $widget_conditions_post_types );
  251. $widget_conditions_data['page'][] = array( __( 'Post type Archives:', 'jetpack' ), $widget_conditions_post_type_archives );
  252. $pages = self::get_pages();
  253. $dropdown_tree_args = array(
  254. 'depth' => 0,
  255. 'child_of' => 0,
  256. 'selected' => 0,
  257. 'echo' => false,
  258. 'name' => 'page_id',
  259. 'id' => '',
  260. 'class' => '',
  261. 'show_option_none' => '',
  262. 'show_option_no_change' => '',
  263. 'option_none_value' => '',
  264. 'value_field' => 'ID',
  265. );
  266. $pages_dropdown = walk_page_dropdown_tree( $pages, 0, $dropdown_tree_args );
  267. preg_match_all( '/value=.([0-9]+).[^>]*>([^<]+)</', $pages_dropdown, $page_ids_and_titles, PREG_SET_ORDER );
  268. $static_pages = array();
  269. foreach ( $page_ids_and_titles as $page_id_and_title ) {
  270. $static_pages[] = array( (string) $page_id_and_title[1], $page_id_and_title[2] );
  271. }
  272. $widget_conditions_data['page'][] = array( __( 'Static page:', 'jetpack' ), $static_pages );
  273. $widget_conditions_data['taxonomy'] = array();
  274. $widget_conditions_data['taxonomy'][] = array( '', __( 'All taxonomy pages', 'jetpack' ) );
  275. $taxonomies = get_taxonomies(
  276. /**
  277. * Filters args passed to get_taxonomies.
  278. *
  279. * @see https://developer.wordpress.org/reference/functions/get_taxonomies/
  280. *
  281. * @since 5.3.0
  282. *
  283. * @module widget-visibility
  284. *
  285. * @param array $args Widget Visibility taxonomy arguments.
  286. */
  287. apply_filters( 'jetpack_widget_visibility_tax_args', array( '_builtin' => false ) ),
  288. 'objects'
  289. );
  290. usort( $taxonomies, array( __CLASS__, 'strcasecmp_name' ) );
  291. foreach ( $taxonomies as $taxonomy ) {
  292. $taxonomy_terms = get_terms(
  293. array( $taxonomy->name ),
  294. array(
  295. 'number' => 250,
  296. 'hide_empty' => false,
  297. )
  298. );
  299. $widget_conditions_terms = array();
  300. $widget_conditions_terms[] = array( $taxonomy->name, $taxonomy->labels->all_items );
  301. foreach ( $taxonomy_terms as $term ) {
  302. $widget_conditions_terms[] = array( $taxonomy->name . '_tax_' . $term->term_id, $term->name );
  303. }
  304. $widget_conditions_data['taxonomy'][] = array( $taxonomy->labels->name . ':', $widget_conditions_terms );
  305. }
  306. wp_localize_script( 'widget-conditions', 'widget_conditions_data', $widget_conditions_data );
  307. // Save a list of the IDs of all pages that have children for dynamically showing the "Include children" checkbox.
  308. $all_pages = self::get_pages();
  309. $all_parents = array();
  310. foreach ( $all_pages as $page ) {
  311. if ( $page->post_parent ) {
  312. $all_parents[ (string) $page->post_parent ] = true;
  313. }
  314. }
  315. $front_page_id = get_option( 'page_on_front' );
  316. if ( isset( $all_parents[ $front_page_id ] ) ) {
  317. $all_parents['front'] = true;
  318. }
  319. wp_localize_script( 'widget-conditions', 'widget_conditions_parent_pages', $all_parents );
  320. }
  321. /**
  322. * Retrieves a full list of all pages, containing just the IDs, post_parent, post_title, and post_status fields.
  323. *
  324. * Since the WordPress' `get_pages` function does not allow us to fetch only the fields mentioned
  325. * above, we need to introduce a custom method using a direct SQL query fetching those.
  326. *
  327. * By fetching only those four fields and not populating the object cache for all the pages, we can
  328. * improve the performance of the query on sites having a lot of pages.
  329. *
  330. * @see https://core.trac.wordpress.org/ticket/51469
  331. *
  332. * @return array List of all pages on the site (stdClass objects containing ID, post_title, post_parent, and post_status only).
  333. */
  334. public static function get_pages() {
  335. global $wpdb;
  336. $last_changed = wp_cache_get_last_changed( 'posts' );
  337. $cache_key = "get_pages:$last_changed";
  338. $pages = wp_cache_get( $cache_key, 'widget_conditions' );
  339. if ( false === $pages ) {
  340. $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" );
  341. wp_cache_set( $cache_key, $pages, 'widget_conditions' );
  342. }
  343. // Copy-pasted from the get_pages function. For usage in the `widget_conditions_get_pages` filter.
  344. $parsed_args = array(
  345. 'child_of' => 0,
  346. 'sort_order' => 'ASC',
  347. 'sort_column' => 'post_title',
  348. 'hierarchical' => 1,
  349. 'exclude' => array(),
  350. 'include' => array(),
  351. 'meta_key' => '',
  352. 'meta_value' => '',
  353. 'authors' => '',
  354. 'parent' => -1,
  355. 'exclude_tree' => array(),
  356. 'number' => '',
  357. 'offset' => 0,
  358. 'post_type' => 'page',
  359. 'post_status' => 'publish',
  360. );
  361. /**
  362. * Filters the retrieved list of pages.
  363. *
  364. * @since 9.1.0
  365. *
  366. * @module widget-visibility
  367. *
  368. * @param stdClass[] $pages Array of objects containing only the ID, post_parent, post_title, and post_status fields.
  369. * @param array $parsed_args Array of get_pages() arguments.
  370. */
  371. return apply_filters( 'jetpack_widget_visibility_get_pages', $pages, $parsed_args );
  372. }
  373. /**
  374. * Add the widget conditions to each widget in the admin.
  375. *
  376. * @param WP_Widget $widget Widget to add conditions settings to.
  377. * @param null $return unused.
  378. * @param array $instance The widget settings.
  379. */
  380. public static function widget_conditions_admin( $widget, $return, $instance ) {
  381. $conditions = array();
  382. if ( isset( $instance['conditions'] ) ) {
  383. $conditions = $instance['conditions'];
  384. }
  385. if ( ! isset( $conditions['action'] ) ) {
  386. $conditions['action'] = 'show';
  387. }
  388. if ( empty( $conditions['rules'] ) ) {
  389. $conditions['rules'][] = array(
  390. 'major' => '',
  391. 'minor' => '',
  392. 'has_children' => '',
  393. );
  394. }
  395. if ( empty( $conditions['match_all'] ) ) {
  396. $conditions['match_all'] = false;
  397. }
  398. ?>
  399. <div
  400. class="
  401. widget-conditional
  402. <?php
  403. // $_POST['widget-conditions-visible'] is used in the classic widget experience to decide whether to
  404. // display the visibility panel open, e.g. when saving. In the gutenberg widget experience the POST
  405. // value will always be empty, but this is fine - it doesn't rerender the HTML when saving anyway.
  406. if (
  407. empty( $_POST['widget-conditions-visible'] )
  408. || $_POST['widget-conditions-visible'] == '0'
  409. ) {
  410. ?>
  411. widget-conditional-hide
  412. <?php
  413. }
  414. ?>
  415. <?php
  416. if ( ! empty( $conditions['match_all'] ) && $conditions['match_all'] ) {
  417. ?>
  418. intersection
  419. <?php
  420. } else {
  421. ?>
  422. conjunction
  423. <?php
  424. }
  425. ?>
  426. ">
  427. <input type="hidden" name="widget-conditions-visible" value="
  428. <?php
  429. if ( isset( $_POST['widget-conditions-visible'] ) ) {
  430. echo esc_attr( $_POST['widget-conditions-visible'] ); } else {
  431. ?>
  432. 0<?php } ?>" />
  433. <?php
  434. if ( ! isset( $_POST['widget-conditions-visible'] ) ) {
  435. ?>
  436. <a href="#" class="button display-options"><?php esc_html_e( 'Visibility', 'jetpack' ); ?></a><?php } ?>
  437. <div class="widget-conditional-inner">
  438. <div class="condition-top">
  439. <?php
  440. printf(
  441. // translators: %s is a HTML select widget for widget visibility, 'show' and 'hide' are it's options. It will read like 'show if' or 'hide if'.
  442. esc_html_x( '%s if:', 'placeholder: dropdown menu to select widget visibility; hide if or show if', 'jetpack' ),
  443. '<select name="' . esc_attr( $widget->get_field_name( 'conditions[action]' ) ) . '">
  444. <option value="show" ' . selected( $conditions['action'], 'show', false ) . '>' . esc_html_x( 'Show', 'Used in the "%s if:" translation for the widget visibility dropdown', 'jetpack' ) . '</option>
  445. <option value="hide" ' . selected( $conditions['action'], 'hide', false ) . '>' . esc_html_x( 'Hide', 'Used in the "%s if:" translation for the widget visibility dropdown', 'jetpack' ) . '</option>
  446. </select>'
  447. );
  448. ?>
  449. </div><!-- .condition-top -->
  450. <div class="conditions">
  451. <?php
  452. foreach ( $conditions['rules'] as $rule_index => $rule ) {
  453. $rule = wp_parse_args(
  454. $rule,
  455. array(
  456. 'major' => '',
  457. 'minor' => '',
  458. 'has_children' => '',
  459. )
  460. );
  461. ?>
  462. <div class="condition" data-rule-major="<?php echo esc_attr( $rule['major'] ); ?>" data-rule-minor="<?php echo esc_attr( $rule['minor'] ); ?>" data-rule-has-children="<?php echo esc_attr( $rule['has_children'] ); ?>">
  463. <div class="selection alignleft">
  464. <select class="conditions-rule-major" name="<?php echo esc_attr( $widget->get_field_name( 'conditions[rules_major][]' ) ); ?>">
  465. <option value="" <?php selected( '', $rule['major'] ); ?>><?php echo esc_html_x( '-- Select --', 'Used as the default option in a dropdown list', 'jetpack' ); ?></option>
  466. <option value="category" <?php selected( 'category', $rule['major'] ); ?>><?php esc_html_e( 'Category', 'jetpack' ); ?></option>
  467. <option value="author" <?php selected( 'author', $rule['major'] ); ?>><?php echo esc_html_x( 'Author', 'Noun, as in: "The author of this post is..."', 'jetpack' ); ?></option>
  468. <?php if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { // this doesn't work on .com because of caching ?>
  469. <option value="loggedin" <?php selected( 'loggedin', $rule['major'] ); ?>><?php echo esc_html_x( 'User', 'Noun', 'jetpack' ); ?></option>
  470. <option value="role" <?php selected( 'role', $rule['major'] ); ?>><?php echo esc_html_x( 'Role', 'Noun, as in: "The user role of that can access this widget is..."', 'jetpack' ); ?></option>
  471. <?php } ?>
  472. <option value="tag" <?php selected( 'tag', $rule['major'] ); ?>><?php echo esc_html_x( 'Tag', 'Noun, as in: "This post has one tag."', 'jetpack' ); ?></option>
  473. <option value="date" <?php selected( 'date', $rule['major'] ); ?>><?php echo esc_html_x( 'Date', 'Noun, as in: "This page is a date archive."', 'jetpack' ); ?></option>
  474. <option value="page" <?php selected( 'page', $rule['major'] ); ?>><?php echo esc_html_x( 'Page', 'Example: The user is looking at a page, not a post.', 'jetpack' ); ?></option>
  475. <?php if ( get_taxonomies( array( '_builtin' => false ) ) ) : ?>
  476. <option value="taxonomy" <?php selected( 'taxonomy', $rule['major'] ); ?>><?php echo esc_html_x( 'Taxonomy', 'Noun, as in: "This post has one taxonomy."', 'jetpack' ); ?></option>
  477. <?php endif; ?>
  478. </select>
  479. <?php _ex( 'is', 'Widget Visibility: {Rule Major [Page]} is {Rule Minor [Search results]}', 'jetpack' ); ?>
  480. <select class="conditions-rule-minor" name="<?php echo esc_attr( $widget->get_field_name( 'conditions[rules_minor][]' ) ); ?>"
  481. <?php
  482. if ( ! $rule['major'] ) {
  483. ?>
  484. disabled="disabled"<?php } ?>>
  485. <?php
  486. /*
  487. Include the currently selected value so that if the widget is saved without
  488. expanding the Visibility section, we don't lose the minor part of the rule.
  489. If it is opened, this list is cleared out and populated with all the values. */
  490. ?>
  491. <option value="<?php echo esc_attr( $rule['minor'] ); ?>" selected="selected"></option>
  492. </select>
  493. <span class="conditions-rule-has-children"
  494. <?php
  495. if ( ! $rule['has_children'] ) {
  496. ?>
  497. style="display: none;"<?php } ?>>
  498. <label>
  499. <input type="checkbox" name="<?php echo esc_attr( $widget->get_field_name( "conditions[page_children][$rule_index]" ) ); ?>" value="has" <?php checked( $rule['has_children'], true ); ?> />
  500. <?php echo esc_html_x( 'Include children', 'Checkbox on Widget Visibility if children of the selected page should be included in the visibility rule.', 'jetpack' ); ?>
  501. </label>
  502. </span>
  503. </div>
  504. <div class="condition-control">
  505. <span class="condition-conjunction">
  506. <?php echo esc_html_x( 'or', 'Shown between widget visibility conditions.', 'jetpack' ); ?>
  507. </span>
  508. <span class="condition-intersection">
  509. <?php echo esc_html_x( 'and', 'Shown between widget visibility conditions.', 'jetpack' ); ?>
  510. </span>
  511. <div class="actions alignright">
  512. <a href="#" class="delete-condition dashicons dashicons-no"><?php esc_html_e( 'Delete', 'jetpack' ); ?></a><a href="#" class="add-condition dashicons dashicons-plus"><?php esc_html_e( 'Add', 'jetpack' ); ?></a>
  513. </div>
  514. </div>
  515. </div><!-- .condition -->
  516. <?php
  517. }
  518. ?>
  519. </div><!-- .conditions -->
  520. <div class="conditions">
  521. <div class="condition-top">
  522. <label>
  523. <input
  524. type="checkbox"
  525. name="<?php echo esc_attr( $widget->get_field_name( 'conditions[match_all]' ) ); ?>"
  526. value="1"
  527. class="conditions-match-all"
  528. <?php checked( $conditions['match_all'], '1' ); ?> />
  529. <?php esc_html_e( 'Match all conditions', 'jetpack' ); ?>
  530. </label>
  531. </div><!-- .condition-top -->
  532. </div><!-- .conditions -->
  533. </div><!-- .widget-conditional-inner -->
  534. </div><!-- .widget-conditional -->
  535. <?php
  536. }
  537. /**
  538. * On an AJAX update of the widget settings, process the display conditions.
  539. *
  540. * @param array $instance The current instance's settings.
  541. * @param array $new_instance New settings for this instance as input by the user.
  542. * @param array $old_instance Old settings for this instance.
  543. * @return array Modified settings.
  544. */
  545. public static function widget_update( $instance, $new_instance, $old_instance ) {
  546. $conditions = array();
  547. $conditions['action'] = isset( $new_instance['conditions']['action'] ) ? $new_instance['conditions']['action'] : null;
  548. $conditions['match_all'] = isset( $new_instance['conditions']['match_all'] ) ? '1' : '0';
  549. $conditions['rules'] = isset( $new_instance['conditions']['rules'] ) ? $new_instance['conditions']['rules'] : array();
  550. if ( isset( $new_instance['conditions']['rules_major'] ) ) {
  551. foreach ( $new_instance['conditions']['rules_major'] as $index => $major_rule ) {
  552. if ( ! $major_rule ) {
  553. continue;
  554. }
  555. $conditions['rules'][] = array(
  556. 'major' => $major_rule,
  557. 'minor' => isset( $new_instance['conditions']['rules_minor'][ $index ] ) ? $new_instance['conditions']['rules_minor'][ $index ] : '',
  558. 'has_children' => isset( $new_instance['conditions']['page_children'][ $index ] ) ? true : false,
  559. );
  560. }
  561. }
  562. if ( ! empty( $conditions['rules'] ) ) {
  563. $instance['conditions'] = $conditions;
  564. } elseif ( empty( $new_instance['conditions']['rules'] ) ) {
  565. unset( $instance['conditions'] );
  566. }
  567. if (
  568. ( isset( $instance['conditions'] ) && ! isset( $old_instance['conditions'] ) )
  569. ||
  570. (
  571. isset( $instance['conditions'], $old_instance['conditions'] )
  572. &&
  573. serialize( $instance['conditions'] ) != serialize( $old_instance['conditions'] )
  574. )
  575. ) {
  576. /**
  577. * Fires after the widget visibility conditions are saved.
  578. *
  579. * @module widget-visibility
  580. *
  581. * @since 2.4.0
  582. */
  583. do_action( 'widget_conditions_save' );
  584. } elseif ( ! isset( $instance['conditions'] ) && isset( $old_instance['conditions'] ) ) {
  585. /**
  586. * Fires after the widget visibility conditions are deleted.
  587. *
  588. * @module widget-visibility
  589. *
  590. * @since 2.4.0
  591. */
  592. do_action( 'widget_conditions_delete' );
  593. }
  594. return $instance;
  595. }
  596. /**
  597. * Filter the list of widgets for a sidebar so that active sidebars work as expected.
  598. *
  599. * @param array $widget_areas An array of widget areas and their widgets.
  600. * @return array The modified $widget_area array.
  601. */
  602. public static function sidebars_widgets( $widget_areas ) {
  603. $settings = array();
  604. foreach ( $widget_areas as $widget_area => $widgets ) {
  605. if ( empty( $widgets ) ) {
  606. continue;
  607. }
  608. if ( ! is_array( $widgets ) ) {
  609. continue;
  610. }
  611. if ( 'wp_inactive_widgets' == $widget_area ) {
  612. continue;
  613. }
  614. foreach ( $widgets as $position => $widget_id ) {
  615. // Find the conditions for this widget.
  616. if ( preg_match( '/^(.+?)-(\d+)$/', $widget_id, $matches ) ) {
  617. $id_base = $matches[1];
  618. $widget_number = (int) $matches[2];
  619. } else {
  620. $id_base = $widget_id;
  621. $widget_number = null;
  622. }
  623. if ( ! isset( $settings[ $id_base ] ) ) {
  624. $settings[ $id_base ] = get_option( 'widget_' . $id_base );
  625. }
  626. // New multi widget (WP_Widget)
  627. if ( ! is_null( $widget_number ) ) {
  628. if ( isset( $settings[ $id_base ][ $widget_number ] ) && false === self::filter_widget( $settings[ $id_base ][ $widget_number ] ) ) {
  629. unset( $widget_areas[ $widget_area ][ $position ] );
  630. }
  631. }
  632. // Old single widget
  633. elseif ( ! empty( $settings[ $id_base ] ) && false === self::filter_widget( $settings[ $id_base ] ) ) {
  634. unset( $widget_areas[ $widget_area ][ $position ] );
  635. }
  636. }
  637. }
  638. return $widget_areas;
  639. }
  640. public static function template_redirect() {
  641. self::$passed_template_redirect = true;
  642. }
  643. /**
  644. * Generates a condition key based on the rule array
  645. *
  646. * @param array $rule
  647. * @return string key used to retrieve the condition.
  648. */
  649. static function generate_condition_key( $rule ) {
  650. if ( isset( $rule['has_children'] ) ) {
  651. return $rule['major'] . ':' . $rule['minor'] . ':' . $rule['has_children'];
  652. }
  653. return $rule['major'] . ':' . $rule['minor'];
  654. }
  655. /**
  656. * Determine whether the widget should be displayed based on conditions set by the user.
  657. *
  658. * @param array $instance The widget settings.
  659. * @return array Settings to display or bool false to hide.
  660. */
  661. public static function filter_widget( $instance ) {
  662. // Don't filter widgets from the REST API when it's called via the widgets admin page - otherwise they could get
  663. // filtered out and become impossible to edit.
  664. if ( strpos( wp_get_raw_referer(), '/wp-admin/widgets.php' ) && false !== strpos( $_SERVER['REQUEST_URI'], '/wp-json/' ) ) {
  665. return $instance;
  666. }
  667. // WordPress.com specific check - here, referer ends in /rest-proxy/ and doesn't tell us what's requesting.
  668. $current_url = ! empty( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
  669. $nonce = ! empty( $_REQUEST['_gutenberg_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_gutenberg_nonce'] ) ) : '';
  670. $context = ! empty( $_REQUEST['context'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['context'] ) ) : '';
  671. if ( wp_verify_nonce( $nonce, 'gutenberg_request' ) &&
  672. 1 === preg_match( '~^/wp/v2/sites/\d+/(sidebars|widgets)~', $current_url ) && 'edit' === $context ) {
  673. return $instance;
  674. }
  675. if ( ! empty( $instance['conditions']['rules'] ) ) {
  676. // Legacy widgets: visibility found.
  677. if ( self::filter_widget_check_conditions( $instance['conditions'] ) ) {
  678. return $instance;
  679. }
  680. return false;
  681. } elseif ( ! empty( $instance['content'] ) && has_blocks( $instance['content'] ) ) {
  682. // Block-Based widgets: We have gutenberg blocks that could have the 'conditions' attribute.
  683. $blocks = parse_blocks( $instance['content'] );
  684. if ( empty( $blocks[0]['attrs']['conditions']['rules'] ) ) {
  685. // No Rules: Display widget.
  686. return $instance;
  687. }
  688. if ( self::filter_widget_check_conditions( $blocks[0]['attrs']['conditions'] ) ) {
  689. // Rules passed checks: Display widget.
  690. return $instance;
  691. }
  692. // Rules failed checks: Hide widget.
  693. return false;
  694. }
  695. // No visibility found.
  696. return $instance;
  697. }
  698. /**
  699. * Determine whether the widget should be displayed based on conditions set by the user.
  700. *
  701. * @param array $conditions Visibility Conditions: An array with keys 'rules', 'action', and 'match_all'.
  702. * @return bool If the widget controlled by these conditions should show.
  703. */
  704. public static function filter_widget_check_conditions( $conditions ) {
  705. global $wp_query;
  706. // Store the results of all in-page condition lookups so that multiple widgets with
  707. // the same visibility conditions don't result in duplicate DB queries.
  708. static $condition_result_cache = array();
  709. $condition_result = false;
  710. foreach ( $conditions['rules'] as $rule ) {
  711. $condition_result = false;
  712. $condition_key = self::generate_condition_key( $rule );
  713. if ( isset( $condition_result_cache[ $condition_key ] ) ) {
  714. $condition_result = $condition_result_cache[ $condition_key ];
  715. } else {
  716. switch ( $rule['major'] ) {
  717. case 'date':
  718. switch ( $rule['minor'] ) {
  719. case '':
  720. $condition_result = is_date();
  721. break;
  722. case 'month':
  723. $condition_result = is_month();
  724. break;
  725. case 'day':
  726. $condition_result = is_day();
  727. break;
  728. case 'year':
  729. $condition_result = is_year();
  730. break;
  731. }
  732. break;
  733. case 'page':
  734. // Previously hardcoded post type options.
  735. if ( 'post' == $rule['minor'] ) {
  736. $rule['minor'] = 'post_type-post';
  737. } elseif ( ! $rule['minor'] ) {
  738. $rule['minor'] = 'post_type-page';
  739. }
  740. switch ( $rule['minor'] ) {
  741. case '404':
  742. $condition_result = is_404();
  743. break;
  744. case 'search':
  745. $condition_result = is_search();
  746. break;
  747. case 'archive':
  748. $condition_result = is_archive();
  749. break;
  750. case 'posts':
  751. $condition_result = $wp_query->is_posts_page;
  752. break;
  753. case 'home':
  754. $condition_result = is_home();
  755. break;
  756. case 'front':
  757. if ( current_theme_supports( 'infinite-scroll' ) ) {
  758. $condition_result = is_front_page();
  759. } else {
  760. $condition_result = is_front_page() && ! is_paged();
  761. }
  762. break;
  763. default:
  764. if ( substr( $rule['minor'], 0, 10 ) == 'post_type-' ) {
  765. $condition_result = is_singular( substr( $rule['minor'], 10 ) );
  766. } elseif ( substr( $rule['minor'], 0, 18 ) == 'post_type_archive-' ) {
  767. $condition_result = is_post_type_archive( substr( $rule['minor'], 18 ) );
  768. } elseif ( $rule['minor'] == get_option( 'page_for_posts' ) ) {
  769. // If $rule['minor'] is a page ID which is also the posts page
  770. $condition_result = $wp_query->is_posts_page;
  771. } else {
  772. // $rule['minor'] is a page ID
  773. $condition_result = is_page() && ( $rule['minor'] == get_the_ID() );
  774. // Check if $rule['minor'] is parent of page ID
  775. if ( ! $condition_result && isset( $rule['has_children'] ) && $rule['has_children'] ) {
  776. $condition_result = wp_get_post_parent_id( get_the_ID() ) == $rule['minor'];
  777. }
  778. }
  779. break;
  780. }
  781. break;
  782. case 'tag':
  783. // All tag pages.
  784. if ( ! $rule['minor'] ) {
  785. if ( is_tag() ) {
  786. $condition_result = true;
  787. } elseif ( is_singular() ) {
  788. if ( in_array( 'post_tag', get_post_taxonomies() ) ) {
  789. $condition_result = true;
  790. }
  791. }
  792. break;
  793. }
  794. // All pages with the specified tag term.
  795. if ( is_tag( $rule['minor'] ) ) {
  796. $condition_result = true;
  797. } elseif ( is_singular() && has_term( $rule['minor'], 'post_tag' ) ) {
  798. $condition_result = true;
  799. }
  800. break;
  801. case 'category':
  802. // All category pages.
  803. if ( ! $rule['minor'] ) {
  804. if ( is_category() ) {
  805. $condition_result = true;
  806. } elseif ( is_singular() ) {
  807. if ( in_array( 'category', get_post_taxonomies() ) ) {
  808. $condition_result = true;
  809. }
  810. }
  811. break;
  812. }
  813. // All pages with the specified category term.
  814. if ( is_category( $rule['minor'] ) ) {
  815. $condition_result = true;
  816. } elseif ( is_singular() && has_term( $rule['minor'], 'category' ) ) {
  817. $condition_result = true;
  818. }
  819. break;
  820. case 'loggedin':
  821. $condition_result = is_user_logged_in();
  822. if ( 'loggedin' !== $rule['minor'] ) {
  823. $condition_result = ! $condition_result;
  824. }
  825. break;
  826. case 'author':
  827. $post = get_post();
  828. if ( ! $rule['minor'] && is_author() ) {
  829. $condition_result = true;
  830. } elseif ( $rule['minor'] && is_author( $rule['minor'] ) ) {
  831. $condition_result = true;
  832. } elseif ( is_singular() && $rule['minor'] && $rule['minor'] == $post->post_author ) {
  833. $condition_result = true;
  834. }
  835. break;
  836. case 'role':
  837. if ( is_user_logged_in() ) {
  838. $current_user = wp_get_current_user();
  839. $user_roles = $current_user->roles;
  840. if ( in_array( $rule['minor'], $user_roles ) ) {
  841. $condition_result = true;
  842. } else {
  843. $condition_result = false;
  844. }
  845. } else {
  846. $condition_result = false;
  847. }
  848. break;
  849. case 'post_type':
  850. if ( substr( $rule['minor'], 0, 10 ) == 'post_type-' ) {
  851. $condition_result = is_singular( substr( $rule['minor'], 10 ) );
  852. } elseif ( substr( $rule['minor'], 0, 18 ) == 'post_type_archive-' ) {
  853. $condition_result = is_post_type_archive( substr( $rule['minor'], 18 ) );
  854. }
  855. break;
  856. case 'taxonomy':
  857. // All taxonomy pages.
  858. if ( ! $rule['minor'] ) {
  859. if ( is_archive() ) {
  860. if ( is_tag() || is_category() || is_tax() ) {
  861. $condition_result = true;
  862. }
  863. } elseif ( is_singular() ) {
  864. $post_taxonomies = get_post_taxonomies();
  865. $condition_result = ! empty( $post_taxonomies );
  866. }
  867. break;
  868. }
  869. // Specified taxonomy page.
  870. $term = explode( '_tax_', $rule['minor'] ); // $term[0] = taxonomy name; $term[1] = term id
  871. if ( isset( $term[0] ) && isset( $term[1] ) ) {
  872. $term[1] = self::maybe_get_split_term( $term[1], $term[0] );
  873. }
  874. // All pages of the specified taxonomy.
  875. if ( ! isset( $term[1] ) || ! $term[1] ) {
  876. if ( is_tax( $term[0] ) ) {
  877. $condition_result = true;
  878. } elseif ( is_singular() ) {
  879. if ( in_array( $term[0], get_post_taxonomies() ) ) {
  880. $condition_result = true;
  881. }
  882. }
  883. break;
  884. }
  885. // All pages with the specified taxonomy term.
  886. if ( is_tax( $term[0], $term[1] ) ) {
  887. $condition_result = true;
  888. } elseif ( is_singular() && has_term( $term[1], $term[0] ) ) {
  889. $condition_result = true;
  890. }
  891. break;
  892. }
  893. if ( $condition_result || self::$passed_template_redirect ) {
  894. // Some of the conditions will return false when checked before the template_redirect
  895. // action has been called, like is_page(). Only store positive lookup results, which
  896. // won't be false positives, before template_redirect, and everything after.
  897. $condition_result_cache[ $condition_key ] = $condition_result;
  898. }
  899. }
  900. if (
  901. isset( $conditions['match_all'] )
  902. && '1' === $conditions['match_all']
  903. && ! $condition_result
  904. ) {
  905. // In case the match_all flag was set we quit on first failed condition
  906. break;
  907. } elseif (
  908. (
  909. empty( $conditions['match_all'] )
  910. || '1' !== $conditions['match_all']
  911. )
  912. && $condition_result
  913. ) {
  914. // Only quit on first condition if the match_all flag was not set
  915. break;
  916. }
  917. }
  918. if (
  919. (
  920. 'show' === $conditions['action']
  921. && ! $condition_result
  922. ) || (
  923. 'hide' === $conditions['action']
  924. && $condition_result
  925. )
  926. ) {
  927. return false;
  928. }
  929. return true;
  930. }
  931. public static function strcasecmp_name( $a, $b ) {
  932. return strcasecmp( $a->name, $b->name );
  933. }
  934. public static function maybe_get_split_term( $old_term_id = '', $taxonomy = '' ) {
  935. $term_id = $old_term_id;
  936. if ( 'tag' == $taxonomy ) {
  937. $taxonomy = 'post_tag';
  938. }
  939. if ( $new_term_id = wp_get_split_term( $old_term_id, $taxonomy ) ) {
  940. $term_id = $new_term_id;
  941. }
  942. return $term_id;
  943. }
  944. /**
  945. * Upgrade routine to go through all widgets and move the Post Type
  946. * setting to its newer location.
  947. *
  948. * @since 4.7.1
  949. */
  950. static function migrate_post_type_rules() {
  951. global $wp_registered_widgets;
  952. $sidebars_widgets = get_option( 'sidebars_widgets' );
  953. // Going through all sidebars and through inactive and orphaned widgets
  954. foreach ( $sidebars_widgets as $s => $sidebar ) {
  955. if ( ! is_array( $sidebar ) ) {
  956. continue;
  957. }
  958. foreach ( $sidebar as $w => $widget ) {
  959. // $widget is the id of the widget
  960. if ( empty( $wp_registered_widgets[ $widget ] ) ) {
  961. continue;
  962. }
  963. $opts = $wp_registered_widgets[ $widget ];
  964. $instances = get_option( $opts['callback'][0]->option_name );
  965. // Going through each instance of the widget
  966. foreach ( $instances as $number => $instance ) {
  967. if (
  968. ! is_array( $instance ) ||
  969. empty( $instance['conditions'] ) ||
  970. empty( $instance['conditions']['rules'] )
  971. ) {
  972. continue;
  973. }
  974. // Going through all visibility rules
  975. foreach ( $instance['conditions']['rules'] as $index => $rule ) {
  976. // We only need Post Type rules
  977. if ( 'post_type' !== $rule['major'] ) {
  978. continue;
  979. }
  980. $rule_type = false;
  981. // Post type or type archive rule
  982. if ( 0 === strpos( $rule['minor'], 'post_type_archive' ) ) {
  983. $rule_type = 'post_type_archive';
  984. } elseif ( 0 === strpos( $rule['minor'], 'post_type' ) ) {
  985. $rule_type = 'post_type';
  986. }
  987. if ( $rule_type ) {
  988. $post_type = substr( $rule['minor'], strlen( $rule_type ) + 1 );
  989. $rule['minor'] = $rule_type . '-' . $post_type;
  990. $rule['major'] = 'page';
  991. $instances[ $number ]['conditions']['rules'][ $index ] = $rule;
  992. }
  993. }
  994. }
  995. update_option( $opts['callback'][0]->option_name, $instances );
  996. }
  997. }
  998. }
  999. }
  1000. add_action( 'init', array( 'Jetpack_Widget_Conditions', 'init' ) );
  1001. // Add the 'conditions' attribute to server side rendered blocks
  1002. // 'init' happens too late to hook on block registration.
  1003. global $pagenow;
  1004. $current_url = ! empty( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
  1005. if ( is_customize_preview() || 'widgets.php' === $pagenow || ( false !== strpos( $current_url, '/wp-json/wp/v2/block-renderer' ) ) ) {
  1006. Jetpack_Widget_Conditions::add_block_attributes_filter();
  1007. }