| 1 |
<?php |
| 2 |
namespace Vimeography\Api; |
| 3 |
|
| 4 |
class Themes extends \WP_REST_Controller |
| 5 |
{ |
| 6 |
public function __construct() |
| 7 |
{ |
| 8 |
add_action('rest_api_init', function () { |
| 9 |
$this->register_routes(); |
| 10 |
}); |
| 11 |
} |
| 12 |
/** |
| 13 |
* Register the routes for the objects of the controller. |
| 14 |
*/ |
| 15 |
public function register_routes() |
| 16 |
{ |
| 17 |
$version = '1'; |
| 18 |
$namespace = 'vimeography/v' . $version; |
| 19 |
$base = 'themes'; |
| 20 |
register_rest_route($namespace, '/' . $base, array( |
| 21 |
array( |
| 22 |
'methods' => \WP_REST_Server::READABLE, |
| 23 |
'callback' => array($this, 'get_items'), |
| 24 |
'permission_callback' => array($this, 'get_items_permissions_check'), |
| 25 |
'args' => array() |
| 26 |
) |
| 27 |
)); |
| 28 |
|
| 29 |
register_rest_route($namespace, '/' . $base . '/schema', array( |
| 30 |
'methods' => \WP_REST_Server::READABLE, |
| 31 |
'callback' => array($this, 'get_public_item_schema') |
| 32 |
)); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get a collection of items |
| 37 |
* |
| 38 |
* @param WP_REST_Request $request Full data about the request. |
| 39 |
* @return WP_Error|WP_REST_Response |
| 40 |
*/ |
| 41 |
public function get_items($request) |
| 42 |
{ |
| 43 |
$themes = \Vimeography::get_instance()->addons->themes; |
| 44 |
$activated_themes = get_option('vimeography_activation_keys'); |
| 45 |
|
| 46 |
$items = array(); |
| 47 |
foreach ($themes as $theme) { |
| 48 |
if (is_array($activated_themes)) { |
| 49 |
foreach ($activated_themes as $activation) { |
| 50 |
if ( |
| 51 |
strtolower($activation->plugin_name) == strtolower($theme['slug']) |
| 52 |
) { |
| 53 |
$theme['is_licensed'] = true; |
| 54 |
} else { |
| 55 |
$theme['is_licensed'] = false; |
| 56 |
} |
| 57 |
} |
| 58 |
} else { |
| 59 |
$theme['is_licensed'] = false; |
| 60 |
} |
| 61 |
|
| 62 |
$theme["settings"] = array(); |
| 63 |
|
| 64 |
if (file_exists($theme['settings_file'])) { |
| 65 |
$this->theme_supports_settings = true; |
| 66 |
include $theme['settings_file']; |
| 67 |
|
| 68 |
/* $settings is defined in the theme settings file */ |
| 69 |
$theme["settings"] = $settings; |
| 70 |
} |
| 71 |
|
| 72 |
$out = array( |
| 73 |
"name" => $theme['name'], |
| 74 |
"description" => $theme['description'], |
| 75 |
"version" => $theme['version'], |
| 76 |
"thumbnail" => $theme['thumbnail'], |
| 77 |
"is_licensed" => $theme['is_licensed'], |
| 78 |
"settings" => $theme["settings"] |
| 79 |
); |
| 80 |
|
| 81 |
$items[] = $out; |
| 82 |
} |
| 83 |
|
| 84 |
return $items; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if a given request has access to get items |
| 89 |
* |
| 90 |
* @param WP_REST_Request $request Full data about the request. |
| 91 |
* @return WP_Error|bool |
| 92 |
*/ |
| 93 |
public function get_items_permissions_check($request) |
| 94 |
{ |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Check if a given request has access to get a specific item |
| 100 |
* |
| 101 |
* @param WP_REST_Request $request Full data about the request. |
| 102 |
* @return WP_Error|bool |
| 103 |
*/ |
| 104 |
public function get_item_permissions_check($request) |
| 105 |
{ |
| 106 |
return $this->get_items_permissions_check($request); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Prepare the item for create or update operation |
| 111 |
* |
| 112 |
* @param WP_REST_Request $request Request object |
| 113 |
* @return WP_Error|object $prepared_item |
| 114 |
*/ |
| 115 |
protected function prepare_item_for_database($request) |
| 116 |
{ |
| 117 |
return array(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Prepare the item for the REST response |
| 122 |
* |
| 123 |
* @param mixed $item WordPress representation of the item. |
| 124 |
* @param WP_REST_Request $request Request object. |
| 125 |
* @return mixed |
| 126 |
*/ |
| 127 |
public function prepare_item_for_response($item, $request) |
| 128 |
{ |
| 129 |
return array(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get the query params for collections |
| 134 |
* |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public function get_collection_params() |
| 138 |
{ |
| 139 |
return array( |
| 140 |
'page' => array( |
| 141 |
'description' => 'Current page of the collection.', |
| 142 |
'type' => 'integer', |
| 143 |
'default' => 1, |
| 144 |
'sanitize_callback' => 'absint' |
| 145 |
), |
| 146 |
'per_page' => array( |
| 147 |
'description' => |
| 148 |
'Maximum number of items to be returned in result set.', |
| 149 |
'type' => 'integer', |
| 150 |
'default' => 10, |
| 151 |
'sanitize_callback' => 'absint' |
| 152 |
), |
| 153 |
'search' => array( |
| 154 |
'description' => 'Limit results to those matching a string.', |
| 155 |
'type' => 'string', |
| 156 |
'sanitize_callback' => 'sanitize_text_field' |
| 157 |
) |
| 158 |
); |
| 159 |
} |
| 160 |
} |
| 161 |
|