PluginProbe
Customify / 2.0.2
Customify v2.0.2
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / includes / lib / class-customify-cloud-api.php

class-customify-cloud-api.php in Customify 2.0.2, at includes/lib/class-customify-cloud-api.php

216 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This is the class that handles the communication with the cloud.
4 *
5 * @see https://pixelgrade.com
6 * @author Pixelgrade
7 * @since 1.7.4
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly
12 }
13
14 if ( ! class_exists( 'Customify_Cloud_Api' ) ) :
15
16 class Customify_Cloud_Api {
17
18 /**
19 * External REST API endpoints used for communicating with the Pixelgrade Cloud.
20 * @var array
21 * @access public
22 * @since 1.7.4
23 */
24 public static $externalApiEndpoints;
25
26 /**
27 * Constructor.
28 *
29 * @since 1.7.4
30 */
31 public function __construct() {
32
33 $this->init();
34 }
35
36 /**
37 * Initialize this module.
38 *
39 * @since 1.7.4
40 */
41 public function init() {
42 // Make sure our constants are in place, if not already defined.
43 defined( 'PIXELGRADE_CLOUD__API_BASE' ) || define( 'PIXELGRADE_CLOUD__API_BASE', 'https://cloud.pixelgrade.com/' );
44
45 // Save the external API endpoints in a easy to get property.
46 self::$externalApiEndpoints = apply_filters( 'customify_style_manager_external_api_endpoints', array(
47 'cloud' => array(
48 'getDesignAssets' => array(
49 'method' => 'GET',
50 'url' => PIXELGRADE_CLOUD__API_BASE . 'wp-json/pixcloud/v1/front/design_assets',
51 ),
52 'stats' => array(
53 'method' => 'POST',
54 'url' => PIXELGRADE_CLOUD__API_BASE . 'wp-json/pixcloud/v1/front/stats',
55 ),
56 ),
57 ) );
58 }
59
60 /**
61 * Fetch the design assets data from the Pixelgrade Cloud.
62 *
63 * @since 1.7.4
64 *
65 * @return array|false
66 */
67 public function fetch_design_assets() {
68 $request_data = array(
69 'site_url' => home_url('/'),
70 // We are only interested in data needed to identify the theme and eventually deliver only design assets suitable for it.
71 'theme_data' => $this->get_active_theme_data(),
72 // We are only interested in data needed to identify the plugin version and eventually deliver design assets suitable for it.
73 'site_data' => $this->get_site_data(),
74 // Extra post statuses besides `publish`.
75 'post_status' => array(),
76 );
77
78 // Handle development and testing constants.
79 if ( defined('SM_FETCH_DRAFT_ASSETS') && true === SM_FETCH_DRAFT_ASSETS ) {
80 $request_data['post_status'][] = 'draft';
81 }
82 if ( defined('SM_FETCH_PRIVATE_ASSETS') && true === SM_FETCH_PRIVATE_ASSETS ) {
83 $request_data['post_status'][] = 'private';
84 }
85 if ( defined('SM_FETCH_FUTURE_ASSETS') && true === SM_FETCH_FUTURE_ASSETS ) {
86 $request_data['post_status'][] = 'future';
87 }
88
89 // Allow others to filter the data we send.
90 $request_data = apply_filters( 'customify_pixelgrade_cloud_request_data', $request_data, $this );
91
92 $request_args = array(
93 'method' => self::$externalApiEndpoints['cloud']['getDesignAssets']['method'],
94 'timeout' => 5,
95 'blocking' => true,
96 'body' => $request_data,
97 'sslverify' => false,
98 );
99 // Get the design assets from the cloud.
100 $response = wp_remote_request( self::$externalApiEndpoints['cloud']['getDesignAssets']['url'], $request_args );
101 // Bail in case of decode error or failure to retrieve data.
102 // We will return the data already available.
103 if ( is_wp_error( $response ) ) {
104 return false;
105 }
106 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
107 // Bail in case of decode error or failure to retrieve data.
108 // We will return the data already available.
109 if ( null === $response_data || empty( $response_data['data'] ) || empty( $response_data['code'] ) || 'success' !== $response_data['code'] ) {
110 return false;
111 }
112
113 return apply_filters( 'customify_style_manager_fetch_design_assets', $response_data['data'] );
114 }
115
116 /**
117 * Get the active theme data.
118 *
119 * @since 1.7.4
120 *
121 * @return array
122 */
123 public function get_active_theme_data() {
124 $theme_data = array();
125
126 $slug = basename( get_template_directory() );
127
128 $theme_data['slug'] = $slug;
129
130 // Get the current theme style.css data.
131 $current_theme = wp_get_theme( get_template() );
132 if ( ! empty( $current_theme ) && ! is_wp_error( $current_theme ) ) {
133 $theme_data['name'] = $current_theme->get('Name');
134 $theme_data['themeuri'] = $current_theme->get('ThemeURI');
135 $theme_data['version'] = $current_theme->get('Version');
136 $theme_data['textdomain'] = $current_theme->get('TextDomain');
137 }
138
139 // Maybe get the WUpdates theme info if it's a theme delivered from WUpdates.
140 $wupdates_ids = apply_filters( 'wupdates_gather_ids', array() );
141 if ( ! empty( $wupdates_ids[ $slug ] ) ) {
142 $theme_data['wupdates'] = $wupdates_ids[ $slug ];
143 }
144
145 return apply_filters( 'customify_style_manager_get_theme_data', $theme_data );
146 }
147
148 /**
149 * Get the site data.
150 *
151 * @since 1.7.4
152 *
153 * @return array
154 */
155 public function get_site_data() {
156 $site_data = array(
157 'url' => home_url('/'),
158 'is_ssl' => is_ssl(),
159 );
160
161 $site_data['wp'] = array(
162 'version' => get_bloginfo('version'),
163 );
164
165 $site_data['customify'] = array(
166 'version' => PixCustomifyPlugin()->get_version(),
167 );
168
169 return apply_filters( 'customify_style_manager_get_site_data', $site_data );
170 }
171
172 /**
173 * Send stats to the Pixelgrade Cloud.
174 *
175 * @since 1.7.4
176 *
177 * @param array $request_data The data to be sent.
178 * @param bool $blocking Optional. Whether this should be a blocking request. Defaults to false.
179 *
180 * @return array|false
181 */
182 public function send_stats( $request_data = array(), $blocking = false ) {
183 if ( empty( $request_data ) ) {
184 // This is what we send by default.
185 $request_data = array(
186 'site_url' => home_url('/'),
187 // We are only interested in data needed to identify the theme and eventually deliver only design assets suitable for it.
188 'theme_data' => $this->get_active_theme_data(),
189 // We are only interested in data needed to identify the plugin version and eventually deliver design assets suitable for it.
190 'site_data' => $this->get_site_data(),
191 );
192 }
193
194 /**
195 * Filters request data sent to the cloud.
196 *
197 * @param array $request_data
198 * @param object $this @todo This argument is no longer needed and should be removed when Pixelgrade Care doesn't rely on it.
199 */
200 $request_data = apply_filters( 'customify_pixelgrade_cloud_request_data', $request_data, $this );
201
202 $request_args = array(
203 'method' => self::$externalApiEndpoints['cloud']['stats']['method'],
204 'timeout' => 5,
205 'blocking' => $blocking,
206 'body' => $request_data,
207 'sslverify' => false,
208 );
209
210 // Make the request and return the response.
211 return wp_remote_request( self::$externalApiEndpoints['cloud']['stats']['url'], $request_args );
212 }
213 }
214
215 endif;
216