class-wp-customize-partial.php 8.6 KB

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