| 1 |
<?php |
| 2 |
/** |
| 3 |
* Remote funnels controller |
| 4 |
* |
| 5 |
* @package WPFunnels\Rest\Controllers |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Rest\Controllers; |
| 8 |
|
| 9 |
use WP_Error; |
| 10 |
use WP_REST_Request; |
| 11 |
use WP_REST_Response; |
| 12 |
use WP_REST_Server; |
| 13 |
use WPFunnels\Wpfnl_functions; |
| 14 |
|
| 15 |
class RemoteFunnelsController extends Wpfnl_REST_Controller { |
| 16 |
|
| 17 |
/** |
| 18 |
* Endpoint namespace. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $namespace = 'wpfunnels/v1'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Route base. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $rest_base = 'remote_funnel/'; |
| 30 |
|
| 31 |
|
| 32 |
public function update_items_permissions_check( $request ) { |
| 33 |
if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'products' ) ) { |
| 34 |
return new WP_Error( 'wpfunnels_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) ); |
| 35 |
} |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Makes sure the current user has access to READ the settings APIs. |
| 41 |
* |
| 42 |
* @param WP_REST_Request $request Full data about the request. |
| 43 |
* |
| 44 |
* @return WP_Error|boolean |
| 45 |
* @since 3.0.0 |
| 46 |
*/ |
| 47 |
public function get_items_permissions_check( $request ) { |
| 48 |
if ( ! Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'products' ) ) { |
| 49 |
return new WP_Error( 'wpfunnels_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) ); |
| 50 |
} |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function register_routes() |
| 57 |
{ |
| 58 |
register_rest_route( |
| 59 |
$this->namespace, '/' . $this->rest_base . 'get_template_properties'. '/(?P<funnel_id>\d+)' , array( |
| 60 |
array( |
| 61 |
'methods' => WP_REST_Server::READABLE, |
| 62 |
'callback' => array( $this, 'get_template_properties' ), |
| 63 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 64 |
), |
| 65 |
) |
| 66 |
); |
| 67 |
|
| 68 |
register_rest_route( |
| 69 |
$this->namespace, '/' . $this->rest_base . 'get_step_properties'. '/(?P<step_id>\d+)' , array( |
| 70 |
array( |
| 71 |
'methods' => WP_REST_Server::READABLE, |
| 72 |
'callback' => array( $this, 'get_step_properties' ), |
| 73 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 74 |
), |
| 75 |
) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
public function get_template_properties($request) { |
| 81 |
$funnel_id = $request['funnel_id']; |
| 82 |
$industries = $this->get_properties(WPFNL_TAXONOMY_TEMPLATES_INDUSTRIES); |
| 83 |
$builders = $this->get_properties(WPFNL_TAXONOMY_TEMPLATES_BUILDER); |
| 84 |
|
| 85 |
$template_industry = wp_get_post_terms($funnel_id, WPFNL_TAXONOMY_TEMPLATES_INDUSTRIES, [ 'fields' => 'ids' ]); |
| 86 |
$template_builder = wp_get_post_terms($funnel_id, WPFNL_TAXONOMY_TEMPLATES_BUILDER, [ 'fields' => 'ids' ]); |
| 87 |
$template_type = get_post_meta($funnel_id, '_template_type', true); |
| 88 |
|
| 89 |
$thumbnail_id = get_post_meta($funnel_id, '_thumbnail_id', true); |
| 90 |
$featured_image_src = ''; |
| 91 |
if($thumbnail_id) { |
| 92 |
$featured_image = wp_get_attachment_image_src($thumbnail_id); |
| 93 |
if($featured_image) { |
| 94 |
$featured_image_src = $featured_image[0]; |
| 95 |
} |
| 96 |
} else { |
| 97 |
$thumbnail_id = ''; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if ($template_industry && !is_wp_error($template_industry)) { |
| 102 |
$template_industry = $template_industry[0]; |
| 103 |
} else { |
| 104 |
$template_industry = ''; |
| 105 |
} |
| 106 |
|
| 107 |
if ($template_builder && !is_wp_error($template_builder)) { |
| 108 |
$template_builder = $template_builder[0]; |
| 109 |
}else { |
| 110 |
$template_builder = ''; |
| 111 |
} |
| 112 |
|
| 113 |
$response['success'] = true; |
| 114 |
$response['industries'] = $industries; |
| 115 |
$response['builders'] = $builders; |
| 116 |
$response['industry'] = $template_industry; |
| 117 |
$response['builder'] = $template_builder; |
| 118 |
$response['type'] = $template_type ? $template_type : 'free'; |
| 119 |
$response['featuredImage'] = $featured_image_src; |
| 120 |
$response['thumbnailID'] = $thumbnail_id; |
| 121 |
return $this->prepare_item_for_response( $response, $request ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Get properties of a single |
| 127 |
* funnel |
| 128 |
* |
| 129 |
* @since 2.0.0 |
| 130 |
*/ |
| 131 |
public function get_properties( $taxonomy ) { |
| 132 |
$categories = get_terms([ |
| 133 |
'taxonomy' => $taxonomy, |
| 134 |
'hide_empty' => false, |
| 135 |
]); |
| 136 |
if (!empty($categories) && !is_wp_error($categories)) { |
| 137 |
return $categories; |
| 138 |
} |
| 139 |
return []; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function get_step_properties($request) { |
| 145 |
$step_id = $request['step_id']; |
| 146 |
$thumbnail_id = get_post_meta($step_id, '_thumbnail_id', true); |
| 147 |
$custom_url = get_post_meta($step_id, '_step_custom_url', true); |
| 148 |
$featured_image_src = ''; |
| 149 |
if($thumbnail_id) { |
| 150 |
$featured_image = wp_get_attachment_image_src($thumbnail_id); |
| 151 |
if($featured_image) { |
| 152 |
$featured_image_src = $featured_image[0]; |
| 153 |
} |
| 154 |
} else { |
| 155 |
$thumbnail_id = ''; |
| 156 |
} |
| 157 |
$response['success'] = true; |
| 158 |
$response['featuredImage'] = $featured_image_src; |
| 159 |
$response['thumbnailID'] = $thumbnail_id; |
| 160 |
$response['customUrl'] = $custom_url; |
| 161 |
return $this->prepare_item_for_response( $response, $request ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Prepare a single setting object for response. |
| 167 |
* |
| 168 |
* @param object $item Setting object. |
| 169 |
* @param WP_REST_Request $request Request object. |
| 170 |
* |
| 171 |
* @return WP_REST_Response $response Response data. |
| 172 |
* @since 1.0.0 |
| 173 |
*/ |
| 174 |
public function prepare_item_for_response( $item, $request ) { |
| 175 |
$data = $this->add_additional_fields_to_object( $item, $request ); |
| 176 |
$response = rest_ensure_response( $data ); |
| 177 |
return $response; |
| 178 |
} |
| 179 |
} |
| 180 |
|