class-wp-customize-partial.php 9.0 KB

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