PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.0
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.0
6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.2 6.1.1 6.1.0 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 All 125 releases
wp-to-buffer / lib / social / includes / class-install.php

class-install.php in Social Media Auto Poster – Schedule & Publish to Buffer 6.2.0, at lib/social/includes/class-install.php

421 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Install class.
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * Runs any steps required on plugin activation and upgrade.
13 *
14 * @package WPZinc\Social
15 * @author WP Zinc
16 * @version 3.2.5
17 */
18 class Install {
19
20 /**
21 * Holds the base class object.
22 *
23 * @since 3.2.5
24 *
25 * @var object
26 */
27 public $base;
28
29 /**
30 * Constructor
31 *
32 * @since 3.4.7
33 *
34 * @param object $base Base Plugin Class.
35 */
36 public function __construct( $base ) {
37
38 // Store base class.
39 $this->base = $base;
40
41 }
42
43 /**
44 * Runs installation routines for first time users
45 *
46 * @since 3.4.0
47 */
48 public function install() {
49
50 // Enable logging by default.
51 $this->base->get_class( 'settings' )->update_option(
52 'log',
53 array(
54 'enabled' => 1,
55 'display_on_posts' => 1,
56 'preserve_days' => 30,
57 'log_level' => array(
58 'success',
59 'test',
60 'pending',
61 'warning',
62 'error',
63 ),
64 )
65 );
66
67 // Create logging database table.
68 $this->base->get_class( 'log' )->activate();
69
70 // Reschedule the cron events.
71 $this->base->get_class( 'cron' )->schedule_log_cleanup_event();
72 $this->base->get_class( 'cron' )->schedule_media_cleanup_event();
73 $this->base->get_class( 'cron' )->schedule_refresh_token_event();
74
75 // Bail if settings already exist.
76 $settings = $this->base->get_class( 'settings' )->get_settings( 'post' );
77 if ( $settings !== false ) {
78 return;
79 }
80
81 // Get default installation settings.
82 $settings = $this->base->get_class( 'settings' )->default_installation_settings( 'post' );
83 $this->base->get_class( 'settings' )->update_settings( 'post', $settings );
84
85 }
86
87 /**
88 * Runs migrations whenever the Plugin's version is updated.
89 *
90 * @since 3.2.5
91 */
92 public function upgrade() {
93
94 // These sit outside of version checks, to ensure they run regardless of version.
95 // Otherwise, Free to Free Plugin updates for the multi account result in losing
96 // account data because there's also an API version switch.
97 // At a later relase, we'll need to remove these so they don't run on every upgrade.
98
99 // Migrate Access Tokens to multi account settings.
100 $this->migrate_access_tokens_to_new_multi_account_settings();
101
102 // Migrate Status Settings to new format that supports "Type"
103 // instead of a numerical setting for the status' index.
104 $this->migrate_status_settings_to_new_format();
105
106 // Get current installed version number.
107 // false | 1.1.7.
108 $installed_version = get_option( $this->base->plugin->name . '-version' );
109
110 // If the version number matches the plugin version, bail.
111 if ( $installed_version === $this->base->plugin->version ) {
112 return;
113 }
114
115 // Reschedule the cron events.
116 $this->base->get_class( 'cron' )->reschedule_log_cleanup_event();
117 $this->base->get_class( 'cron' )->reschedule_media_cleanup_event();
118 $this->base->get_class( 'cron' )->reschedule_refresh_token_event();
119
120 // Update the version number.
121 update_option( $this->base->plugin->name . '-version', $this->base->plugin->version );
122
123 }
124
125 /**
126 * Migrates the access token, refresh token and token expiry to the new multi account settings.
127 *
128 * @since 6.0.0
129 *
130 * @return void
131 */
132 private function migrate_access_tokens_to_new_multi_account_settings() {
133
134 // Check if the non-multi account settings exist.
135 $access_token = get_option( $this->base->plugin->settingsName . '-access-token' );
136 $refresh_token = get_option( $this->base->plugin->settingsName . '-refresh-token' );
137 $token_expires = get_option( $this->base->plugin->settingsName . '-token-expires' );
138
139 // Bail if no access token exists - either the Plugin isn't connected, or it has
140 // already been migrated to the new multi account settings.
141 if ( empty( $access_token ) ) {
142 return;
143 }
144
145 // Store tokens in new multi account settings.
146 // This will be stored under the ID 'default'.
147 $this->base->get_class( 'settings' )->update_account(
148 $access_token,
149 $refresh_token,
150 $token_expires
151 );
152
153 // Delete old settings.
154 delete_option( $this->base->plugin->settingsName . '-access-token' );
155 delete_option( $this->base->plugin->settingsName . '-refresh-token' );
156 delete_option( $this->base->plugin->settingsName . '-token-expires' );
157
158 }
159
160 /**
161 * Migrates Status Settings to the new format that supports
162 * a "Type" selection dropdown.
163 *
164 * @since 6.0.0
165 *
166 * @return void
167 */
168 private function migrate_status_settings_to_new_format() {
169
170 // Define option flags.
171 $migrated_status_settings_flag = $this->base->plugin->name . '-migrate-status-settings-completed';
172
173 // Bail if the migration has already run.
174 if ( get_option( $migrated_status_settings_flag ) ) {
175 return;
176 }
177
178 // Get accounts.
179 $accounts = $this->base->get_class( 'settings' )->get_accounts();
180
181 // Bail if no accounts exist.
182 if ( empty( $accounts ) ) {
183 return;
184 }
185
186 // Build array of all profiles across all accounts connected to the Plugin.
187 $profiles = array();
188 foreach ( $accounts as $account_id => $account ) {
189 $account_profiles = get_transient( $this->base->plugin->name . '_' . strtolower( $this->base->plugin->account ) . '_api_profiles_' . $account_id );
190 if ( ! $account_profiles ) {
191 continue;
192 }
193 $profiles = array_merge( $profiles, $account_profiles );
194 }
195
196 // If no profiles exist, bail.
197 if ( empty( $profiles ) ) {
198 return;
199 }
200
201 // Fetch actions (publish, update, repost, bulk publish).
202 $actions = $this->base->get_class( 'common' )->get_post_actions();
203
204 // Fetch all Post Type settings.
205 $post_types = $this->base->get_class( 'common' )->get_post_types();
206
207 // Iterate through Post Types, migrating their Status Settings.
208 foreach ( $post_types as $post_type ) {
209 // Get Post Type Settings.
210 $settings = $this->base->get_class( 'settings' )->get_settings( $post_type->name );
211
212 // Skip if not an array.
213 if ( ! is_array( $settings ) ) {
214 continue;
215 }
216
217 // Migrate.
218 $settings = $this->migrate_status_settings( $settings, $actions, $profiles );
219
220 // Save settings for this Post Type.
221 $this->base->get_class( 'settings' )->update_settings( $post_type->name, $settings );
222 }
223
224 // Mark the migration as completed.
225 update_option( $migrated_status_settings_flag, time(), false );
226
227 }
228
229 /**
230 * Migrates status settings to the new format that supports
231 * a "Type" selection dropdown.
232 *
233 * @since 6.0.0
234 *
235 * @param array $settings Post Type or individual Post Settings.
236 * @param array $actions Actions.
237 * @param array $profiles Profiles.
238 * @return array Migrated Settings.
239 */
240 public function migrate_status_settings( $settings, $actions, $profiles ) {
241
242 // Iterate through settings.
243 foreach ( $settings as $profile_id => $profile ) {
244 // Skip if profile isn't an array.
245 if ( ! is_array( $profile ) ) {
246 continue;
247 }
248
249 // Iterate through actions.
250 foreach ( $actions as $action => $action_label ) {
251 // Skip if the action isn't an array.
252 if ( ! array_key_exists( $action, $profile ) || ! is_array( $profile[ $action ] ) ) {
253 continue;
254 }
255
256 // Iterate through statuses.
257 foreach ( $profile[ $action ]['status'] as $status_index => $status ) {
258 // Skip if a post_type already exists.
259 if ( array_key_exists( 'post_type', $status ) ) {
260 continue;
261 }
262
263 // Status Type.
264 switch ( (string) $status['image'] ) {
265 // No Image.
266 case '-1':
267 $status['post_type'] = 'text';
268 $status['image'] = '';
269 break;
270
271 // Use Feat. Image, Linked to Post.
272 case '1':
273 $status['post_type'] = 'image';
274 $status['image'] = 'featured_image';
275 break;
276
277 // Use Feat. Image, not Linked to Post.
278 case '2':
279 $status['post_type'] = 'image';
280 $status['image'] = 'featured_image';
281 break;
282
283 // Use Text to Image, Linked to Post.
284 case '3':
285 $status['post_type'] = 'image';
286 $status['image'] = 'text_to_image';
287 break;
288
289 // Use Text to Image, not Linked to Post.
290 case '4':
291 $status['post_type'] = 'image';
292 $status['image'] = 'text_to_image';
293 break;
294
295 // OpenGraph / Link Preview.
296 case '0':
297 case '':
298 default:
299 if ( array_key_exists( 'link', $this->base->get_class( 'common' )->get_status_post_type_options() ) ) {
300 $status['post_type'] = 'link';
301 $status['image'] = '';
302
303 // Move the {url} or {url_short} from the message to the URL setting.
304 if ( strpos( $status['message'], '{url}' ) !== false ) {
305 $status['message'] = str_replace( '{url}', '', $status['message'] );
306 $status['url'] = '{url}';
307 } elseif ( strpos( $status['message'], '{url_short}' ) !== false ) {
308 $status['message'] = str_replace( '{url_short}', '', $status['message'] );
309 $status['url'] = '{url_short}';
310 } else {
311 // Status is OpenGraph but no URL included.
312 // Set the URL to the Post's URL.
313 $status['url'] = '{url}';
314 }
315 } else {
316 $status['post_type'] = 'text';
317 $status['image'] = '';
318 }
319 break;
320 }
321
322 // Schedule.
323 switch ( $status['schedule'] ) {
324 case 'queue_bottom':
325 $status['schedule'] = 'queue_end';
326 break;
327
328 case 'queue_top':
329 $status['schedule'] = 'queue_start';
330 break;
331
332 case 'now':
333 $status['schedule'] = 'immediate';
334 break;
335 }
336
337 // Adjust image_additional setting.
338 if ( array_key_exists( 'image_additional', $status ) ) {
339 switch ( $status['image_additional'] ) {
340 /**
341 * Blank means "Specified in Post settings" < 6.0.0.
342 * 6.0.0 sets blank as 'None'.
343 */
344 case '':
345 $status['image_additional'] = 'post_settings';
346 break;
347
348 /**
349 * 1 means "Auto populate from Post content" < 6.0.0.
350 */
351 case '1':
352 $status['image_additional'] = 'post_content';
353 break;
354 }
355 }
356
357 // Depending on the Profile Service, the Type may need to be adjusted.
358 // Don't do this if the profile_id = default.
359 foreach ( $profiles as $api_profile ) {
360 if ( $api_profile['id'] !== (string) $profile_id ) {
361 continue;
362 }
363
364 switch ( $api_profile['service'] ) {
365 case 'instagram':
366 $status['post_type'] = $status['update_type'] === 'story' ? 'story' : 'image';
367 break;
368
369 case 'pinterest':
370 $status['post_type'] = 'pin';
371 $status['url'] = array_key_exists( 'source_url', $status ) ? $status['source_url'] : '{url}';
372 if ( ! array_key_exists( 'pinterest', $status ) ) {
373 $status['pinterest'] = array();
374 }
375 if ( ! is_array( $status['pinterest'] ) ) {
376 $status['pinterest'] = array();
377 }
378 $status['pinterest']['board'] = array_key_exists( 'sub_profile', $status ) ? $status['sub_profile'] : '';
379 $status['pinterest']['title'] = array_key_exists( 'title', $status ) ? $status['title'] : '{title}';
380 break;
381
382 case 'googlebusiness':
383 $status['post_type'] = 'googlebusiness';
384 $status['url'] = '{url}';
385 break;
386 }
387 break;
388 }
389
390 // Remove unused status settings.
391 // update_type: Instagram Story/Post, which is now handled by the post_type setting.
392 // title: Pinterest Title, which is now handled by the pinterest array.
393 // source_url: Pinterest Source URL, which is now handled by the url setting.
394 unset( $status['update_type'], $status['title'], $status['source_url'], $status['sub_profile'] );
395
396 // Assign status back to settings.
397 $settings[ $profile_id ][ $action ]['status'][ $status_index ] = $status;
398 }
399 }
400 }
401
402 return $settings;
403
404 }
405
406 /**
407 * Runs uninstallation routines
408 *
409 * @since 3.7.2
410 */
411 public function uninstall() {
412
413 // Unschedule any CRON events.
414 $this->base->get_class( 'cron' )->unschedule_log_cleanup_event();
415 $this->base->get_class( 'cron' )->unschedule_media_cleanup_event();
416 $this->base->get_class( 'cron' )->unschedule_refresh_token_event();
417
418 }
419
420 }
421