Keine Beschreibung

class-wp-customize-partial.php 10KB

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