選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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

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