PluginProbe
Customify / 1.7.4
Customify v1.7.4
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 1.7.4, at includes/lib/class-customify-cloud-api.php

200 lines 5.8 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 = apply_filters( 'customify_pixelgrade_cloud_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 ), $this );
75
76 $request_args = array(
77 'method' => self::$externalApiEndpoints['cloud']['getDesignAssets']['method'],
78 'timeout' => 4,
79 'blocking' => true,
80 'body' => $request_data,
81 'sslverify' => false,
82 );
83 // Get the design assets from the cloud.
84 $response = wp_remote_request( self::$externalApiEndpoints['cloud']['getDesignAssets']['url'], $request_args );
85 // Bail in case of decode error or failure to retrieve data.
86 // We will return the data already available.
87 if ( is_wp_error( $response ) ) {
88 return false;
89 }
90 $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
91 // Bail in case of decode error or failure to retrieve data.
92 // We will return the data already available.
93 if ( null === $response_data || empty( $response_data['data'] ) || empty( $response_data['code'] ) || 'success' !== $response_data['code'] ) {
94 return false;
95 }
96
97 return apply_filters( 'customify_style_manager_fetch_design_assets', $response_data['data'] );
98 }
99
100 /**
101 * Get the active theme data.
102 *
103 * @since 1.7.4
104 *
105 * @return array
106 */
107 public function get_active_theme_data() {
108 $theme_data = array();
109
110 $slug = basename( get_template_directory() );
111
112 $theme_data['slug'] = $slug;
113
114 // Get the current theme style.css data.
115 $current_theme = wp_get_theme( get_template() );
116 if ( ! empty( $current_theme ) && ! is_wp_error( $current_theme ) ) {
117 $theme_data['name'] = $current_theme->get('Name');
118 $theme_data['themeuri'] = $current_theme->get('ThemeURI');
119 $theme_data['version'] = $current_theme->get('Version');
120 $theme_data['textdomain'] = $current_theme->get('TextDomain');
121 }
122
123 // Maybe get the WUpdates theme info if it's a theme delivered from WUpdates.
124 $wupdates_ids = apply_filters( 'wupdates_gather_ids', array() );
125 if ( ! empty( $wupdates_ids[ $slug ] ) ) {
126 $theme_data['wupdates'] = $wupdates_ids[ $slug ];
127 }
128
129 return apply_filters( 'customify_style_manager_get_theme_data', $theme_data );
130 }
131
132 /**
133 * Get the site data.
134 *
135 * @since 1.7.4
136 *
137 * @return array
138 */
139 public function get_site_data() {
140 $site_data = array(
141 'url' => home_url('/'),
142 'is_ssl' => is_ssl(),
143 );
144
145 $site_data['wp'] = array(
146 'version' => get_bloginfo('version'),
147 );
148
149 $site_data['customify'] = array(
150 'version' => PixCustomifyPlugin()->get_version(),
151 );
152
153 return apply_filters( 'customify_style_manager_get_site_data', $site_data );
154 }
155
156 /**
157 * Send stats to the Pixelgrade Cloud.
158 *
159 * @since 1.7.4
160 *
161 * @param array $request_data The data to be sent.
162 * @param bool $blocking Optional. Whether this should be a blocking request. Defaults to false.
163 *
164 * @return array|false
165 */
166 public function send_stats( $request_data = array(), $blocking = false ) {
167 if ( empty( $request_data ) ) {
168 // This is what we send by default.
169 $request_data = array(
170 'site_url' => home_url('/'),
171 // We are only interested in data needed to identify the theme and eventually deliver only design assets suitable for it.
172 'theme_data' => $this->get_active_theme_data(),
173 // We are only interested in data needed to identify the plugin version and eventually deliver design assets suitable for it.
174 'site_data' => $this->get_site_data(),
175 );
176 }
177
178 /**
179 * Filters request data sent to the cloud.
180 *
181 * @param array $request_data
182 * @param object $this @todo This argument is no longer needed and should be removed when Pixelgrade Care doesn't rely on it.
183 */
184 $request_data = apply_filters( 'customify_pixelgrade_cloud_request_data', $request_data, $this );
185
186 $request_args = array(
187 'method' => self::$externalApiEndpoints['cloud']['stats']['method'],
188 'timeout' => 5,
189 'blocking' => $blocking,
190 'body' => $request_data,
191 'sslverify' => false,
192 );
193
194 // Make the request and return the response.
195 return wp_remote_request( self::$externalApiEndpoints['cloud']['stats']['url'], $request_args );
196 }
197 }
198
199 endif;
200