-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlemonade_sna_core_pinterest_functions.php
1288 lines (1128 loc) · 38.6 KB
/
lemonade_sna_core_pinterest_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin functions
*
* @package LemonadeSNAPinterest
* @since 1.0.0
*/
////////////////
//DB FUNCTIONS//
////////////////
/**
* Adds column into existing DB.
*
* Checks if column is not existing and adds then it into a DB.
*
* @since 2.0.0
* @global object $wpdb WordPress Database object.
*
* @param string $column_name A name for column to add.
* @param string $db A DB table name.
* @param string $column_attr The colum attributes.
* @return bool False if nothing has changed, true if the column has been added.
*/
function lemonade_sna_pinterest_add_column_to_db( $column_name, $db, $column_attr ) {
global $wpdb;
$result = false;
$column_name = esc_sql( $column_name );
$db = esc_sql( $db );
$column_attr = esc_sql( $column_attr );
$existing_columns = $wpdb->get_col( "show columns from `$db`" );
if( is_array( $existing_columns ) and !empty( $existing_columns ) ) {
foreach( $existing_columns as $col ) {
if( $col == $column_name ) {
return false; //Stop if the column name exists
}
}
}
$result = $wpdb->query( "ALTER TABLE `$db` ADD `$column_name` $column_attr" );
return $result;
}
////////////////////
//HELPER FUNCTIONS//
////////////////////
/**
* Validates a string from numbers only.
*
* Checks if a string contains digits only.
*
* @since 2.0.0
*
* @param string $num String to validate.
* @return bool If valid returns true. Otherwise false.
*/
function lemonade_sna_pinterest_numbers_only( $num ) {
return preg_match( '/^([0-9])+$/', $num );
}
/**
* Validates a string from digits and letters.
*
* Checks if a string contains digits and letters only.
*
* @since 2.0.0
*
* @param sring $str String to validate.
* @return bool If valid returns true. Otherwise false.
*/
function lemonade_sna_pinterest_numbers_letters_only( $str ) {
return preg_match( '/^[a-zA-Z\d]+$/', $str );
}
/**
* Validates time interval.
*
* Checks if a given time interval has a correct value: 'days' is a positive integer,
* 'hours' is a positive integer not more than 24, etc.
*
* @since 2.0.0
*
* @param string $type Type of the interval: 'days', 'hours', 'minutes', 'limit'.
* @param int $value Value of the interval.
* @return bool True if valid, false if invalid.
*/
function lemonade_sna_pinterest_validate_interval( $type, $value ) {
$valid = false;
switch( $type ) {
case 'days' :
case 'limit' :
$valid = (int)$value > 0 ? true : false;
break;
case 'hours' :
$valid = ( (int)$value > 0 && (int)$value <= 24 ) ? true : false;
break;
case 'minutes' :
$valid = ( (int)$value > 0 && (int)$value <= 60 ) ? true : false;
break;
}
return $valid;
}
/**
* Checks Internet connection.
*
* Checks if it is possible to connect to a host.
*
* @since 2.0.0
*
* @param string $host Host url (without http://) for test.
* @return bool True if connected, false if not connected.
*/
function lemonade_sna_pinterest_check_internet_connection( $host ) {
return ( bool ) @fsockopen( $host, 80, $i, $s, 5 );
}
/**
* Gets array of weekdays with names.
*
* Gets array of weekdays names numbered from 1 (Monday) to 7 (Sunday).
*
* @since 2.0.0
*
* @return array Weekdays with serial numbers and names.
*/
function lemonade_sna_pinterest_days_of_week() {
$days = array(
'1' => 'Monday',
'2' => 'Tuesday',
'3' => 'Wednesday',
'4' => 'Thursday',
'5' => 'Friday',
'6' => 'Saturday',
'0' => 'Sunday'
);
return $days;
}
///////////////////////////
//DATE AND TIME FUNCTIONS//
///////////////////////////
/**
* Converts given datetime.
*
* Converts given datetime into given format for given timezone.
*
* @since 2.0.0
*
* @link http://php.net/manual/en/datetime.formats.php
* @link http://php.net/manual/en/function.date.php
* @link http://php.net/manual/en/timezones.php
*
* @param string|int $datetime Datetime in the valid format. Default 0.
* @param string $format Datetime format for the output. Example: 'Y-m-d'. Default 'Y-m-d H:i:s'.
* @param string $tz Timezone formatted like like 'Africa/Abidjan' or '-11:30'. Default 'UTC'.
*/
function lemonade_sna_pinterest_get_date_or_time_with_timezone( $datetime = 0, $format = 'Y-m-d H:i:s', $tz = 'UTC' ) {
if( empty( $datetime ) ) { //Get datetime object
$str_datetime = new DateTime();
} else {
try {
$str_datetime = new DateTime( $datetime );
} catch( Exception $e ) {
return false;
}
}
if( !empty( $tz ) ) {
try {
$c_tz = new DateTimeZone( $tz );
$str_datetime->setTimeZone( $c_tz );
} catch( Exception $e ) {
if( empty( $datetime ) ) {
$result = time() + $tz * 3600;
} else {
$result = strtotime( $datetime ) + $tz * 3600;
}
return @date( $format, $result );
}
}
return $str_datetime->format( $format );
}
//////////////
//SHORTCODES//
//////////////
/**
* Gets available shortcodes for posts templates.
*
* Gets array of shortcodes, which can be used in posts templates, and its descriptions.
*
* @since 2.0.0
*
* @param string $special Optional. Allows to get recommended shortcodes for special template fields. Possible values are: 'link', 'image_url', 'og_url', 'og_title', 'og_site_name', 'og_image', 'article_author', 'article_published_time'.
* @return array Array of shortcodes.
*/
function lemonade_sna_pinterest_built_in_shortcodes( $special = '' ) {
//Array of built-in shortcodes
$shortcodes = array(
'url' => array( 'desc' => __( 'Url of the post.', 'lemonade_sna' ), 'special' => array( 'link', 'og_url' ) ),
'slug' => array( 'desc' => __( 'Slug of the posts URL.', 'lemonade_sna' ), 'special' => array( 'link', 'og_url' ) ),
'pid'=> array( 'desc' => __( 'Post ID.', 'lemonade_sna' ) ),
'title' => array( 'desc' => __( 'Post title.', 'lemonade_sna' ), 'special' => array( 'og_title' ) ),
'content' => array( 'desc' => __( 'Processed post content.', 'lemonade_sna' ) ),
'announce' => array( 'desc' => __( 'Processed content of the post till the <!–more–> tag. If the tag is missed, than cuts after the last word in a segment limited by amount of symbols, which you can set up.', 'lemonade_sna' ) ),
'excerpt' => array( 'desc' => __( 'Processed excerpt of the post.', 'lemonade_sna' ) ),
'featured_img' => array( 'desc' => __( 'Url to featured image.', 'lemonade_sna' ), 'special' => array( 'image_url', 'og_image' ) ),
'tags' => array( 'desc' => __( 'Tags of the post.', 'lemonade_sna' ) ),
'categories' => array( 'desc' => __( 'Categories of the post.', 'lemonade_sna' ) ),
'author_display' => array( 'desc' => __( 'Post author display name.', 'lemonade_sna' ), 'special' => array( 'article_author' ) ),
'author_login' => array( 'desc' => __( 'Post author login.', 'lemonade_sna' ), 'special' => array( 'article_author' ) ),
'author_email' => array( 'desc' => __( 'Post author email.', 'lemonade_sna' ) ),
'post_date' => array( 'desc' => __( 'Date when the post was published converted to the site local timezone.', 'lemonade_sna' ), 'special' => array( 'article_published_time' ) ),
'post_date_gmt' => array( 'desc' => __( 'Date when the post was published converted to GMT timezone.', 'lemonade_sna' ), 'special' => array( 'article_published_time' ) ),
'post_modified' => array( 'desc' => __( 'Date when the post was last modified converted to the site local timezone.', 'lemonade_sna' ), 'special' => array( 'article_published_time' ) ),
'post_modified_gmt' => array( 'desc' => __( 'Date when the post was last modified converted to GMT timezone.', 'lemonade_sna' ), 'special' => array( 'article_published_time' ) ),
'post_type' => array( 'desc' => __( 'Post type.', 'lemonade_sna' ) ),
'post_mime_type' => array( 'desc' => __( 'Post mime type.', 'lemonade_sna' ) ),
'comment_count' => array( 'desc' => __( 'Number of comments.', 'lemonade_sna' ) ),
'site_name' => array( 'desc' => __( 'Site name.', 'lemonade_sna' ), 'special' => array( 'og_site_name' ) ),
'site_url' => array( 'desc' => __( 'Site url.', 'lemonade_sna' ), 'special' => array( 'link', 'og_url' ) ),
'site_desc' => array( 'desc' => __( 'Site description.', 'lemonade_sna' ) ),
);
if( $special != '' ) { //Select shortcodes which satisfies $special parameter
foreach( $shortcodes as $key => $shortcode ) {
if( !empty( $shortcode['special'] ) && in_array( $special, $shortcode['special'] ) ) {
$shortcodes_to_return[$key] = $shortcode;
}
}
} else {
$shortcodes_to_return = $shortcodes;
}
return $shortcodes_to_return;
}
/**
* Interprets shortcodes.
*
* For each post places a corresponding string instead of a given shortcode.
*
* @since 2.0.0
*
* @param string $shortcode A shortcode for interpretation.
* @param int $post_id Post ID.
* @param string $sep Optional. Separator between tags, categories, etc., if there are more than one item in a list. Default ' '.
* @param int $limit Optional. Limit of symbols for interpretation of '%announce%' shortcode. Default 200.
* @param bool $split Optional. If it is true, then for shortcodes with the '#' at the beginning, like '#%tags%', for each item which contains more than one word before each word the '#' hashtag will be added. Otherwise all the words will be glued together, like '#MySuperHasgtag'. Default true.
* @return string Interpreted shortcode or empty value if Post ID is incorrect.
*/
function lemonade_sna_pinterest_shortcodes_interp( $shortcode, $post_id, $sep = ' ', $limit = 200, $split = true ) {
//Validate function params
$post_id = (int)$post_id;
$sep = esc_attr( $sep );
$limit = (int)$limit;
$split = (bool)$split;
$post = get_post( $post_id ); //Get post object
if( null === $post ){
return '';
}
if( empty( $sep ) ) {
$sep = ' ';
}
$return = '';
switch( $shortcode ) {
case '%url%' :
$return = get_permalink( $post_id );
if( false === $return ) {
$return = '';
}
break;
case '%slug%' :
$return = sanitize_title( $post->post_name );
break;
case '%pid%' :
$return = (int)$post_id;
break;
case '#%title%' :
$title = $post->post_title;
if( !empty( $title ) ) {
if( str_word_count( $title ) > 1 ) { //If a title contains more than one word
$title = str_word_count( $title, 1 );
foreach( $title as $word ) {
if( $split === true ) {
$return .= ' #' . $word;
} else {
$return .= ucfirst( $word );
}
}
if( strstr( $return, '#' ) === false ) {
$return = '#' . $return;
}
$return = trim( $return );
} else {
$return = '#' . $title ;
}
}
break;
case '%title%' :
$return = $post->post_title;
break;
case '#%content%' :
$content = strip_shortcodes( wp_strip_all_tags( $post->post_content ) ); //Strip all shortcodes and tags from the content
$content = preg_replace( '!\s+!', " ", preg_replace( '/\r|\n/', " ", str_replace( ']]>', ']]>', $content ) ) ); //Replace all line breaks, multi spaces and CDATA close tags
if( !empty( $content ) ) {
if( str_word_count( $content ) > 1 ) {
$content = str_word_count( $content, 1 );
foreach( $content as $word ) {
if( $split === true ) {
$return .= ' #' . $word;
} else {
$return .= ucfirst( $word ); //Create one hashtag like '#MySuperLongHashtag' from all the content
}
}
if( strstr( $return, '#' ) === false ) {
$return = '#' . $return;
}
$return = trim( $return );
} else {
$return = '#' . $content;
}
}
break;
case '%content%' :
$content = strip_shortcodes( wp_strip_all_tags( $post->post_content ) );
$return = preg_replace( '!\s+!', " ", preg_replace( '/\r|\n/', " ", str_replace( ']]>', ']]>', $content ) ) );
break;
case '#%announce%' :
$content = $post->post_content;
if( !empty( $content ) ) {
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { //Check if there is <<!--more-->> tag
$content = explode( $matches[0], $content, 2 );
$return = preg_replace( '!\s+!', " ", preg_replace( '/\r|\n/', " ", str_replace( ']]>', ']]>', strip_shortcodes( wp_strip_all_tags( $content[0] ) ) ) ) ); //Process the content before the More tag
} else {
$content = preg_replace( '!\s+!', " ", preg_replace( '/\r|\n/', " ", str_replace( ']]>', ']]>', strip_shortcodes( wp_strip_all_tags( $content ) ) ) ) ); //Process the content
$str_len = strlen( $content );
if( $str_len <= $limit ) { //If a length of the content is less then setted up with $limit parameter
$return = $content;
} else {
$limit = $limit - 1;
$where_cut = strrpos( $content, ' ', $limit - $str_len ); //Find a position after the last word in the segment of symbols limited with $limit parameter
if( $where_cut !== false ) {
$return = substr( $content, 0, $where_cut ) . '...';
} else {
$return = substr( $content, 0, $limit ) . '...';
}
}
}
}
if( !empty( $return ) ) {
if( str_word_count( $return ) > 1 ) { //If the processed announce contains more than 1 word
$words = str_word_count( $return, 1 );
$string = '';
foreach( $words as $word ) {
if( $split === true ) {
$string .= ' #' . $word;
} else {
$string .= ucfirst( $word );
}
}
if( false === strpos( $string, '#' ) ) {
$string = '#' . $string;
}
$return = trim( $string );
} else {
$return = '#' . $return;
}
}
break;
case '%announce%' :
$content = $post->post_content;
$return = '';
if( !empty( $content ) ) {
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
$content = explode( $matches[0], $content, 2 );
$return = preg_replace( '!\s+!', " ", preg_replace( '/\r|\n/', " ", str_replace( ']]>', ']]>', strip_shortcodes( wp_strip_all_tags( $content[0] ) ) ) ) );
} else {
$content = preg_replace( '!\s+!', " ", preg_replace( '/\r|\n/', " ", str_replace( ']]>', ']]>', strip_shortcodes( wp_strip_all_tags( $content ) ) ) ) );
$str_len = strlen( $content );
if( $str_len < $limit ) {
$return = $content;
} else {
$limit = $limit - 1;
$where_cut = strrpos( $content, ' ', $limit - $str_len );
if( $where_cut !== false ) {
$return = substr( $content, 0, $where_cut ) . '...';
} else {
$return = substr( $content, 0, $limit ) . '...';
}
}
}
}
break;
case '#%excerpt%' :
$excerpt = $post->post_excerpt;
if( !empty( $excerpt ) ) {
if( str_word_count( $excerpt ) > 1 ) {
$excerpt = str_word_count( $excerpt, 1 );
foreach( $excerpt as $word ) {
if( $split === true ) {
$return .= ' #' . $word;
} else {
$return .= ucfirst( $word );
}
}
if( strstr( $return, '#' ) === false ) {
$return = '#' . $return;
}
$return = trim( $return );
} else {
$return = '#' . $excerpt;
}
}
break;
case '%excerpt%' :
$return = get_the_excerpt( $post );
break;
case '%featured_img%' :
$return = get_the_post_thumbnail_url( $post );
if( !$return ) {
$return = '';
}
break;
case '#%tags%' :
$tags = wp_get_post_tags( $post_id, array( 'fields' => 'names' ) );
if( !empty( $tags ) ) {
array_walk(
$tags,
function( &$i, $key, $split ) {
if( str_word_count( $i ) > 1 ) {
$tr = str_word_count( $i, 1 );
$str = '';
foreach( $tr as $k => $t ) {
if( $split === true ) {
$str .= ' #' . $t;
} else {
$str .= ucfirst( $t );
}
}
if( false === strpos( $str, '#' ) ) {
$str = '#' . $str;
}
$i = trim( $str );
} else {
$i = '#' . $i;
}
},
$split
);
$return = trim( implode( $sep, $tags ), $sep );
}
break;
case '%tags%' :
$tags = wp_get_post_tags( $post_id, array( 'fields' => 'names' ) );
if( !empty( $tags ) ) {
array_walk(
$tags,
function( &$i, $key, $split ) {
if( str_word_count( $i ) > 1 ) {
$tr = str_word_count( $i, 1 );
$str = '';
foreach( $tr as $k => $t ) {
if( $split === true ) {
$str .= ' ' . $t;
} else {
$str .= ucfirst( $t );
}
}
$i = trim( $str );
}
},
$split
);
$return = trim( implode( $sep, $tags ), $sep );
}
break;
case '#%categories%' :
$categories = wp_get_post_categories( $post_id, array( 'fields' => 'names' ) );
if( !empty( $categories ) ) {
array_walk( $categories,
function( &$i, $key, $split ) {
if( str_word_count( $i ) > 1 ) {
$tr = str_word_count( $i, 1 );
$str = '';
foreach( $tr as $t ) {
if( $split === true ) {
$str .= ' #' . $t;
} else {
$str .= ucfirst( $t );
}
}
if( false === strpos( $str, '#' ) ) {
$str = '#' . $str;
}
$i = trim( $str );
} else {
$i = '#' . $i;
}
},
$split
);
$return = trim( implode( $sep, $categories ), $sep );
}
break;
case '%categories%' :
$categories = wp_get_post_categories( $post_id, array( 'fields' => 'names' ) );
if( !empty( $categories ) ) {
array_walk( $categories,
function( &$i, $key, $split ) {
if( str_word_count( $i ) > 1 ) {
$tr = str_word_count( $i, 1 );
$str = '';
foreach( $tr as $k => $t ) {
if( $split === true ) {
$str .= ' ' . $t;
} else {
$str .= ucfirst( $t );
}
}
$i = trim( $str );
}
},
$split
);
$return = trim( implode( $sep, $categories ), $sep );
}
break;
case '#%author_display%' :
$author_id = $post->post_author;
$author = get_the_author_meta( 'display_name', $author_id );
if( !empty( $author ) ) {
if( str_word_count( $author ) > 1 ) {
$author = str_word_count( $author, 1 );
foreach( $author as $word ) {
if( $split === true ) {
$return .= ' #' . $word;
} else {
$return .= ucfirst( $word );
}
}
if( strstr( $return, '#' ) === false ) {
$return = '#' . $return;
}
$return = trim( $return );
} else {
$return = '#' . $author;
}
}
break;
case '%author_display%' :
$author_id = $post->post_author;
$return = get_the_author_meta( 'display_name', $author_id );
if( !empty( $author ) ) {
if( str_word_count( $author ) > 1 ) {
$author = str_word_count( $author, 1 );
foreach( $author as $word ) {
if( $split === true ) {
$return .= ' ' . $word;
} else {
$return .= ucfirst( $word );
}
}
$return = trim( $return );
}
}
break;
case '%author_login%' :
$author_id = $post->post_author;
$return = get_the_author_meta( 'user_login', $author_id );
break;
case '%author_email%' :
$author_id = $post->post_author;
$return = get_the_author_meta( 'user_email', $author_id );
break;
case '%post_date%' :
$date = date( 'Y-m-d', strtotime( $post->post_date ) );
/**
* Filters date output.
*
* Filters a returned by a shortcode date output.
*
* @since 2.0.0
*
* @param string $date Date string formatted '0000-00-00'.
*/
$return = apply_filters( 'lemonade_sna_pinterest_shortcode_date_output', $date );
break;
case '%post_date_gmt%' :
$date = date( 'Y-m-d', strtotime( $post->post_date_gmt ) );
/** This filter is documented in lemonade_sna_core_pinterest_functions.php */
$return = apply_filters( 'lemonade_sna_pinterest_shortcode_date_output', $date );
break;
case '%post_modified%' :
if( !empty( $post->post_modified ) ) {
$date = date( 'Y-m-d', strtotime( $post->post_modified ) );
/** This filter is documented in lemonade_sna_core_pinterest_functions.php */
$return = apply_filters( 'lemonade_sna_pinterest_shortcode_date_output', $date );
}
break;
case '%post_modified_gmt%' :
if( !empty( $post->post_modified_gmt ) ) {
$date = date( 'Y-m-d', strtotime( $post->post_modified_gmt ) );
/** This filter is documented in lemonade_sna_core_pinterest_functions.php */
$return = apply_filters( 'lemonade_sna_pinterest_shortcode_date_output', $date );
}
break;
case '%post_type%' :
$post_type = get_post_type_object( $post->post_type );
if( null !== $post_type ) {
$return = $post_type->labels->singular_name;
}
break;
case '%post_mime_type%' :
$return = !empty( $post->post_mime_type ) ? $post->post_mime_type : '';
break;
case '%comment_count%' :
$return = !empty( $post->comment_count ) ? $post->comment_count : 0;
break;
case '%site_url%' :
$return = get_site_url();
break;
case '#%site_desc%' :
$desc = get_bloginfo( 'description' );
if( !empty( $desc ) ) {
if( str_word_count( $desc ) > 1 ) {
$desc = str_word_count( $desc, 1 );
foreach( $desc as $word ) {
if( $split === true ){
$return .= ' #' . $word;
} else {
$return .= ucfirst( $word );
}
}
if( strstr( $return, '#' ) === false ) {
$return = '#' . $return;
}
$return = trim( $return );
} else {
$return = '#' . $desc;
}
}
break;
case '%site_desc%' :
$return = get_bloginfo( 'description' );
break;
case '#%site_name%' :
$name = get_bloginfo( 'name' );
if( !empty( $name ) ) {
if( str_word_count( $name ) > 1 ) {
$name = str_word_count( $name, 1 );
foreach( $name as $word ) {
if( $split === true ){
$return .= ' #' . $word;
} else {
$return .= ucfirst( $word );
}
}
if( strstr( $return, '#' ) === false ) {
$return = '#' . $return;
}
$return = trim( $return );
} else {
$return = '#' . $name;
}
}
break;
case '%site_name%' :
$return = get_bloginfo( 'name' );
break;
default :
$return = $shortcode;
break;
}
$return = esc_html( $return ); //Add sanitization for output
/**
* Fires after shortcode was interpreted.
*
* @since 2.0.0
*
* @param string $shortcode Shortcode.
* @param int $post_id Post ID.
* @param string $sep Separator between shorcodes replacements, like post tags, which contain more than one item.
* @param int $limit Limit of announce (ammount of symbols).
* @param bool $split If it is true, then for shortcodes with the '#' at the beginning, like '#%tags%', for each item which contains more than one word before each word the '#' hashtag will be added. Otherwise all the words will be glued together, like '#MySuperHasgtag'.
* @param string $return Interpreted shortcode.
*/
do_action( 'lemonade_sna_pinterest_after_shortcode_interpretation', $shortcode, $post_id, $sep, $limit, $split, $return );
/**
* Filters interpreted shortcode.
*
* Filters the result of shortcode interpretation.
*
* @since 2.0.0
*
* @param string $return Interpreted shortcode.
* @param string $shortcode Shortcode.
* @param int $post_id Post ID.
* @param string $sep Separator between shorcodes replacements, like post tags, which contain more than one item.
* @param int $limit Limit of announce (ammount of symbols).
* @param bool $split If it is true, then for shortcodes with the '#' at the beginning, like '#%tags%', for each item which contains more than one word before each word the '#' hashtag will be added. Otherwise all the words will be glued together, like '#MySuperHasgtag'.
*/
apply_filters( 'lemonade_sna_pinterest_filter_shortcode_interpretation', $return, $shortcode, $post_id, $sep, $limit, $split );
return $return;
}
/**
* Replaces shotcodes in a given string.
*
* Changes all known shortcodes in a string to their interpretation.
*
* @since 2.0.0
*
* @param string $string A string with shortcodes.
* @param int $post_id Post ID.
* @param string $sep Optional. Separator between tags, categories, etc., if there are more than one item in a list. Default ' '.
* @param int $limit Optional. Limit of symbols for interpretation of '%announce%' shortcode. Default 200.
* @param bool $split Optional. If it is true, then for shortcodes with the '#' at the beginning, like '#%tags%', for each item which contains more than one word before each word the '#' hashtag will be added. Otherwise all the words will be glued together, like '#MySuperHasgtag'. Default true.
* @return string The string with replaced shortcodes.
*/
function lemonade_sna_pinterest_shortcodes( $string, $post_id, $sep = ' ', $limit = 200, $split = true ) {
$search = array();
$search2 = array();
$replace = array();
$search[] = '#%title%';
$search[] = '#%content%';
$search[] = '#%announce%';
$search[] = '#%excerpt%';
$search[] = '#%site_desc%';
$search[] = '#%author_display%';
foreach( array_keys( lemonade_sna_pinterest_built_in_shortcodes() ) as $item ) {
$search[] = '#%' . $item . '%';
$search[] = '%' . $item . '%';
}
foreach( $search as $to_replace ) {
if( stripos( $string, $to_replace ) !== false ) {
$search2[] = $to_replace;
$replace[] = lemonade_sna_pinterest_shortcodes_interp( $to_replace, $post_id, $sep, $limit, $split );
}
}
return str_replace( $search2, $replace, $string );
}
//////////////////////
//TEMPLATE FUNCTIONS//
//////////////////////
/**
* Gets an Lemonade SNA Autoposter template.
*
* Gets an Lemonade SNA Autoposter template for a given ID.
*
* @since 2.0.0
*
* @global object $wpdb WPDB object.
*
* @param int $id Template ID.
* @return object|null Autoposter template object from DB or null if nothing found.
*/
function lemonade_sna_pinterest_get_autoposter_template( $id ) {
global $wpdb;
$table = $wpdb->prefix . 'lemonade_autoposter_templates';
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE `id`=%d", $id ) );
}
/**
* Gets a list of Autoposter templates.
*
* Gets a list of templates from the DB, for a special network, if setted up.
*
* @since 2.0.0
*
* @global object $wpdb WPDB object.
*
* @param string $network Optional. A network name. Available: 'Pinterest'.
* @return array|null Array of templates or null if MySql query was incorrect.
*/
function lemonade_sna_pinterest_get_list_of_templates( $network = '' ) {
global $wpdb;
$table = $wpdb->prefix . 'lemonade_autoposter_templates';
$select = "SELECT * FROM $table";
if( !empty($network ) ) {
$select .= " WHERE network=%s";
$templates = $wpdb->get_results( $wpdb->prepare( $select, $network ) );
} else {
$templates = $wpdb->get_results( $select );
}
return $templates;
}
/**
* Gets a list of active templates.
*
* Gets templates which are active at the moment from the DB.
*
* @since 2.0.0
*
* @global object $wpdb WPDB object.
*
* @param string $network Optional. A network for which to check. Available: 'Pinterest'.
* @return array|null Array of active templates or null if MySql query is incorrect.
*/
function lemonade_sna_pinterest_get_list_of_active_templates( $network = '' ) {
global $wpdb;
$table = $wpdb->prefix . 'lemonade_autoposter_templates';
$select = "SELECT * FROM $table WHERE is_active=%d";
$prepare[] = 1;
if( !empty( $network ) ) {
$select .= " AND network=%s";
$prepare[] = $network;
}
$templates = $wpdb->get_results( $wpdb->prepare( $select, $prepare ) );
return $templates;
}
/**
* Gets a list of inactive templates.
*
* Selects from the DB templates which are not active at the moment.
*
* @since 2.0.0
*
* @global object $wpdb WPDB object.
*
* @param string $network A social network. Available: 'Pinterest'.
* @return array|null Array of inactive templates entries or null if MySql query is incorrect
*/
function lemonade_sna_pinterest_get_list_of_unactive_templates( $network = '' ) {
global $wpdb;
$table = $wpdb->prefix . 'lemonade_autoposter_templates';
$query = "SELECT * FROM $table WHERE NOT `is_active`=1";
if( empty( $network ) ) {
$templates = $wpdb->get_results( $query );
} else {
$query .= " AND network=%s";
$templates = $wpdb->get_results( $wpdb->prepare( $query, $network ) );
}
return $templates;
}
/**
* Deletes template.
*
* Deletes a template with given ID.
*
* @since 2.0.0
*
* @global object $wpdb WPDB object.
*
* @param int $templ_id ID of a template to delete.
* @return bool True if deleted or false if an error occured.
*/
function lemonade_sna_pinterest_delete_template( $templ_id ) {
global $wpdb;
$lines = get_option( 'lemonade_sna_pinterest_autoposter_lines' );
if( !empty( $lines[$templ_id] ) ) {
unset( $lines[$templ_id] );
update_option( 'lemonade_sna_pinterest_autoposter_lines', $lines );
}
$posted_table = $wpdb->prefix . 'lemonade_autoposter_posts_published';
$posted = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM $posted_table WHERE template_id=%d", $templ_id ) );
$update = true;
if( NULL !== $posted && !empty( $posted ) ) {
$update = $wpdb->update(
$posted_table,
array(
'template_id' => 0
),
array(
'template_id' => $templ_id
),
array(
'%d'
),
array(
'%d'
)
);
}
if( false !== $update ) {
$templates_table = $wpdb->prefix . 'lemonade_autoposter_templates';
$template = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM $templates_table WHERE id=%d", $templ_id ) );
if( NULL === $template ) {
return false;
}
$delete = $wpdb->delete(
$templates_table,
array(
'id' => $templ_id
),
array(
'%d'
)
);
if( false === $delete ) {
return false;
}
} else {
return false;
}
/**
* Fires after Autoposter template was deleted from the Data Base.
*
* @since 2.0.0
*
* @param int $templ_id ID of the template which was deleted.
*/
do_action( 'lemonade_sna_pinterest_after_autoposter_template_deleted', $templ_id );
return true;
}
/**
* Gets posts for an Autoposter template.
*
* Gets ids of all the posts which correspond to special rules setted up in an Autoposter template.
*
* @since 2.0.0
*