├── README.md ├── composer.json ├── index.php ├── plugin.php └── src ├── boot.php └── routes ├── docs.php ├── endpoints.php ├── include ├── mailchimp.php └── support-form.php ├── products.php └── util.php /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Endpoints 4 | 5 | ## Products 6 | * GET `/calderawp_api/v2/products` 7 | * All products 8 | * Arguments: 9 | * `per_page` Default: 10. # of products per page 10 | * `page` Default: 1. Page # 11 | * `soon` Default: false. Show coming soon products only? 12 | * `search` Default: false. Keyword search for products. 13 | * GET `/calderawp_api/v2/products/` 14 | * A specific product by ID 15 | * GET `/calderawp_api/v2/products/cf-add-ons` 16 | * Caldera Forms Add-ons 17 | * Arguments: 18 | * `per_page` Default: 10. # of products per page 19 | * `page` Default: 1. Page # 20 | * `soon` Default: false. Show coming soon products only? 21 | * `slug` Default: false. Allows searching products by slug. 22 | * `category` Default: false. Product category. 23 | * GET `/calderawp_api/v2/products/caldera-search` 24 | * Products in Caldera Search Bundle 25 | * GET `/calderawp_api/v2/products/cf-bundles` 26 | * Caldera Forms Bundles 27 | * GET `/calderawp_api/v2/products/featured` 28 | * Featured products 29 | * Arguments: 30 | * `per_page` Default: 10. # of products per page 31 | * `page` Default: 1. Page # 32 | 33 | ## Documentation 34 | * GET `/calderawp_api/v2/docs` 35 | * All docs 36 | * Arguments: 37 | * `per_page` Default: 10. # of docs per page 38 | * `page` Default: 1. Page # 39 | * `doc_slug` Default: false. Retrieve specific doc by slug. 40 | * `product_slug` Default: false. Retrieve docs for product, by slug 41 | * `product_id` Default: false. Retrieve docs for product, by id 42 | * `search` Default: false. Keyword search for docs. 43 | * GET `/calderawp_api/v2/docs/` 44 | * Specific doc, by ID -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "calderawp/calderawp-api", 3 | "description": "Products API For CalderaWP", 4 | "type": "wordpress-plugin", 5 | "keywords": [ 6 | "wordpress" 7 | ], 8 | "license": "GPL-2.0+", 9 | "authors": [ 10 | { 11 | "name": "David Cramer", 12 | "homepage": "http://cramer.co.za", 13 | "email": "David@CalderaWP.com" 14 | }, 15 | { 16 | "name": "Josh Pollock", 17 | "homepage": "http://joshpress.net", 18 | "email": "Josh@CalderaWP.com" 19 | } 20 | ], 21 | "require": { 22 | "php": ">=5.3.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "calderawp\\calderawp_api\\": "src" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 'Doc', 41 | 'singular_name' => 'Doc', 42 | 'menu_name' => 'Documentations', 43 | 'name_admin_bar' => 'Doc', 44 | 'archives' => 'Item Archives', 45 | 'parent_item_colon' => 'Parent Item:', 46 | 'all_items' => 'All Items', 47 | 'add_new_item' => 'Add New Item', 48 | 'add_new' => 'Add New', 49 | 'new_item' => 'New Item', 50 | 'edit_item' => 'Edit Item', 51 | 'update_item' => 'Update Item', 52 | 'view_item' => 'View Item', 53 | 'search_items' => 'Search Item', 54 | 'not_found' => 'Not found', 55 | 'not_found_in_trash' => 'Not found in Trash', 56 | 'featured_image' => 'Featured Image', 57 | 'set_featured_image' => 'Set featured image', 58 | 'remove_featured_image' => 'Remove featured image', 59 | 'use_featured_image' => 'Use as featured image', 60 | 'insert_into_item' => 'Insert into item', 61 | 'uploaded_to_this_item' => 'Uploaded to this item', 62 | 'items_list' => 'Items list', 63 | 'items_list_navigation' => 'Items list navigation', 64 | 'filter_items_list' => 'Filter items list', 65 | ); 66 | $args = array( 67 | 'label' => 'Doc', 68 | 'description' => 'Post Type Description', 69 | 'labels' => $labels, 70 | 'supports' => array( 'thumbnail', 'excerpt', 'title', 'editor'), 71 | 'taxonomies' => array( 'category', 'post_tag' ), 72 | 'hierarchical' => false, 73 | 'public' => true, 74 | 'show_ui' => true, 75 | 'show_in_menu' => true, 76 | 'menu_position' => 5, 77 | 'show_in_admin_bar' => true, 78 | 'show_in_nav_menus' => true, 79 | 'can_export' => true, 80 | 'has_archive' => true, 81 | 'exclude_from_search' => false, 82 | 'publicly_queryable' => true, 83 | 'capability_type' => 'page', 84 | ); 85 | register_post_type( 'doc', $args ); 86 | 87 | } 88 | 89 | add_action( 'init', 'caldera_theme_register_post_type', 0 ); 90 | 91 | } 92 | 93 | 94 | -------------------------------------------------------------------------------- /src/boot.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link 9 | * @copyright 2015 Josh Pollock 10 | */ 11 | 12 | namespace calderawp\calderawp_api; 13 | 14 | 15 | class boot { 16 | 17 | /** 18 | * Constructor for class 19 | * 20 | * 21 | * @since 0.0.1 22 | * 23 | * @param string $api_namespace 24 | * @param $version 25 | */ 26 | public function __construct( $api_namespace, $version ) { 27 | $this->api_namespace = $api_namespace; 28 | $this->version = $version; 29 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); 30 | } 31 | 32 | /** 33 | * Register our endpoints 34 | * 35 | * @since 0.0.1 36 | */ 37 | public function register_routes() { 38 | $root = $this->api_namespace; 39 | $version = $this->version; 40 | 41 | /** 42 | * Product endpoints 43 | */ 44 | $base = "{$root}/{$version}/products"; 45 | $cb_class = new \calderawp\calderawp_api\routes\products( 'download', $base ); 46 | 47 | /** 48 | * Single product query 49 | */ 50 | register_rest_route( "{$root}/{$version}", '/products' . '/(?P[\d]+)', array( 51 | array( 52 | 'methods' => \WP_REST_Server::READABLE, 53 | 'callback' => array( $cb_class, 'get_item' ), 54 | 'args' => array( 55 | 56 | ), 57 | 'permission_callback' => array( $this, 'permissions_check' ) 58 | ), 59 | ) 60 | ); 61 | 62 | /** 63 | * All products, or query products by slug. 64 | */ 65 | register_rest_route( "{$root}/{$version}", '/products', array( 66 | array( 67 | 'methods' => \WP_REST_Server::READABLE, 68 | 'callback' => array( $cb_class, 'get_items' ), 69 | 'args' => array( 70 | 'per_page' => array( 71 | 'default' => 10, 72 | 'sanitize_callback' => 'absint', 73 | ), 74 | 'page' => array( 75 | 'default' => 1, 76 | 'sanitize_callback' => 'absint', 77 | ), 78 | 'soon' => array( 79 | 'default' => 0, 80 | 'sanitize_callback' => 'absint', 81 | ), 82 | ), 83 | 84 | 'permission_callback' => array( $this, 'permissions_check' ) 85 | ), 86 | ) 87 | 88 | ); 89 | 90 | /** 91 | * Caldera Forms add-ons query 92 | */ 93 | register_rest_route( "{$root}/{$version}", '/products/cf-addons', array( 94 | array( 95 | 'methods' => \WP_REST_Server::READABLE, 96 | 'callback' => array( $cb_class, 'get_cf_addons' ), 97 | 'args' => array( 98 | 'per_page' => array( 99 | 'default' => 10, 100 | 'sanitize_callback' => 'absint', 101 | ), 102 | 'page' => array( 103 | 'default' => 1, 104 | 'sanitize_callback' => 'absint', 105 | ), 106 | 'soon' => array( 107 | 'default' => 0, 108 | 'sanitize_callback' => 'absint', 109 | ), 110 | 'category' => array( 111 | 'default' => 'all-cf-addons', 112 | 'required' => false, 113 | 'sanitize_callback' => 'sanitize_text_field', 114 | ) 115 | 116 | ), 117 | 118 | 'permission_callback' => array( $this, 'permissions_check' ) 119 | ), 120 | ) 121 | 122 | ); 123 | 124 | /** 125 | * Caldera Search add-ons query 126 | */ 127 | register_rest_route( "{$root}/{$version}", '/products/caldera-search', array( 128 | array( 129 | 'methods' => \WP_REST_Server::READABLE, 130 | 'callback' => array( $cb_class, 'get_caldera_search' ), 131 | 'permission_callback' => array( $this, 'permissions_check' ) 132 | ), 133 | ) 134 | 135 | ); 136 | 137 | /** 138 | * Caldera bundles 139 | */ 140 | register_rest_route( "{$root}/{$version}", '/products/cf-bundles', array( 141 | array( 142 | 'methods' => \WP_REST_Server::READABLE, 143 | 'callback' => array( $cb_class, 'get_cf_bundles' ), 144 | 'permission_callback' => array( $this, 'permissions_check' ) 145 | ), 146 | ) 147 | 148 | ); 149 | 150 | /** 151 | * Featured products 152 | */ 153 | register_rest_route( "{$root}/{$version}", '/products/featured', array( 154 | array( 155 | 'methods' => \WP_REST_Server::READABLE, 156 | 'callback' => array( $cb_class, 'get_featured' ), 157 | 'args' => array( 158 | 'per_page' => array( 159 | 'default' => 10, 160 | 'sanitize_callback' => 'absint', 161 | ), 162 | 'page' => array( 163 | 'default' => 1, 164 | 'sanitize_callback' => 'absint', 165 | ), 166 | ), 167 | 168 | 'permission_callback' => array( $this, 'permissions_check' ) 169 | ), 170 | ) 171 | 172 | ); 173 | 174 | /** 175 | * Docs Endpoints 176 | */ 177 | $base = "{$root}/{$version}/docs"; 178 | $cb_class = new \calderawp\calderawp_api\routes\docs( 'doc', $base ); 179 | 180 | /** 181 | * Single product documentation 182 | */ 183 | register_rest_route( "{$root}/{$version}", '/docs' . '/(?P[\d]+)', array( 184 | array( 185 | 'methods' => \WP_REST_Server::READABLE, 186 | 'callback' => array( $cb_class, 'get_item' ), 187 | 'args' => array( 188 | 189 | ), 190 | 'permission_callback' => array( $this, 'permissions_check' ) 191 | ), 192 | ) 193 | ); 194 | 195 | /** 196 | * Docs for all products or a specific product by slug/ID 197 | */ 198 | register_rest_route( "{$root}/{$version}", '/docs', array( 199 | array( 200 | 'methods' => \WP_REST_Server::READABLE, 201 | 'callback' => array( $cb_class, 'get_items' ), 202 | 'args' => array( 203 | 'per_page' => array( 204 | 'default' => 10, 205 | 'sanitize_callback' => 'absint', 206 | ), 207 | 'page' => array( 208 | 'default' => 1, 209 | 'sanitize_callback' => 'absint', 210 | ), 211 | 'doc_slug' => array( 212 | 'default' => 'false', 213 | 'sanitize_callback' => 'sanitize_title', 214 | ), 215 | 'product_slug' => array( 216 | 'default' => 'false', 217 | 'sanitize_callback' => 'sanitize_title', 218 | ), 219 | 'product_id' => array( 220 | 'default' => 0, 221 | 'sanitize_callback' => 'absint', 222 | ), 223 | 224 | ), 225 | 226 | 'permission_callback' => array( $this, 'permissions_check' ) 227 | ), 228 | ) 229 | 230 | ); 231 | 232 | register_rest_route( "{$root}/{$version}", '/docs/important', array( 233 | array( 234 | 'methods' => \WP_REST_Server::READABLE, 235 | 'callback' => array( $cb_class, 'get_important' ), 236 | 'args' => array( 237 | ), 238 | 239 | 'permission_callback' => array( $this, 'permissions_check' ) 240 | ), 241 | ) 242 | 243 | ); 244 | 245 | /** 246 | * Utility endpoints 247 | */ 248 | $cb_class = new \calderawp\calderawp_api\routes\util(); 249 | /** 250 | * Docs for all products or a specific product by slug/ID 251 | */ 252 | register_rest_route( "{$root}/{$version}", '/util', array( 253 | array( 254 | 'methods' => \WP_REST_Server::READABLE, 255 | 'callback' => array( $cb_class, 'get_items' ), 256 | 'args' => array( 257 | 'what' => array( 258 | 'default' => 'all', 259 | 'sanitize_callback' => 'strip_tags', 260 | ) 261 | 262 | ), 263 | 264 | 'permission_callback' => array( $this, 'permissions_check' ) 265 | ), 266 | ) 267 | 268 | ); 269 | 270 | 271 | 272 | } 273 | 274 | /** 275 | * For now, all methods are public. 276 | * 277 | * @since 0.0.1 278 | * 279 | * @param \WP_REST_Request $request Full details about the request. 280 | * 281 | * @return bool Always returns true. 282 | */ 283 | public function permissions_check( $request ) { 284 | return true; 285 | 286 | } 287 | 288 | 289 | } 290 | -------------------------------------------------------------------------------- /src/routes/docs.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link 9 | * @copyright 2015 Josh Pollock 10 | */ 11 | 12 | namespace calderawp\calderawp_api\routes; 13 | 14 | 15 | class docs extends endpoints { 16 | 17 | /** 18 | * Get a single product 19 | * 20 | * @since 0.0.1 21 | * 22 | * @param \WP_REST_Request $request Full details about the request 23 | * 24 | * @return \WP_HTTP_Response 25 | */ 26 | public function get_item( $request) { 27 | $params = $request->get_params(); 28 | $id = $params[ 'id' ]; 29 | if ( 1 < $id ) { 30 | $post = get_post( $id ); 31 | }else{ 32 | $post = null; 33 | } 34 | 35 | 36 | if ( $post ) { 37 | $data = $this->make_data( $post, array() ); 38 | 39 | $response = rest_ensure_response( $data ); 40 | $response->link_header( 'alternate', get_permalink( $id ), array( 'type' => 'text/html' ) ); 41 | }else{ 42 | $response = new \WP_REST_Response( 0, 404, array() ); 43 | } 44 | 45 | $response->set_matched_route( $request->get_route() ); 46 | 47 | return $response; 48 | 49 | } 50 | 51 | /** 52 | * Get docs by product ID, product slug, or all docs. 53 | * 54 | * @since 0.0.1 55 | * 56 | * @param \WP_REST_Request $request 57 | * 58 | * @return \WP_Error|\WP_HTTP_Response 59 | */ 60 | public function get_items( $request ) { 61 | $params = $request->get_params(); 62 | $args = $this->query_args( $params ); 63 | 64 | if ( $params[ 'product_id' ] ) { 65 | $args[ 'meta_key' ] = 'product'; 66 | $args[ 'meta_value' ] = $params[ 'product_id' ]; 67 | }elseif( 'false' !== $params[ 'product_slug' ] ) { 68 | $product = $this->find_product( $params[ 'product_slug' ] ); 69 | if ( is_object( $product ) ) { 70 | $args[ 'meta_key' ] = 'product'; 71 | $args[ 'meta_value' ] = $product->ID; 72 | }else{ 73 | return new \WP_Error( 'calderawp-api-invalid-product-slug' ); 74 | } 75 | }elseif('false' !== $params[ 'doc_slug' ] ) { 76 | $args[ 'name' ] = $params[ 'doc_slug' ]; 77 | } 78 | 79 | return $this->do_query( $request, $args ); 80 | 81 | } 82 | 83 | /** 84 | * Get Important docs 85 | * 86 | * GET /docs/important 87 | * 88 | * @since 1.3.0 89 | * 90 | * @param \WP_REST_Request $request 91 | * 92 | * @return \WP_HTTP_Response 93 | */ 94 | public function get_important( $request){ 95 | $args = array( 96 | 'post__in' => [ 97 | 30815, //getting started 98 | 18879,//magic tags 99 | 33603, //emails 100 | 24978, //auto responders 101 | 53059, //condtional recipients 102 | ] 103 | ); 104 | 105 | return $this->do_query( $request, $args ); 106 | 107 | } 108 | 109 | /** 110 | * Add current post to response data for this route. 111 | * 112 | * @since 0.0.1 113 | * 114 | * @access protected 115 | * 116 | * @param \WP_Post $post Current post object. 117 | * @param array $data Current collection of data 118 | * @param int|string||WP_Post $product Product this is a doc for. If null, the default, it will be queried for by slug or ID. 119 | * 120 | * @return array 121 | */ 122 | protected function make_data( $post, $data, $product = null ) { 123 | if ( ! is_object( $product ) ) { 124 | $product = $this->find_product( $product ); 125 | } 126 | 127 | $data[ $post->ID ] = array( 128 | 'title' => $post->post_title, 129 | 'link' => get_the_permalink( $post->ID ), 130 | 'excerpt' => $post->post_excerpt, 131 | 'content' => $post->post_content, 132 | 'product_name' => null, 133 | 'product_link' => null, 134 | 'slug' => $post->post_name, 135 | ); 136 | 137 | if ( ! is_null( $product ) ) { 138 | $data[ $post->ID ][ 'product_name' ] = $product->post_title; 139 | $data[ $post->ID ][ 'product_link' ] = get_the_permalink( $product->ID ); 140 | } 141 | 142 | return $data; 143 | 144 | } 145 | 146 | /** 147 | * Find product by ID or slug. 148 | * 149 | * @since 0.0.1 150 | * 151 | * @access protected 152 | * 153 | * @param int|string $id_or_slug 154 | */ 155 | protected function find_product( $id_or_slug ) { 156 | if ( is_int( $id_or_slug ) ) { 157 | $args[ 'meta_key' ] = 'product'; 158 | $args[ 'meta_value' ] = $id_or_slug; 159 | }else{ 160 | $args[ 'name' ] = $id_or_slug; 161 | } 162 | 163 | $args[ 'post_type' ] = 'download'; 164 | 165 | $query = new \WP_Query( $args ); 166 | if ( $query->have_posts() ) { 167 | return $query->posts[0]; 168 | } 169 | 170 | 171 | } 172 | 173 | 174 | /** 175 | * @inheritdoc 176 | * @since 1.2.0 177 | */ 178 | protected function query_args($params){ 179 | $params = parent::query_args($params); 180 | unset( $params[ 'meta_key' ] ); 181 | unset( $params[ 'orderby' ] ); 182 | return $params; 183 | 184 | } 185 | 186 | 187 | } 188 | -------------------------------------------------------------------------------- /src/routes/endpoints.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link 9 | * @copyright 2015 Josh Pollock 10 | */ 11 | 12 | namespace calderawp\calderawp_api\routes; 13 | 14 | 15 | abstract class endpoints extends \WP_REST_Posts_Controller { 16 | 17 | /** @var string */ 18 | protected $post_type; 19 | 20 | /** @var string */ 21 | protected $base; 22 | 23 | /** 24 | * @param string $post_type Name of post type this route is for. 25 | * @param string $base Base URL for this API. 26 | */ 27 | public function __construct( $post_type, $base ) { 28 | $this->post_type = $post_type; 29 | $this->base = $base; 30 | } 31 | 32 | /** 33 | * Create WP_Query args from request 34 | * 35 | * @since 0.0.1 36 | * 37 | * @access protected 38 | * 39 | * @param array $params $params from request 40 | * 41 | * @return array 42 | */ 43 | protected function query_args( $params ) { 44 | 45 | $per_page = $params['per_page']; 46 | 47 | $args = array( 48 | 'posts_per_page' => $per_page, 49 | 'paged' => $params[ 'page' ], 50 | 'post_type' => $this->post_type, 51 | 'orderby' => 'meta_value_num', 52 | 'meta_key' => 'order', 53 | ); 54 | 55 | if( isset( $params[ 'search' ] ) ){ 56 | $args[ 's' ] = $params[ 'search' ]; 57 | } 58 | 59 | if ( isset( $params[ 'soon' ] ) && 1 == $params[ 'soon' ] ) { 60 | $args[ 'meta_key' ] = 'edd_coming_soon'; 61 | $args[ 'meta_value' ] = true; 62 | } 63 | 64 | return $args; 65 | 66 | } 67 | 68 | /** 69 | * Query for products and create response 70 | * 71 | * @since 0.0.1 72 | * 73 | * @access protected 74 | * 75 | * @param \WP_REST_Request $request Full details about the request 76 | * @param array $args WP_Query args. 77 | * @param bool $respond. Optional. Whether to create a response, the default, or just return the data. 78 | * 79 | * @return \WP_HTTP_Response 80 | */ 81 | protected function do_query( $request, $args, $respond = true) { 82 | $posts_query = new \WP_Query(); 83 | $args[ 'post_type' ] = $this->post_type; 84 | $args = apply_filters( 'calderawp_api_wp_query_args', $args, $request, get_class( $this ) ); 85 | 86 | $query_result = $posts_query->query( $args ); 87 | $data = array(); 88 | if ( ! empty( $query_result ) ) { 89 | foreach ( $query_result as $post ) { 90 | $data = $this->make_data( $post, $data ); 91 | } 92 | } 93 | 94 | $data = apply_filters( 'calderawp_api_response_data', $data, $args, get_class( $this ) ); 95 | 96 | if ( $respond ) { 97 | return $this->create_response( $request, $args, $data ); 98 | } else { 99 | return $data; 100 | } 101 | 102 | } 103 | 104 | /** 105 | * Create the response. 106 | * 107 | * @since 0.0.1 108 | * 109 | * @access protected 110 | * 111 | * @param \WP_REST_Request $request Full details about the request 112 | * @param array $args WP_Query Args 113 | * @param array $data Raw response data 114 | * 115 | * @return \WP_Error|\WP_HTTP_ResponseInterface|\WP_REST_Response 116 | */ 117 | protected function create_response( $request, $args, $data ) { 118 | $response = rest_ensure_response( $data ); 119 | $count_query = new \WP_Query(); 120 | unset( $args['paged'] ); 121 | 122 | $query_result = $count_query->query( $args ); 123 | $total_posts = $count_query->found_posts; 124 | $response->header( 'X-WP-Total', (int) $total_posts ); 125 | if( 0 == absint( $request[ 'per_page' ] ) ){ 126 | $max_pages = 1; 127 | }else{ 128 | $max_pages = ceil( $total_posts / $request[ 'per_page' ] ); 129 | } 130 | 131 | $response->header( 'X-WP-TotalPages', (int) $max_pages ); 132 | 133 | if ( $request['page'] > 1 ) { 134 | $prev_page = $request['page'] - 1; 135 | if ( $prev_page > $max_pages ) { 136 | $prev_page = $max_pages; 137 | } 138 | $prev_link = add_query_arg( 'page', $prev_page, rest_url( $this->base ) ); 139 | $response->link_header( 'prev', $prev_link ); 140 | } 141 | 142 | if ( $max_pages > $request['page'] ) { 143 | $next_page = $request['page'] + 1; 144 | $next_link = add_query_arg( 'page', $next_page, rest_url( $this->base ) ); 145 | $response->link_header( 'next', $next_link ); 146 | } 147 | 148 | return $response; 149 | 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /src/routes/include/mailchimp.php: -------------------------------------------------------------------------------- 1 | 7 | 8 | 13 |
14 |
15 |
16 |
* indicates required
17 |
18 | 20 | 21 |
22 |
23 | 24 | 25 |
26 |
27 | 28 | 29 |
30 |
31 | 32 | 33 |
34 |
35 |
36 |
37 |
38 |
39 | 40 | 41 | '; 42 | 43 | return $form; 44 | 45 | -------------------------------------------------------------------------------- /src/routes/include/support-form.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link 9 | * @copyright 2015 Josh Pollock 10 | */ 11 | 12 | namespace calderawp\calderawp_api\routes; 13 | 14 | 15 | class products extends endpoints { 16 | 17 | 18 | /** 19 | * Get a single product 20 | * 21 | * @since 0.0.1 22 | * 23 | * @param \WP_REST_Request $request Full details about the request 24 | * 25 | * @return \WP_HTTP_Response 26 | */ 27 | public function get_item( $request) { 28 | $params = $request->get_params(); 29 | $id = $params[ 'id' ]; 30 | if ( 1 < $id ) { 31 | $post = get_post( $id ); 32 | }else{ 33 | $post = null; 34 | } 35 | 36 | 37 | if ( $post ) { 38 | $data = $this->make_data( $post, array() ); 39 | 40 | $response = rest_ensure_response( $data ); 41 | $response->link_header( 'alternate', get_permalink( $id ), array( 'type' => 'text/html' ) ); 42 | }else{ 43 | $response = new \WP_REST_Response( 0, 404, array() ); 44 | } 45 | 46 | $response->set_matched_route( $request->get_route() ); 47 | 48 | return $response; 49 | 50 | } 51 | 52 | /** 53 | * Get multiple products 54 | * 55 | * @since 0.0.1 56 | * 57 | * @param \WP_REST_Request $request Full details about the request 58 | * 59 | * @return \WP_HTTP_Response 60 | */ 61 | public function get_items( $request ) { 62 | $params = $request->get_params(); 63 | 64 | if( $params[ 'soon' ] ) { 65 | $args[ 'meta_key' ] = 'edd_coming_soon'; 66 | $args[ 'meta_value' ] = true; 67 | }else{ 68 | $args = $this->query_args( $params ); 69 | } 70 | 71 | $args[ 'post_type' ] = $this->post_type; 72 | 73 | return $this->do_query( $request, $args ); 74 | 75 | } 76 | 77 | /** 78 | * Get Caldera Forms add-ons 79 | * 80 | * @since 0.0.1 81 | * 82 | * @param \WP_REST_Request $request Full details about the request 83 | * 84 | * @return \WP_HTTP_Response 85 | */ 86 | public function get_cf_addons( $request ) { 87 | $params = $request->get_params(); 88 | $args = $this->query_args( $params ); 89 | $category = $request[ 'category' ]; 90 | 91 | switch( $category ){ 92 | case 'tool' : 93 | $category = 'developer-tool'; 94 | break; 95 | case 'free' : 96 | $category = 'free-caldera-forms-add-on'; 97 | break; 98 | case 'payment' : 99 | $category = 'payment-processers'; 100 | //hack to make paypal show up 101 | add_filter( 'calderawp_api_response_data', function( $data, $args, $class_name ){ 102 | if( $class_name == get_class( $this ) ){ 103 | $data = $this->make_data( get_post( 561 ), $data ); 104 | } 105 | 106 | 107 | return $data; 108 | }, 20, 3 ); 109 | break; 110 | case 'bundles' : 111 | case 'bundle' : 112 | $category = 'caldera-forms-bundles'; 113 | break; 114 | } 115 | if( ! in_array( $category, [ 'developer-tool', 'email', 'content-managment','free-caldera-forms-add-on', 'payment-processers', 'caldera-forms-bundles' ] ) ){ 116 | $category = 'all-cf-addons'; 117 | } 118 | $args[ 'tax_query' ] = array( 119 | array( 120 | 'taxonomy' => 'download_category', 121 | 'field' => 'slug', 122 | 'terms' => $category, 123 | ), 124 | ); 125 | 126 | 127 | 128 | return $this->do_query( $request, $args ); 129 | 130 | } 131 | 132 | /** 133 | * Get plugins in caldera search bundle 134 | * 135 | * @since 0.2.0 136 | * 137 | * @param \WP_REST_Request $request Full details about the request 138 | * 139 | * @return \WP_HTTP_Response 140 | */ 141 | public function get_caldera_search( \WP_REST_Request $request ) { 142 | $args[ 'post__in' ] = array( 333, 3688, 1427, 4172 ); 143 | 144 | return $this->do_query( $request, $args ); 145 | 146 | } 147 | 148 | /** 149 | * Get Caldera Forms bundle 150 | * 151 | * @since 0.2.0 152 | * 153 | * @param \WP_REST_Request $request Full details about the request 154 | * 155 | * @return \WP_HTTP_Response 156 | */ 157 | public function get_cf_bundles( \WP_REST_Request $request ){ 158 | $bundles = [ 159 | 20521, //free 160 | 20520, //developer 161 | 20518, //agency 162 | //20515, //unlimited (discontinued) 163 | 48255, //enterprise 164 | ]; 165 | $args[ 'post__in' ] = $bundles; 166 | 167 | return $this->do_query( $request, $args ); 168 | 169 | } 170 | 171 | /** 172 | * Get featured plugins 173 | * 174 | * @since 0.0.1 175 | * 176 | * @param \WP_REST_Request $request Full details about the request 177 | * 178 | * @return \WP_HTTP_Response 179 | */ 180 | public function get_featured( $request ) { 181 | $params = $request->get_params(); 182 | $args = $this->query_args( $params ); 183 | $args[ 'meta_key' ] = 'show_on_front_page'; 184 | $args[ 'meta_value' ] = true; 185 | 186 | return $this->do_query( $request, $args ); 187 | 188 | } 189 | 190 | /** 191 | * Add current post to response data for this route. 192 | * 193 | * @since 0.0.1 194 | * 195 | * @param \WP_Post $post Current post object. 196 | * @param array $data Current collection of data 197 | * 198 | * @return array 199 | */ 200 | protected function make_data( \WP_Post $post, $data ) { 201 | $image = get_post_thumbnail_id( $post->ID ); 202 | if ( $image ) { 203 | $_image = wp_get_attachment_image_src( $image, 'large' ); 204 | if ( is_array( $_image ) ) { 205 | $image = $_image[0]; 206 | } 207 | 208 | } 209 | 210 | $data[ $post->ID ] = array( 211 | 'name' => $post->post_title, 212 | 'link' => get_the_permalink( $post->ID ), 213 | 'image_markup' => get_the_post_thumbnail( $post->ID, 'large' ), 214 | 'image_src' => $image, 215 | 'excerpt' => $post->post_excerpt, 216 | 'tagline' => get_post_meta( $post->ID, 'product_tagline', true ), 217 | 'prices' => edd_get_variable_prices( $post->ID ), 218 | 'slug' => $post->post_name, 219 | 'cf' => get_post_meta( $post->ID, 'cf_add_on', true ), 220 | ); 221 | 222 | for ( $i = 1; $i <= 3; $i++ ) { 223 | foreach( array( 224 | 'title', 225 | 'text', 226 | 'image' 227 | ) as $field ) { 228 | if ( 'image' != $field ) { 229 | $field = "benefit_{$i}_{$field}"; 230 | $data[ $post->ID ][ $field ] = get_post_meta( $post->ID, $field, true ); 231 | }else{ 232 | $field = "benefit_{$i}_{$field}"; 233 | $_field = get_post_meta( $post->ID, $field, true ); 234 | $url = false; 235 | 236 | if ( is_array( $_field ) && isset( $_field[ 'ID' ] )) { 237 | $img = $_field[ 'ID' ]; 238 | $img = wp_get_attachment_image_src( $img, 'large' ); 239 | 240 | if ( is_array( $img ) ) { 241 | 242 | $url = $img[0]; 243 | } 244 | $_field[ 'image_src' ] = $url; 245 | 246 | } 247 | $data[ $post->ID ][ $field ] = $_field; 248 | } 249 | 250 | } 251 | 252 | } 253 | 254 | 255 | return $data; 256 | 257 | } 258 | 259 | 260 | 261 | } 262 | -------------------------------------------------------------------------------- /src/routes/util.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link 9 | * @copyright 2015 Josh Pollock 10 | */ 11 | 12 | namespace calderawp\calderawp_api\routes; 13 | 14 | 15 | class util { 16 | 17 | /** 18 | * Get utility stuff 19 | * 20 | * @since 0.0.1 21 | * 22 | * @param \WP_REST_Request $request Full details about the request 23 | * 24 | * @return \WP_HTTP_Response 25 | */ 26 | public function get_items( $request ) { 27 | $params = $request->get_params(); 28 | $what = $params[ 'what' ]; 29 | switch ( $what ) { 30 | case 'mailchimp' == $what : 31 | $data[ 'mailchimp' ] = $this->mailchimp(); 32 | break; 33 | case 'support' == $what : 34 | $data[ 'support' ] = $this->support(); 35 | break; 36 | default : 37 | $data[ 'mailchimp' ] = $this->mailchimp(); 38 | $data[ 'support' ] = $this->support(); 39 | break; 40 | } 41 | 42 | $response = rest_ensure_response( $data ); 43 | 44 | return $response; 45 | 46 | } 47 | 48 | /** 49 | * Mailchimp subscribe form. 50 | * 51 | * @since 0.0.1 52 | * 53 | * @return string 54 | */ 55 | protected function mailchimp() { 56 | $data[ 'title' ] = __( 'Join Our Mailing List', 'calderawp-license-manager' ); 57 | $data[ 'message' ] = __( 'Get news, WordPress tricks and special saving.', 'calderawp-license-manager' ); 58 | $data[ 'form' ] = include dirname( __FILE__ ) . '/include/mailchimp.php'; 59 | 60 | return $data; 61 | 62 | } 63 | 64 | /** 65 | * Support form. 66 | * 67 | * @todo this 68 | * 69 | * @since 0.0.1 70 | * 71 | * @return string 72 | */ 73 | protected function support() { 74 | $data[ 'title' ] = ''; 75 | $data[ 'message' ] = ''; 76 | $data[ 'form' ] = include dirname( __FILE__ ) . '/include/support-form.php'; 77 | 78 | return $data; 79 | 80 | } 81 | 82 | } 83 | --------------------------------------------------------------------------------