/** * Register custom endpoint to transform image ID into thumb URL * * @since 0.1 */ public function register_endpoints() { register_rest_route('content-layout-control/v1', '/components/content-block/thumb-url/(?P<id>\\d+)', array('methods' => 'GET', 'callback' => array($this, 'api_get_thumb_url'), 'permission_callback' => array(CLC_Content_Layout_Control(), 'current_user_can'))); register_rest_route('content-layout-control/v1', '/components/content-block/links/(?P<search>.+)', array('methods' => 'GET', 'callback' => array($this, 'api_get_links'), 'permission_callback' => array(CLC_Content_Layout_Control(), 'current_user_can'))); }
/** * Save values as post meta and render post_content * * @since 0.1 */ public function save_to_post_content($value, $setting) { if (!is_array($value)) { return; } if (!current_user_can(CLC_Content_Layout_Control()->capability)) { return; } foreach ($value as $post_id => $components) { // Post exists. Checked in sanitize callback update_post_meta($post_id, 'content_layout', $components); // Render and save as `post_content` $content = empty($components) ? '' : CLC_Content_Layout_Control()->render_layout($components); wp_update_post(array('ID' => $post_id, 'post_content' => $content)); } }
/** * Sanitize component values * * @since 0.1 */ static function sanitize($val) { $output = array(); if (!is_array($val)) { return $output; } $clc = CLC_Content_Layout_Control(); if (empty($clc->components)) { return $output; } foreach ($val as $post_id => $components) { $post_id = absint($post_id); // check if post exists if (!get_post_status($post_id)) { continue; } $output[$post_id] = array(); foreach ($components as $component) { if (!is_array($component) || empty($component['type']) || !isset($clc->components[$component['type']]) || !is_subclass_of($clc->components[$component['type']], 'CLC_Component')) { continue; } $output[$post_id][] = $clc->components[$component['type']]->sanitize($component); } } return $output; }