class-wp-customize-partial.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * Customize API: WP_Customize_Partial class
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 4.5.0
  8. */
  9. /**
  10. * Core Customizer class for implementing selective refresh partials.
  11. *
  12. * Representation of a rendered region in the previewed page that gets
  13. * selectively refreshed when an associated setting is changed.
  14. * This class is analogous of WP_Customize_Control.
  15. *
  16. * @since 4.5.0
  17. */
  18. #[AllowDynamicProperties]
  19. class WP_Customize_Partial {
  20. /**
  21. * Component.
  22. *
  23. * @since 4.5.0
  24. * @var WP_Customize_Selective_Refresh
  25. */
  26. public $component;
  27. /**
  28. * Unique identifier for the partial.
  29. *
  30. * If the partial is used to display a single setting, this would generally
  31. * be the same as the associated setting's ID.
  32. *
  33. * @since 4.5.0
  34. * @var string
  35. */
  36. public $id;
  37. /**
  38. * Parsed ID.
  39. *
  40. * @since 4.5.0
  41. * @var array {
  42. * @type string $base ID base.
  43. * @type array $keys Keys for multidimensional.
  44. * }
  45. */
  46. protected $id_data = array();
  47. /**
  48. * Type of this partial.
  49. *
  50. * @since 4.5.0
  51. * @var string
  52. */
  53. public $type = 'default';
  54. /**
  55. * The jQuery selector to find the container element for the partial.
  56. *
  57. * @since 4.5.0
  58. * @var string
  59. */
  60. public $selector;
  61. /**
  62. * IDs for settings tied to the partial.
  63. *
  64. * @since 4.5.0
  65. * @var string[]
  66. */
  67. public $settings;
  68. /**
  69. * The ID for the setting that this partial is primarily responsible for rendering.
  70. *
  71. * If not supplied, it will default to the ID of the first setting.
  72. *
  73. * @since 4.5.0
  74. * @var string
  75. */
  76. public $primary_setting;
  77. /**
  78. * Capability required to edit this partial.
  79. *
  80. * Normally this is empty and the capability is derived from the capabilities
  81. * of the associated `$settings`.
  82. *
  83. * @since 4.5.0
  84. * @var string
  85. */
  86. public $capability;
  87. /**
  88. * Render callback.
  89. *
  90. * @since 4.5.0
  91. *
  92. * @see WP_Customize_Partial::render()
  93. * @var callable Callback is called with one argument, the instance of
  94. * WP_Customize_Partial. The callback can either echo the
  95. * partial or return the partial as a string, or return false if error.
  96. */
  97. public $render_callback;
  98. /**
  99. * Whether the container element is included in the partial, or if only the contents are rendered.
  100. *
  101. * @since 4.5.0
  102. * @var bool
  103. */
  104. public $container_inclusive = false;
  105. /**
  106. * Whether to refresh the entire preview in case a partial cannot be refreshed.
  107. *
  108. * A partial render is considered a failure if the render_callback returns false.
  109. *
  110. * @since 4.5.0
  111. * @var bool
  112. */
  113. public $fallback_refresh = true;
  114. /**
  115. * Constructor.
  116. *
  117. * Supplied `$args` override class property defaults.
  118. *
  119. * If `$args['settings']` is not defined, use the $id as the setting ID.
  120. *
  121. * @since 4.5.0
  122. *
  123. * @param WP_Customize_Selective_Refresh $component Customize Partial Refresh plugin instance.
  124. * @param string $id Control ID.
  125. * @param array $args {
  126. * Optional. Array of properties for the new Partials object. Default empty array.
  127. *
  128. * @type string $type Type of the partial to be created.
  129. * @type string $selector The jQuery selector to find the container element for the partial, that is,
  130. * a partial's placement.
  131. * @type string[] $settings IDs for settings tied to the partial. If undefined, `$id` will be used.
  132. * @type string $primary_setting The ID for the setting that this partial is primarily responsible for
  133. * rendering. If not supplied, it will default to the ID of the first setting.
  134. * @type string $capability Capability required to edit this partial.
  135. * Normally this is empty and the capability is derived from the capabilities
  136. * of the associated `$settings`.
  137. * @type callable $render_callback Render callback.
  138. * Callback is called with one argument, the instance of WP_Customize_Partial.
  139. * The callback can either echo the partial or return the partial as a string,
  140. * or return false if error.
  141. * @type bool $container_inclusive Whether the container element is included in the partial, or if only
  142. * the contents are rendered.
  143. * @type bool $fallback_refresh Whether to refresh the entire preview in case a partial cannot be refreshed.
  144. * A partial render is considered a failure if the render_callback returns
  145. * false.
  146. * }
  147. */
  148. public function __construct( WP_Customize_Selective_Refresh $component, $id, $args = array() ) {
  149. $keys = array_keys( get_object_vars( $this ) );
  150. foreach ( $keys as $key ) {
  151. if ( isset( $args[ $key ] ) ) {
  152. $this->$key = $args[ $key ];
  153. }
  154. }
  155. $this->component = $component;
  156. $this->id = $id;
  157. $this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
  158. $this->id_data['base'] = array_shift( $this->id_data['keys'] );
  159. if ( empty( $this->render_callback ) ) {
  160. $this->render_callback = array( $this, 'render_callback' );
  161. }
  162. // Process settings.
  163. if ( ! isset( $this->settings ) ) {
  164. $this->settings = array( $id );
  165. } elseif ( is_string( $this->settings ) ) {
  166. $this->settings = array( $this->settings );
  167. }
  168. if ( empty( $this->primary_setting ) ) {
  169. $this->primary_setting = current( $this->settings );
  170. }
  171. }
  172. /**
  173. * Retrieves parsed ID data for multidimensional setting.
  174. *
  175. * @since 4.5.0
  176. *
  177. * @return array {
  178. * ID data for multidimensional partial.
  179. *
  180. * @type string $base ID base.
  181. * @type array $keys Keys for multidimensional array.
  182. * }
  183. */
  184. final public function id_data() {
  185. return $this->id_data;
  186. }
  187. /**
  188. * Renders the template partial involving the associated settings.
  189. *
  190. * @since 4.5.0
  191. *
  192. * @param array $container_context Optional. Array of context data associated with the target container (placement).
  193. * Default empty array.
  194. * @return string|array|false The rendered partial as a string, raw data array (for client-side JS template),
  195. * or false if no render applied.
  196. */
  197. final public function render( $container_context = array() ) {
  198. $partial = $this;
  199. $rendered = false;
  200. if ( ! empty( $this->render_callback ) ) {
  201. ob_start();
  202. $return_render = call_user_func( $this->render_callback, $this, $container_context );
  203. $ob_render = ob_get_clean();
  204. if ( null !== $return_render && '' !== $ob_render ) {
  205. _doing_it_wrong( __FUNCTION__, __( 'Partial render must echo the content or return the content string (or array), but not both.' ), '4.5.0' );
  206. }
  207. /*
  208. * Note that the string return takes precedence because the $ob_render may just\
  209. * include PHP warnings or notices.
  210. */
  211. $rendered = null !== $return_render ? $return_render : $ob_render;
  212. }
  213. /**
  214. * Filters partial rendering.
  215. *
  216. * @since 4.5.0
  217. *
  218. * @param string|array|false $rendered The partial value. Default false.
  219. * @param WP_Customize_Partial $partial WP_Customize_Setting instance.
  220. * @param array $container_context Optional array of context data associated with
  221. * the target container.
  222. */
  223. $rendered = apply_filters( 'customize_partial_render', $rendered, $partial, $container_context );
  224. /**
  225. * Filters partial rendering for a specific partial.
  226. *
  227. * The dynamic portion of the hook name, `$partial->ID` refers to the partial ID.
  228. *
  229. * @since 4.5.0
  230. *
  231. * @param string|array|false $rendered The partial value. Default false.
  232. * @param WP_Customize_Partial $partial WP_Customize_Setting instance.
  233. * @param array $container_context Optional array of context data associated with
  234. * the target container.
  235. */
  236. $rendered = apply_filters( "customize_partial_render_{$partial->id}", $rendered, $partial, $container_context );
  237. return $rendered;
  238. }
  239. /**
  240. * Default callback used when invoking WP_Customize_Control::render().
  241. *
  242. * Note that this method may echo the partial *or* return the partial as
  243. * a string or array, but not both. Output buffering is performed when this
  244. * is called. Subclasses can override this with their specific logic, or they
  245. * may provide an 'render_callback' argument to the constructor.
  246. *
  247. * This method may return an HTML string for straight DOM injection, or it
  248. * may return an array for supporting Partial JS subclasses to render by
  249. * applying to client-side templating.
  250. *
  251. * @since 4.5.0
  252. *
  253. * @param WP_Customize_Partial $partial Partial.
  254. * @param array $context Context.
  255. * @return string|array|false
  256. */
  257. public function render_callback( WP_Customize_Partial $partial, $context = array() ) {
  258. unset( $partial, $context );
  259. return false;
  260. }
  261. /**
  262. * Retrieves the data to export to the client via JSON.
  263. *
  264. * @since 4.5.0
  265. *
  266. * @return array Array of parameters passed to the JavaScript.
  267. */
  268. public function json() {
  269. $exports = array(
  270. 'settings' => $this->settings,
  271. 'primarySetting' => $this->primary_setting,
  272. 'selector' => $this->selector,
  273. 'type' => $this->type,
  274. 'fallbackRefresh' => $this->fallback_refresh,
  275. 'containerInclusive' => $this->container_inclusive,
  276. );
  277. return $exports;
  278. }
  279. /**
  280. * Checks if the user can refresh this partial.
  281. *
  282. * Returns false if the user cannot manipulate one of the associated settings,
  283. * or if one of the associated settings does not exist.
  284. *
  285. * @since 4.5.0
  286. *
  287. * @return bool False if user can't edit one of the related settings,
  288. * or if one of the associated settings does not exist.
  289. */
  290. final public function check_capabilities() {
  291. if ( ! empty( $this->capability ) && ! current_user_can( $this->capability ) ) {
  292. return false;
  293. }
  294. foreach ( $this->settings as $setting_id ) {
  295. $setting = $this->component->manager->get_setting( $setting_id );
  296. if ( ! $setting || ! $setting->check_capabilities() ) {
  297. return false;
  298. }
  299. }
  300. return true;
  301. }
  302. }