PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.3
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.3
6.2.5 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 All 126 releases
wp-to-buffer / lib / social / includes / class-install.php

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

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