| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Registers the `blockspare_template` custom post type. |
| 5 |
* |
| 6 |
* @package Blockspare |
| 7 |
*/ |
| 8 |
|
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
|
| 12 |
class Blockspare_TB_Template_Post_Type |
| 13 |
{ |
| 14 |
|
| 15 |
|
| 16 |
const SLUG = 'blockspare_template'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Registers the post type on `init`. |
| 20 |
*/ |
| 21 |
public function register() |
| 22 |
{ |
| 23 |
register_post_type( |
| 24 |
self::SLUG, |
| 25 |
array( |
| 26 |
'labels' => $this->labels(), |
| 27 |
'public' => false, // Templates are never viewed directly at a public URL. |
| 28 |
'show_ui' => true, |
| 29 |
'show_in_menu' => false, // We build our own top-level admin page instead (see Blockspare_TB_Admin_Menu). |
| 30 |
'show_in_rest' => true, |
| 31 |
'rest_base' => 'blocksparetb-templates', |
| 32 |
'rest_controller_class' => 'Blockspare_TB_Templates_Controller', |
| 33 |
'supports' => array( |
| 34 |
'title', |
| 35 |
'editor', |
| 36 |
'revisions', |
| 37 |
'custom-fields', |
| 38 |
'author' |
| 39 |
), |
| 40 |
'capability_type' => 'page', |
| 41 |
'map_meta_cap' => true, |
| 42 |
'hierarchical' => false, |
| 43 |
'has_archive' => false, |
| 44 |
'exclude_from_search' => true, |
| 45 |
'publicly_queryable' => false, |
| 46 |
'query_var' => false, |
| 47 |
'rewrite' => false, |
| 48 |
) |
| 49 |
); |
| 50 |
$post_type = get_post_type_object(self::SLUG); |
| 51 |
|
| 52 |
add_filter('rest_pre_dispatch', array($this, 'blockspare_limit_free_user_templates'), 10, 3); |
| 53 |
} |
| 54 |
/** |
| 55 |
* Restricts template creation via REST API to a maximum of 2 for free users. |
| 56 |
*/ |
| 57 |
public function blockspare_limit_free_user_templates($result, $server, $request) |
| 58 |
{ |
| 59 |
// Match POST requests to the blocksparetb-templates REST endpoint |
| 60 |
if ('/wp/v2/blocksparetb-templates' === $request->get_route() && 'POST' === $request->get_method()) { |
| 61 |
|
| 62 |
$is_pro_user = apply_filters('blockspare_is_pro_user', false); |
| 63 |
|
| 64 |
if (!$is_pro_user) { |
| 65 |
$published_templates = wp_count_posts(self::SLUG); |
| 66 |
$total_templates = isset($published_templates->publish) ? (int)$published_templates->publish : 0; |
| 67 |
|
| 68 |
if ($total_templates >= 2) { |
| 69 |
return new WP_Error( |
| 70 |
'template_limit_reached', |
| 71 |
__('Free users can only create up to 2 templates. Upgrade to Pro to create more.', 'blockspare'), |
| 72 |
array('status' => 403) |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return $result; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Human-readable labels for the post type. |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
private function labels() |
| 87 |
{ |
| 88 |
return array( |
| 89 |
'name' => __('Theme Templates', 'blockspare'), |
| 90 |
'singular_name' => __('Theme Template', 'blockspare'), |
| 91 |
'add_new_item' => __('Add New Template', 'blockspare'), |
| 92 |
'edit_item' => __('Edit Template', 'blockspare'), |
| 93 |
'all_items' => __('All Templates', 'blockspare'), |
| 94 |
'search_items' => __('Search Templates', 'blockspare'), |
| 95 |
'not_found' => __('No templates found.', 'blockspare'), |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|