PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / trunk
Social Media Auto Poster – Schedule & Publish to Buffer vtrunk
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-common.php

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

657 lines 17.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Common class.
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * Common functions that don't fit into other classes.
13 *
14 * @package WPZinc\Social
15 * @author WP Zinc
16 * @version 3.0.0
17 */
18 class Common {
19
20 /**
21 * Holds the base class object.
22 *
23 * @since 3.4.7
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 * Helper method to retrieve status post type options
45 *
46 * @since 6.0.0
47 *
48 * @return array Status Post Type Options
49 */
50 public function get_status_post_type_options() {
51
52 // Build status post type options.
53 $status_post_type_options = array(
54 'text' => __( 'Text', 'wp-to-buffer' ),
55 'link' => __( 'Link', 'wp-to-buffer' ),
56 'image' => __( 'Image', 'wp-to-buffer' ),
57 );
58
59 /**
60 * Defines the available status post type options.
61 *
62 * @since 6.0.0
63 *
64 * @param array $status_post_type_options Status Post Type Options.
65 */
66 $status_post_type_options = apply_filters( $this->base->plugin->filter_name . '_get_status_post_type_options', $status_post_type_options );
67
68 // Return filtered results.
69 return $status_post_type_options;
70
71 }
72
73 /**
74 * Helper method to retrieve schedule options
75 *
76 * @since 3.0.0
77 *
78 * @param mixed $post_type Post Type (false | string).
79 * @param bool $is_post_screen Displaying the Post Screen.
80 * @return array Schedule Options
81 */
82 public function get_schedule_options( $post_type = false, $is_post_screen = false ) {
83
84 // Immediate posting is supported by every service, so it is always available.
85 $schedule = array(
86 'immediate' => __( 'Post Immediately', 'wp-to-buffer' ),
87 );
88
89 /**
90 * Defines the available schedule options for each individual status.
91 *
92 * @since 3.0.0
93 *
94 * @param array $schedule Schedule Options.
95 * @param string $post_type Post Type.
96 * @param bool $is_post_screen On Post Edit Screen.
97 */
98 $schedule = apply_filters( $this->base->plugin->filter_name . '_get_schedule_options', $schedule, $post_type, $is_post_screen );
99
100 // Return filtered results.
101 return $schedule;
102
103 }
104
105 /**
106 * Helper method to retrieve Google Business Start Date options
107 *
108 * @since 4.9.0
109 *
110 * @param mixed $post_type Post Type (false | string).
111 * @return array Start Date Options
112 */
113 public function get_google_business_start_date_options( $post_type = false ) {
114
115 // Build schedule options.
116 $schedule = array(
117 'custom' => __( 'Custom Field / Post Meta Value', 'wp-to-buffer' ),
118 );
119
120 /**
121 * Defines the available start date options for a Google Business Profile status.
122 *
123 * @since 4.9.0
124 *
125 * @param array $schedule Schedule Options.
126 * @param bool|string $post_type Post Type (false | string).
127 */
128 $schedule = apply_filters( $this->base->plugin->filter_name . '_get_google_business_start_date_options', $schedule, $post_type );
129
130 // Return filtered results.
131 return $schedule;
132
133 }
134
135 /**
136 * Helper method to retrieve Google Business Start Date options
137 *
138 * @since 4.9.0
139 *
140 * @param mixed $post_type Post Type (false | string).
141 * @return array End Date Options
142 */
143 public function get_google_business_end_date_options( $post_type = false ) {
144
145 // Build schedule options.
146 $schedule = array(
147 'custom' => __( 'Custom Field / Post Meta Value', 'wp-to-buffer' ),
148 );
149
150 /**
151 * Defines the available start date options for a Google Business Profile status.
152 *
153 * @since 4.9.0
154 *
155 * @param array $schedule Schedule Options.
156 * @param bool|string $post_type Post Type (false | string).
157 */
158 $schedule = apply_filters( $this->base->plugin->filter_name . '_get_google_business_end_date_options', $schedule, $post_type );
159
160 // Return filtered results.
161 return $schedule;
162
163 }
164
165 /**
166 * Helper method to retrieve public Post Types
167 *
168 * @since 3.0.0
169 *
170 * @return array Public Post Types
171 */
172 public function get_post_types() {
173
174 // Get public Post Types.
175 $types = get_post_types(
176 array(
177 'public' => true,
178 ),
179 'objects'
180 );
181
182 // Filter out excluded post types.
183 $excluded_types = $this->get_excluded_post_types();
184 if ( count( $excluded_types ) > 0 ) {
185 foreach ( $excluded_types as $excluded_type ) {
186 unset( $types[ $excluded_type ] );
187 }
188 }
189
190 /**
191 * Defines the available Post Type Objects that can have statues defined and be sent to social media.
192 *
193 * @since 3.0.0
194 *
195 * @param array $types Post Types.
196 */
197 $types = apply_filters( $this->base->plugin->filter_name . '_get_post_types', $types );
198
199 // Return filtered results.
200 return $types;
201
202 }
203
204 /**
205 * Helper method to retrieve excluded Post Types, which should not send
206 * statuses to the API
207 *
208 * @since 3.0.0
209 *
210 * @return array Excluded Post Types
211 */
212 public function get_excluded_post_types() {
213
214 // Get excluded Post Types.
215 $types = array(
216 'attachment',
217 'revision',
218 'elementor_library',
219 );
220
221 /**
222 * Defines the Post Type Objects that cannot have statues defined and not be sent to social media.
223 *
224 * @since 3.0.0
225 *
226 * @param array $types Post Types.
227 */
228 $types = apply_filters( $this->base->plugin->filter_name . '_get_excluded_post_types', $types );
229
230 // Return filtered results.
231 return $types;
232
233 }
234
235 /**
236 * Helper method to retrieve excluded Taxonomies
237 *
238 * @since 3.0.5
239 *
240 * @return array Excluded Post Types
241 */
242 public function get_excluded_taxonomies() {
243
244 // Get excluded Post Types.
245 $taxonomies = array(
246 'post_format',
247 'nav_menu',
248 );
249
250 /**
251 * Defines taxonomies to exclude from the Conditions: Taxonomies dropdowns for each individual status.
252 *
253 * @since 3.0.5
254 *
255 * @param array $taxonomies Excluded Taxonomies.
256 */
257 $taxonomies = apply_filters( $this->base->plugin->filter_name . '_get_excluded_taxonomies', $taxonomies );
258
259 // Return filtered results.
260 return $taxonomies;
261
262 }
263
264 /**
265 * Helper method to retrieve a Post Type's taxonomies
266 *
267 * @since 3.0.0
268 *
269 * @param string $post_type Post Type.
270 * @return array Taxonomies
271 */
272 public function get_taxonomies( $post_type ) {
273
274 // Get Post Type Taxonomies.
275 $taxonomies = get_object_taxonomies( $post_type, 'objects' );
276
277 // Get excluded Taxonomies.
278 $excluded_taxonomies = $this->get_excluded_taxonomies();
279
280 // If excluded taxonomies exist, remove them from the taxonomies array now.
281 if ( count( $excluded_taxonomies ) > 0 ) {
282 foreach ( $excluded_taxonomies as $excluded_taxonomy ) {
283 unset( $taxonomies[ $excluded_taxonomy ] );
284 }
285 }
286
287 /**
288 * Defines available taxonomies for the given Post Type, which are used in the Conditions: Taxonomies dropdowns
289 * for each individual status.
290 *
291 * @since 3.0.0
292 *
293 * @param array $taxonomies Taxonomies.
294 * @param string $post_type Post Type.
295 */
296 $taxonomies = apply_filters( $this->base->plugin->filter_name . '_get_taxonomies', $taxonomies, $post_type );
297
298 // Return filtered results.
299 return $taxonomies;
300
301 }
302
303 /**
304 * Helper method to retrieve all taxonomies
305 *
306 * @since 3.6.7
307 *
308 * @return array Taxonomies
309 */
310 public function get_all_taxonomies() {
311
312 // Get Post Type Taxonomies.
313 $taxonomies = get_taxonomies( array(), 'objects' );
314
315 // Get excluded Taxonomies.
316 $excluded_taxonomies = $this->get_excluded_taxonomies();
317
318 // If excluded taxonomies exist, remove them from the taxonomies array now.
319 if ( count( $excluded_taxonomies ) > 0 ) {
320 foreach ( $excluded_taxonomies as $excluded_taxonomy ) {
321 unset( $taxonomies[ $excluded_taxonomy ] );
322 }
323 }
324
325 /**
326 * Defines available taxonomies, regardless of Post Type, which are used in the Conditions: Taxonomies dropdowns
327 * for each individual status.
328 *
329 * @since 3.6.7
330 *
331 * @param array $taxonomies Taxonomies.
332 */
333 $taxonomies = apply_filters( $this->base->plugin->filter_name . '_get_all_taxonomies', $taxonomies );
334
335 // Return filtered results.
336 return $taxonomies;
337
338 }
339
340 /**
341 * Helper method to retrieve available tags for status updates
342 *
343 * @since 3.0.0
344 *
345 * @param string $post_type Post Type.
346 * @return array Tags
347 */
348 public function get_tags( $post_type ) {
349
350 // Get post type.
351 $post_types = $this->get_post_types();
352
353 // Build tags array.
354 $tags = array(
355 'post' => array(
356 '{sitename}' => __( 'Site Name', 'wp-to-buffer' ),
357 '{title}' => __( 'Post Title', 'wp-to-buffer' ),
358 '{excerpt}' => __( 'Post Excerpt (Full)', 'wp-to-buffer' ),
359 '{excerpt:characters(?)}' => array(
360 'question' => __( 'Enter the maximum number of characters the Post Excerpt should display.', 'wp-to-buffer' ),
361 'default_value' => '150',
362 'replace' => '?',
363 'label' => __( 'Post Excerpt (Character Limited)', 'wp-to-buffer' ),
364 ),
365 '{excerpt:words(?)}' => array(
366 'question' => __( 'Enter the maximum number of words the Post Excerpt should display.', 'wp-to-buffer' ),
367 'default_value' => '55',
368 'replace' => '?',
369 'label' => __( 'Post Excerpt (Word Limited)', 'wp-to-buffer' ),
370 ),
371 '{excerpt:sentences(?)}' => array(
372 'question' => __( 'Enter the maximum number of sentences the Post Excerpt should display.', 'wp-to-buffer' ),
373 'default_value' => '1',
374 'replace' => '?',
375 'label' => __( 'Post Excerpt (Sentence Limited)', 'wp-to-buffer' ),
376 ),
377 '{content}' => __( 'Post Content (Full)', 'wp-to-buffer' ),
378 '{content_more_tag}' => __( 'Post Content (Up to More Tag)', 'wp-to-buffer' ),
379 '{content:characters(?)}' => array(
380 'question' => __( 'Enter the maximum number of characters the Post Content should display.', 'wp-to-buffer' ),
381 'default_value' => '150',
382 'replace' => '?',
383 'label' => __( 'Post Content (Character Limited)', 'wp-to-buffer' ),
384 ),
385 '{content:words(?)}' => array(
386 'question' => __( 'Enter the maximum number of words the Post Content should display.', 'wp-to-buffer' ),
387 'default_value' => '55',
388 'replace' => '?',
389 'label' => __( 'Post Content (Word Limited)', 'wp-to-buffer' ),
390 ),
391 '{content:sentences(?)}' => array(
392 'question' => __( 'Enter the maximum number of sentences the Post Content should display.', 'wp-to-buffer' ),
393 'default_value' => '1',
394 'replace' => '?',
395 'label' => __( 'Post Content (Sentence Limited)', 'wp-to-buffer' ),
396 ),
397 '{date}' => __( 'Post Date', 'wp-to-buffer' ),
398 '{url}' => __( 'Post URL', 'wp-to-buffer' ),
399 '{url_short}' => __( 'Post URL, Shortened', 'wp-to-buffer' ),
400 '{id}' => __( 'Post ID', 'wp-to-buffer' ),
401 ),
402 );
403
404 // Add any taxonomies for the given Post Type, if the Post Type exists.
405 $taxonomies = array();
406 if ( isset( $post_types[ $post_type ] ) ) {
407 // Get taxonomies specific to the Post Type.
408 $taxonomies = $this->get_taxonomies( $post_type );
409 } else {
410 // We're on the Bulk Publishing Settings, so return all Taxonomies.
411 $taxonomies = $this->get_all_taxonomies();
412 }
413
414 if ( count( $taxonomies ) > 0 ) {
415 $tags['taxonomy'] = array();
416
417 foreach ( $taxonomies as $tax => $details ) {
418 $tags['taxonomy'][ '{taxonomy_' . $tax . '}' ] = sprintf(
419 /* translators: Taxonomy Name, Singular */
420 __( 'Taxonomy: %s: Hashtag Format', 'wp-to-buffer' ),
421 $details->labels->singular_name
422 );
423 }
424 }
425
426 /**
427 * Defines Dynamic Status Tags that can be inserted into status(es) for the given Post Type.
428 * These tags are also added to any 'Insert Tag' dropdowns.
429 *
430 * @since 3.0.0
431 *
432 * @param array $tags Dynamic Status Tags.
433 * @param string $post_type Post Type.
434 */
435 $tags = apply_filters( $this->base->plugin->filter_name . '_get_tags', $tags, $post_type );
436
437 // Return filtered results.
438 return $tags;
439
440 }
441
442 /**
443 * Helper method to retrieve available tags for status updates, in a flattened
444 * key/value array
445 *
446 * @since 4.5.7
447 *
448 * @param string $post_type Post Type.
449 * @return array Tags
450 */
451 public function get_tags_flat( $post_type ) {
452
453 $tags_flat = array();
454 foreach ( $this->get_tags( $post_type ) as $tag_group => $tag_group_tags ) {
455 foreach ( $tag_group_tags as $tag => $tag_attributes ) {
456 $tags_flat[] = array(
457 'key' => $tag,
458 'value' => $tag,
459 );
460 }
461 }
462
463 return $tags_flat;
464
465 }
466
467 /**
468 * Helper method to retrieve Post actions
469 *
470 * @since 3.0.0
471 *
472 * @return array Post Actions
473 */
474 public function get_post_actions() {
475
476 // Build post actions.
477 $actions = array(
478 'publish' => __( 'Publish', 'wp-to-buffer' ),
479 'update' => __( 'Update', 'wp-to-buffer' ),
480 );
481
482 /**
483 * Defines the Post actions which trigger status(es) to be sent to social media.
484 *
485 * @since 3.0.0
486 *
487 * @param array $actions Post Actions.
488 */
489 $actions = apply_filters( $this->base->plugin->filter_name . '_get_post_actions', $actions );
490
491 // Return filtered results.
492 return $actions;
493
494 }
495
496 /**
497 * Helper method to retrieve Post actions, with labels in the past tense.
498 *
499 * @since 3.7.2
500 *
501 * @return array Post Actions
502 */
503 public function get_post_actions_past_tense() {
504
505 // Build post actions.
506 $actions = array(
507 'publish' => __( 'Published', 'wp-to-buffer' ),
508 'update' => __( 'Updated', 'wp-to-buffer' ),
509 );
510
511 /**
512 * Defines the Post actions which trigger status(es) to be sent to social media,
513 * with labels set to the past tense.
514 *
515 * @since 3.0.0
516 *
517 * @param array $actions Post Actions.
518 */
519 $actions = apply_filters( $this->base->plugin->filter_name . '_get_post_actions_past_tense', $actions );
520
521 // Return filtered results.
522 return $actions;
523
524 }
525
526 /**
527 * Helper method to return template tags that cannot have a character limit applied to them.
528 *
529 * @since 3.7.8
530 *
531 * @return array Tags.
532 */
533 public function get_tags_excluded_from_character_limit() {
534
535 $tags = array(
536 'date',
537 'url',
538 'id',
539 'author_user_email',
540 'author_user_url',
541 );
542
543 /**
544 * Defines the tags that cannot have a character limit applied to them, as doing so would
545 * wrongly concatenate data (e.g. a URL would become malformed).
546 *
547 * @since 3.7.8
548 *
549 * @param array $tags Tags.
550 */
551 $tags = apply_filters( $this->base->plugin->filter_name . '_get_tags_excluded_from_character_limit', $tags );
552
553 // Return filtered results.
554 return $tags;
555
556 }
557
558 /**
559 * Helper method to retrieve transient expiration time
560 *
561 * @since 3.0.0
562 *
563 * @return int Expiration Time (seconds)
564 */
565 public function get_transient_expiration_time() {
566
567 // Set expiration time for all transients = 1 week.
568 $expiration_time = ( 7 * DAY_IN_SECONDS );
569
570 /**
571 * Defines the number of seconds before expiring transients.
572 *
573 * @since 3.0.0
574 *
575 * @param int $expiration_time Transient Expiration Time, in seconds.
576 */
577 $expiration_time = apply_filters( $this->base->plugin->filter_name . '_get_transient_expiration_time', $expiration_time );
578
579 // Return filtered results.
580 return $expiration_time;
581
582 }
583
584 /**
585 * Defines the registered filters that can be used on the Log WP_List_Table
586 *
587 * @since 3.9.8
588 *
589 * @return array Filters
590 */
591 public function get_log_filters() {
592
593 // Define filters.
594 $filters = array(
595 'action',
596 'profile_id',
597 'result',
598 'request_sent_start_date',
599 'request_sent_end_date',
600 'orderby',
601 'order',
602 );
603
604 /**
605 * Defines the registered filters that can be used on the Log WP_List_Tables.
606 *
607 * @since 3.9.8
608 *
609 * @param array $filters Filters.
610 */
611 $filters = apply_filters( $this->base->plugin->filter_name . '_get_log_filters', $filters );
612
613 // Return filtered results.
614 return $filters;
615
616 }
617
618 /**
619 * Helper method to check if the collation of the given table is correct
620 *
621 * @since 5.5.5
622 *
623 * @param string $table_name Table Name.
624 * @param string $required_collation Required Collation.
625 * @return bool
626 */
627 public function is_table_charset_and_collation_correct( $table_name = 'options', $required_collation = 'utf8mb4' ) {
628
629 global $wpdb;
630
631 $result = $wpdb->get_row(
632 "SHOW CREATE TABLE `{$wpdb->{$table_name}}`",
633 ARRAY_A
634 );
635
636 // If no result, return true, as we can't reliably tell if the collation is correct.
637 if ( ! array_key_exists( 'Create Table', $result ) || empty( $result['Create Table'] ) ) {
638 return true;
639 }
640
641 // Extract the default charset and collation from the create table SQL.
642 preg_match( '/DEFAULT CHARSET=([a-zA-Z0-9_]+)/i', $result['Create Table'], $default_charset );
643 preg_match( '/COLLATE=([a-zA-Z0-9_]+)/i', $result['Create Table'], $default_collation );
644
645 if ( strpos( $default_charset[1], $required_collation ) === false ) {
646 return false;
647 }
648 if ( strpos( $default_collation[1], $required_collation ) === false ) {
649 return false;
650 }
651
652 return true;
653
654 }
655
656 }
657