Brak opisu

class-wp-customize-selective-refresh.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. /**
  3. * Customize API: WP_Customize_Selective_Refresh class
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 4.5.0
  8. */
  9. /**
  10. * Core Customizer class for implementing selective refresh.
  11. *
  12. * @since 4.5.0
  13. */
  14. final class WP_Customize_Selective_Refresh {
  15. /**
  16. * Query var used in requests to render partials.
  17. *
  18. * @since 4.5.0
  19. */
  20. const RENDER_QUERY_VAR = 'wp_customize_render_partials';
  21. /**
  22. * Customize manager.
  23. *
  24. * @since 4.5.0
  25. * @var WP_Customize_Manager
  26. */
  27. public $manager;
  28. /**
  29. * Registered instances of WP_Customize_Partial.
  30. *
  31. * @since 4.5.0
  32. * @var WP_Customize_Partial[]
  33. */
  34. protected $partials = array();
  35. /**
  36. * Log of errors triggered when partials are rendered.
  37. *
  38. * @since 4.5.0
  39. * @var array
  40. */
  41. protected $triggered_errors = array();
  42. /**
  43. * Keep track of the current partial being rendered.
  44. *
  45. * @since 4.5.0
  46. * @var string|null
  47. */
  48. protected $current_partial_id;
  49. /**
  50. * Plugin bootstrap for Partial Refresh functionality.
  51. *
  52. * @since 4.5.0
  53. *
  54. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  55. */
  56. public function __construct( WP_Customize_Manager $manager ) {
  57. $this->manager = $manager;
  58. require_once ABSPATH . WPINC . '/customize/class-wp-customize-partial.php';
  59. add_action( 'customize_preview_init', array( $this, 'init_preview' ) );
  60. }
  61. /**
  62. * Retrieves the registered partials.
  63. *
  64. * @since 4.5.0
  65. *
  66. * @return array Partials.
  67. */
  68. public function partials() {
  69. return $this->partials;
  70. }
  71. /**
  72. * Adds a partial.
  73. *
  74. * @since 4.5.0
  75. *
  76. * @see WP_Customize_Partial::__construct()
  77. *
  78. * @param WP_Customize_Partial|string $id Customize Partial object, or Partial ID.
  79. * @param array $args Optional. Array of properties for the new Partials object.
  80. * See WP_Customize_Partial::__construct() for information
  81. * on accepted arguments. Default empty array.
  82. * @return WP_Customize_Partial The instance of the partial that was added.
  83. */
  84. public function add_partial( $id, $args = array() ) {
  85. if ( $id instanceof WP_Customize_Partial ) {
  86. $partial = $id;
  87. } else {
  88. $class = 'WP_Customize_Partial';
  89. /** This filter is documented in wp-includes/customize/class-wp-customize-selective-refresh.php */
  90. $args = apply_filters( 'customize_dynamic_partial_args', $args, $id );
  91. /** This filter is documented in wp-includes/customize/class-wp-customize-selective-refresh.php */
  92. $class = apply_filters( 'customize_dynamic_partial_class', $class, $id, $args );
  93. $partial = new $class( $this, $id, $args );
  94. }
  95. $this->partials[ $partial->id ] = $partial;
  96. return $partial;
  97. }
  98. /**
  99. * Retrieves a partial.
  100. *
  101. * @since 4.5.0
  102. *
  103. * @param string $id Customize Partial ID.
  104. * @return WP_Customize_Partial|null The partial, if set. Otherwise null.
  105. */
  106. public function get_partial( $id ) {
  107. if ( isset( $this->partials[ $id ] ) ) {
  108. return $this->partials[ $id ];
  109. } else {
  110. return null;
  111. }
  112. }
  113. /**
  114. * Removes a partial.
  115. *
  116. * @since 4.5.0
  117. *
  118. * @param string $id Customize Partial ID.
  119. */
  120. public function remove_partial( $id ) {
  121. unset( $this->partials[ $id ] );
  122. }
  123. /**
  124. * Initializes the Customizer preview.
  125. *
  126. * @since 4.5.0
  127. */
  128. public function init_preview() {
  129. add_action( 'template_redirect', array( $this, 'handle_render_partials_request' ) );
  130. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
  131. }
  132. /**
  133. * Enqueues preview scripts.
  134. *
  135. * @since 4.5.0
  136. */
  137. public function enqueue_preview_scripts() {
  138. wp_enqueue_script( 'customize-selective-refresh' );
  139. add_action( 'wp_footer', array( $this, 'export_preview_data' ), 1000 );
  140. }
  141. /**
  142. * Exports data in preview after it has finished rendering so that partials can be added at runtime.
  143. *
  144. * @since 4.5.0
  145. */
  146. public function export_preview_data() {
  147. $partials = array();
  148. foreach ( $this->partials() as $partial ) {
  149. if ( $partial->check_capabilities() ) {
  150. $partials[ $partial->id ] = $partial->json();
  151. }
  152. }
  153. $switched_locale = switch_to_locale( get_user_locale() );
  154. $l10n = array(
  155. 'shiftClickToEdit' => __( 'Shift-click to edit this element.' ),
  156. 'clickEditMenu' => __( 'Click to edit this menu.' ),
  157. 'clickEditWidget' => __( 'Click to edit this widget.' ),
  158. 'clickEditTitle' => __( 'Click to edit the site title.' ),
  159. 'clickEditMisc' => __( 'Click to edit this element.' ),
  160. /* translators: %s: document.write() */
  161. 'badDocumentWrite' => sprintf( __( '%s is forbidden' ), 'document.write()' ),
  162. );
  163. if ( $switched_locale ) {
  164. restore_previous_locale();
  165. }
  166. $exports = array(
  167. 'partials' => $partials,
  168. 'renderQueryVar' => self::RENDER_QUERY_VAR,
  169. 'l10n' => $l10n,
  170. );
  171. // Export data to JS.
  172. printf( '<script>var _customizePartialRefreshExports = %s;</script>', wp_json_encode( $exports ) );
  173. }
  174. /**
  175. * Registers dynamically-created partials.
  176. *
  177. * @since 4.5.0
  178. *
  179. * @see WP_Customize_Manager::add_dynamic_settings()
  180. *
  181. * @param string[] $partial_ids Array of the partial IDs to add.
  182. * @return WP_Customize_Partial[] Array of added WP_Customize_Partial instances.
  183. */
  184. public function add_dynamic_partials( $partial_ids ) {
  185. $new_partials = array();
  186. foreach ( $partial_ids as $partial_id ) {
  187. // Skip partials already created.
  188. $partial = $this->get_partial( $partial_id );
  189. if ( $partial ) {
  190. continue;
  191. }
  192. $partial_args = false;
  193. $partial_class = 'WP_Customize_Partial';
  194. /**
  195. * Filters a dynamic partial's constructor arguments.
  196. *
  197. * For a dynamic partial to be registered, this filter must be employed
  198. * to override the default false value with an array of args to pass to
  199. * the WP_Customize_Partial constructor.
  200. *
  201. * @since 4.5.0
  202. *
  203. * @param false|array $partial_args The arguments to the WP_Customize_Partial constructor.
  204. * @param string $partial_id ID for dynamic partial.
  205. */
  206. $partial_args = apply_filters( 'customize_dynamic_partial_args', $partial_args, $partial_id );
  207. if ( false === $partial_args ) {
  208. continue;
  209. }
  210. /**
  211. * Filters the class used to construct partials.
  212. *
  213. * Allow non-statically created partials to be constructed with custom WP_Customize_Partial subclass.
  214. *
  215. * @since 4.5.0
  216. *
  217. * @param string $partial_class WP_Customize_Partial or a subclass.
  218. * @param string $partial_id ID for dynamic partial.
  219. * @param array $partial_args The arguments to the WP_Customize_Partial constructor.
  220. */
  221. $partial_class = apply_filters( 'customize_dynamic_partial_class', $partial_class, $partial_id, $partial_args );
  222. $partial = new $partial_class( $this, $partial_id, $partial_args );
  223. $this->add_partial( $partial );
  224. $new_partials[] = $partial;
  225. }
  226. return $new_partials;
  227. }
  228. /**
  229. * Checks whether the request is for rendering partials.
  230. *
  231. * Note that this will not consider whether the request is authorized or valid,
  232. * just that essentially the route is a match.
  233. *
  234. * @since 4.5.0
  235. *
  236. * @return bool Whether the request is for rendering partials.
  237. */
  238. public function is_render_partials_request() {
  239. return ! empty( $_POST[ self::RENDER_QUERY_VAR ] );
  240. }
  241. /**
  242. * Handles PHP errors triggered during rendering the partials.
  243. *
  244. * These errors will be relayed back to the client in the Ajax response.
  245. *
  246. * @since 4.5.0
  247. *
  248. * @param int $errno Error number.
  249. * @param string $errstr Error string.
  250. * @param string $errfile Error file.
  251. * @param string $errline Error line.
  252. * @return true Always true.
  253. */
  254. public function handle_error( $errno, $errstr, $errfile = null, $errline = null ) {
  255. $this->triggered_errors[] = array(
  256. 'partial' => $this->current_partial_id,
  257. 'error_number' => $errno,
  258. 'error_string' => $errstr,
  259. 'error_file' => $errfile,
  260. 'error_line' => $errline,
  261. );
  262. return true;
  263. }
  264. /**
  265. * Handles the Ajax request to return the rendered partials for the requested placements.
  266. *
  267. * @since 4.5.0
  268. */
  269. public function handle_render_partials_request() {
  270. if ( ! $this->is_render_partials_request() ) {
  271. return;
  272. }
  273. /*
  274. * Note that is_customize_preview() returning true will entail that the
  275. * user passed the 'customize' capability check and the nonce check, since
  276. * WP_Customize_Manager::setup_theme() is where the previewing flag is set.
  277. */
  278. if ( ! is_customize_preview() ) {
  279. wp_send_json_error( 'expected_customize_preview', 403 );
  280. } elseif ( ! isset( $_POST['partials'] ) ) {
  281. wp_send_json_error( 'missing_partials', 400 );
  282. }
  283. // Ensure that doing selective refresh on 404 template doesn't result in fallback rendering behavior (full refreshes).
  284. status_header( 200 );
  285. $partials = json_decode( wp_unslash( $_POST['partials'] ), true );
  286. if ( ! is_array( $partials ) ) {
  287. wp_send_json_error( 'malformed_partials' );
  288. }
  289. $this->add_dynamic_partials( array_keys( $partials ) );
  290. /**
  291. * Fires immediately before partials are rendered.
  292. *
  293. * Plugins may do things like call wp_enqueue_scripts() and gather a list of the scripts
  294. * and styles which may get enqueued in the response.
  295. *
  296. * @since 4.5.0
  297. *
  298. * @param WP_Customize_Selective_Refresh $this Selective refresh component.
  299. * @param array $partials Placements' context data for the partials rendered in the request.
  300. * The array is keyed by partial ID, with each item being an array of
  301. * the placements' context data.
  302. */
  303. do_action( 'customize_render_partials_before', $this, $partials );
  304. set_error_handler( array( $this, 'handle_error' ), error_reporting() );
  305. $contents = array();
  306. foreach ( $partials as $partial_id => $container_contexts ) {
  307. $this->current_partial_id = $partial_id;
  308. if ( ! is_array( $container_contexts ) ) {
  309. wp_send_json_error( 'malformed_container_contexts' );
  310. }
  311. $partial = $this->get_partial( $partial_id );
  312. if ( ! $partial || ! $partial->check_capabilities() ) {
  313. $contents[ $partial_id ] = null;
  314. continue;
  315. }
  316. $contents[ $partial_id ] = array();
  317. // @todo The array should include not only the contents, but also whether the container is included?
  318. if ( empty( $container_contexts ) ) {
  319. // Since there are no container contexts, render just once.
  320. $contents[ $partial_id ][] = $partial->render( null );
  321. } else {
  322. foreach ( $container_contexts as $container_context ) {
  323. $contents[ $partial_id ][] = $partial->render( $container_context );
  324. }
  325. }
  326. }
  327. $this->current_partial_id = null;
  328. restore_error_handler();
  329. /**
  330. * Fires immediately after partials are rendered.
  331. *
  332. * Plugins may do things like call wp_footer() to scrape scripts output and return them
  333. * via the {@see 'customize_render_partials_response'} filter.
  334. *
  335. * @since 4.5.0
  336. *
  337. * @param WP_Customize_Selective_Refresh $this Selective refresh component.
  338. * @param array $partials Placements' context data for the partials rendered in the request.
  339. * The array is keyed by partial ID, with each item being an array of
  340. * the placements' context data.
  341. */
  342. do_action( 'customize_render_partials_after', $this, $partials );
  343. $response = array(
  344. 'contents' => $contents,
  345. );
  346. if ( defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY ) {
  347. $response['errors'] = $this->triggered_errors;
  348. }
  349. $setting_validities = $this->manager->validate_setting_values( $this->manager->unsanitized_post_values() );
  350. $exported_setting_validities = array_map( array( $this->manager, 'prepare_setting_validity_for_js' ), $setting_validities );
  351. $response['setting_validities'] = $exported_setting_validities;
  352. /**
  353. * Filters the response from rendering the partials.
  354. *
  355. * Plugins may use this filter to inject `$scripts` and `$styles`, which are dependencies
  356. * for the partials being rendered. The response data will be available to the client via
  357. * the `render-partials-response` JS event, so the client can then inject the scripts and
  358. * styles into the DOM if they have not already been enqueued there.
  359. *
  360. * If plugins do this, they'll need to take care for any scripts that do `document.write()`
  361. * and make sure that these are not injected, or else to override the function to no-op,
  362. * or else the page will be destroyed.
  363. *
  364. * Plugins should be aware that `$scripts` and `$styles` may eventually be included by
  365. * default in the response.
  366. *
  367. * @since 4.5.0
  368. *
  369. * @param array $response {
  370. * Response.
  371. *
  372. * @type array $contents Associative array mapping a partial ID its corresponding array of contents
  373. * for the containers requested.
  374. * @type array $errors List of errors triggered during rendering of partials, if `WP_DEBUG_DISPLAY`
  375. * is enabled.
  376. * }
  377. * @param WP_Customize_Selective_Refresh $refresh Selective refresh component.
  378. * @param array $partials Placements' context data for the partials rendered in the request.
  379. * The array is keyed by partial ID, with each item being an array of
  380. * the placements' context data.
  381. */
  382. $response = apply_filters( 'customize_render_partials_response', $response, $this, $partials );
  383. wp_send_json_success( $response );
  384. }
  385. }