-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwidget.php
2170 lines (1937 loc) · 86.3 KB
/
widget.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
/**
* Genesis Sandbox Featured Post Widget Classes
*
* @category Genesis_Sandbox
* @package Widgets
* @author Travis Smith
* @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
* @link http://wpsmith.net/
* @since 1.1.0
*/
/** Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) exit( 'Cheatin’ uh?' );
/**
* Genesis Sandbox Featured Post widget class.
*
* @since 0.1.8
*
* @category Genesis_Sandbox
* @package Widgets
*/
if ( ! class_exists( 'GS_Featured_Content' ) ) {
class GS_Featured_Content extends WP_Widget {
/**
* Holds a copy of the object for easy reference.
*
* @since 1.0.0
*
* @var object
*/
public static $widget_instance = array();
public static $base = 'featured-content';
public static $self;
/**
* Holds widget settings defaults, populated in constructor.
*
* @var array
*/
protected $defaults;
/**
* Constructor. Set the default widget options and create widget.
*
* @since 0.1.8
*/
function __construct() {
GS_Featured_Content::$self = $this;
$gfwa = genesis_get_option( 'gsfc_gfwa' );
if ( $gfwa )
GS_Featured_Content::$base = 'featured-post';
$this->defaults = apply_filters(
'gsfc_defaults',
array(
'add_column_classes' => 0,
'archive_link' => '',
'byline_position' => 'after-title',
'class' => '',
'column_classes' => '',
'content_limit' => '',
'count' => 0,
'custom_field' => '',
'delete_transients' => 0,
'excerpt_cutoff' => '…',
'excerpt_limit' => 55,
'exclude_cat' => '',
'exclude_displayed' => 0,
'exclude_terms' => '',
'extra_format' => 'ul',
'extra_num' => 3,
'extra_posts' => '',
'extra_title' => '',
'gravatar_alignment' => '',
'gravatar_size' => '',
'image_alignment' => '',
'image_position' => 'before-title',
'image_size' => '',
'include_exclude' => '',
'link_gravatar' => 0,
'link_image' => 1,
'link_image_field' => '',
'link_title' => 1,
'link_title_field' => '',
'meta_key' => '',
'more_from_category' => '',
'more_from_category_text' => __( 'More Posts from this Category', 'gsfc' ),
'more_text' => __( '[Read More...]', 'gsfc' ),
'optimize' => 0,
'order' => '',
'orderby' => '',
'page_id' => '',
'paged' => '',
'post_align' => '',
'post_id' => '',
'post_info' => '[post_date] ' . __( 'By', 'gsfc' ) . ' [post_author_posts_link] [post_comments]',
'post_meta' => '[post_categories] [post_tags]',
'post_type' => 'post',
'posts_cat' => '',
'posts_num' => 1,
'posts_offset' => 0,
'posts_term' => '',
'show_archive_line' => 0,
'show_byline' => 0,
'show_content' => '',
'show_gravatar' => 0,
'show_image' => 0,
'show_paged' => '',
'show_sticky' => '',
'show_title' => 0,
'title' => '',
'title_cutoff' => '…',
'title_limit' => '',
'transients_time' => 86400,
'widget_title_link' => 0,
'widget_title_link_href' => '',
)
);
$widget_ops = array(
'classname' => 'featured-content',
'description' => __( 'Displays featured posts with thumbnails', 'gsfc' ),
);
$control_ops = array(
'id_base' => 'featured-content',
'width' => 505,
'height' => 350,
);
$name = __( 'Genesis Sandbox', 'gsfc' );
if ( defined( 'CHILD_NAME' ) && true === apply_filters( 'gsfc_widget_name', false ) )
$name = CHILD_THEME_NAME;
elseif ( apply_filters( 'gsfc_widget_name', false ) )
$name = apply_filters( 'gsfc_widget_name', false );
parent::__construct( 'featured-content', sprintf( __( '%s - Featured Content', 'gsfc' ), $name ), $widget_ops, $control_ops );
GS_Featured_Content::add();
do_action( 'gsfc_actions', $this );
}
/**
* Adds all Widget's Actions at once for easy removal.
*/
public static function add() {
$self = GS_Featured_Content::$self;
//* Form Fields
add_action( 'gsfc_output_form_fields', array( 'GS_Featured_Content', 'do_form_fields' ), 10, 2 );
//* Post Class
add_filter( 'post_class', array( 'GS_Featured_Content', 'post_class' ) );
//* Excerpts
add_filter( 'excerpt_length', array( 'GS_Featured_Content', 'excerpt_length' ) );
add_filter( 'excerpt_more', array( 'GS_Featured_Content', 'excerpt_more' ) );
//* Do Post Image
add_filter( 'genesis_attr_gsfc-entry-image-widget', array( 'GS_Featured_Content', 'attributes_gsfc_entry_image_widget' ) );
add_action( 'gsfc_before_post_content', array( 'GS_Featured_Content', 'do_post_image' ), 4 );
add_action( 'gsfc_post_content', array( 'GS_Featured_Content', 'do_post_image' ) );
add_action( 'gsfc_after_post_content', array( 'GS_Featured_Content', 'do_post_image' ) );
//* Do before widget post content
add_action( 'gsfc_before_post_content', array( 'GS_Featured_Content', 'do_gravatar' ), 4 );
add_action( 'gsfc_before_post_content', array( 'GS_Featured_Content', 'do_post_title' ) );
//* Maybe Linkify Widget Title
add_action( 'gsfc_widget_title', array( $self, 'widget_title' ), 999, 3 );
//* Do Post Info By Line
add_action( 'gsfc_before_post_content', array( 'GS_Featured_Content', 'do_byline' ), 5 );
add_action( 'gsfc_post_content', array( 'GS_Featured_Content', 'do_byline' ), 2 );
add_action( 'gsfc_after_post_content', array( 'GS_Featured_Content', 'do_byline' ) );
//* Do widget post content
add_action( 'gsfc_post_content', array( 'GS_Featured_Content', 'do_post_content' ) );
//* Do after widget post content
add_action( 'gsfc_after_post_content', array( 'GS_Featured_Content', 'do_post_meta' ) );
//* Do after loop
add_action( 'gsfc_endwhile', array( 'GS_Featured_Content', 'do_posts_nav' ) );
//* Do after loop reset
add_action( 'gsfc_after_loop_reset', array( 'GS_Featured_Content', 'do_extra_posts' ) );
add_action( 'gsfc_after_loop_reset', array( 'GS_Featured_Content', 'do_more_from_category' ) );
//* Admin Scripts
add_action( 'admin_enqueue_scripts', array( 'GS_Featured_Content', 'admin_scripts' ) );
add_action( 'admin_print_footer_scripts', array( 'GS_Featured_Content', 'admin_footer_script' ) );
//* Frontend Scripts
add_action( 'gsfc_before_widget', array( 'GS_Featured_Content', 'enqueue_style' ) );
}
/**
* Whether current admin page is the widgets page.
*/
public static function is_widgets_page() {
if ( ! is_admin() ) return false;
$screen = get_current_screen();
if ( 'widgets' != $screen->base && 'widgets' != $screen->id ) return false;
return true;
}
/**
* Filters excerpt's more text.
*
* @param string $more_text Current excerpt more text.
* @return string Maybe modified more text.
*/
public static function excerpt_more( $more_text ) {
if ( isset( GS_Featured_Content::$widget_instance['more_text'] ) && GS_Featured_Content::$widget_instance['more_text'] ) {
return sprintf( '<a rel="nofollow" class="more-link" href="%s">%s</a>', get_permalink(), GS_Featured_Content::$widget_instance['more_text'], GS_Featured_Content::$widget_instance['more_text'] );
}
return $more_text;
}
/**
* Sets excerpt length.
*
* @param int $length Current excerpt length.
* @return int Maybe new excerpt length.
*/
public static function excerpt_length( $length ) {
if ( GS_Featured_Content::has_value( 'excerpt_limit' ) && 0 != (int)GS_Featured_Content::$widget_instance['excerpt_limit'] )
return (int)GS_Featured_Content::$widget_instance['excerpt_limit'];
return $length;
}
/**
* Adds plugin CSS.
*/
public static function enqueue_style( $instance ) {
if ( is_admin() ) return;
if ( empty( $instance['add_column_classes'] ) ) return;
$suffix = ( defined( 'WP_DEBUG' ) || defined( 'SCRIPT_DEBUG' ) ) ? '.css' : '.min.css';
$deps = defined( 'CHILD_THEME_NAME' ) && CHILD_THEME_NAME ? sanitize_title_with_dashes( CHILD_THEME_NAME ) : 'child-theme';
wp_enqueue_style( 'gsfc-column-classes', plugins_url( GSFC_PLUGIN_NAME . '/css/column-classes' . $suffix ), array( $deps, ), GSFC_PLUGIN_VERSION );
}
/**
* Getter method for retrieving the object instance.
*
* @since 1.0.0
*/
public static function get_instance() {
return GS_Featured_Content::$widget_instance;
}
/**
* Determines whether $instance option isset & has a value
*
* @param string $opt Instance option.
*
* @return bool True if option has value
*/
protected static function has_value( $opt ) {
if( is_array( GS_Featured_Content::$widget_instance ) && isset( GS_Featured_Content::$widget_instance[ $opt ] ) && GS_Featured_Content::$widget_instance[ $opt ] )
return true;
return false;
}
/**
* Returns Column Class Number
*
* @param string $class Column Class.
*
* @return int Column Class Integer
*/
public static function get_col_class_num( $class ) {
switch( $class ) {
case 'one-half':
return 2;
case 'one-third':
case 'two-thirds':
return 3;
case 'one-fourth':
case 'three-fourths':
return 4;
case 'one-fifth':
case 'two-fifths':
case 'three-fifths':
case 'four-fifths':
return 5;
case 'one-sixth':
case 'five-sixths':
return 6;
default:
return 1;
}
}
/**
* Adds number class, and odd/even class to widget output
*
* @global integer $gs_counter
* @param array $classes Array of post classes.
* @return array $classes Modified array of post classes.
*/
public static function post_class( $classes ) {
global $gs_counter;
$classes[] = sprintf( 'gs-%s', $gs_counter + 1 );
$classes[] = $gs_counter + 1 & 1 ? 'gs-odd' : 'gs-even';
$classes[] = $gs_counter == 4 ? 'gs-odd' : 'gs-even';
$classes[] = 'gs-featured-content-entry';
//* First Class
if ( GS_Featured_Content::has_value( 'column_class' ) && ( 0 == $gs_counter || 0 == $gs_counter % GS_Featured_Content::get_col_class_num( GS_Featured_Content::$widget_instance['column_class'] ) ) )
$classes[] = 'first';
//* No BG Class
if ( GS_Featured_Content::has_value( 'use_icon' ) )
$classes[] = 'no-bg';
//* Custom Class
if ( GS_Featured_Content::has_value( 'class' ) )
$classes[] = GS_Featured_Content::$widget_instance['class'];
//* Column Class
if ( GS_Featured_Content::has_value( 'column_class' ) )
$classes[] = GS_Featured_Content::$widget_instance['column_class'];
//* Replace Genesis Widgets
if ( apply_filters( 'gs_replace_genesis', false ) )
$classes[] = 'featured-post';
return $classes;
}
/**
* Outputs post info/byline.
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_byline( $instance ) {
if ( empty( $instance['show_byline'] ) || empty( $instance['post_info'] ) ) {
return;
}
$byline = '';
if ( !empty( $instance['post_info'] ) ) {
$byline = sprintf( '<p class="byline post-info">%s</p>', do_shortcode( $instance['post_info'] ) );
}
GS_Featured_Content::maybe_echo( $instance, 'gsfc_before_post_content', 'byline_position', 'before-title', $byline );
GS_Featured_Content::maybe_echo( $instance, 'gsfc_post_content', 'byline_position', 'after-title', $byline );
}
/**
* Add attributes for entry image element shown in a widget.
*
* @since 2.0.0
*
* @global WP_Post $post Post object.
*
* @param array $attributes Existing attributes.
*
* @return array Amended attributes.
*/
public static function attributes_gsfc_entry_image_widget( $attributes ) {
global $post;
$attributes['class'] = sprintf( 'entry-image attachment-%s gsfc-%s', $post->post_type, $attributes['align'] );
unset( $attributes['align'] );
$attributes['itemprop'] = 'image';
return $attributes;
}
/**
* Inserts Post Image
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_post_image( $instance ) {
//* Bail if empty show param
if ( empty( $instance['show_image'] ) ) {
return;
}
$align = $instance['image_alignment'] ? esc_attr( $instance['image_alignment'] ) : 'alignnone';
$link = $instance['link_image_field'] ? $instance['link_image_field'] : get_permalink();
$link = '' !== genesis_get_custom_field( 'gsfc_link_image_field' ) ? genesis_get_custom_field( 'gsfc_link_image_field' ) : $link;
$image = genesis_get_image( array(
'format' => 'html',
'size' => $instance['image_size'],
'context' => 'featured-post-widget',
'attr' => genesis_parse_attr( 'gsfc-entry-image-widget', array( 'align' => $align, ) ),
) );
$image = $instance['link_image'] == 1 && ! empty( $image ) ? sprintf( '<a href="%s" title="%s" class="%s">%s</a>', $link, the_title_attribute( 'echo=0' ), $align, $image ) : $image;
GS_Featured_Content::maybe_echo( $instance, 'gsfc_before_post_content', 'image_position', 'before-title', $image );
GS_Featured_Content::maybe_echo( $instance, 'gsfc_post_content', 'image_position', 'after-title', $image );
GS_Featured_Content::maybe_echo( $instance, 'gsfc_after_post_content', 'image_position', 'after-content', $image );
}
/**
* Outputs content conditionally based on current filter
*
* @param string $action Action to output.
* @param string $param Instance choice.
* @param string $value Value of instance choice.
* @param mixed $content HTML content to output.
*/
public static function maybe_echo( $instance, $action, $param, $value, $content ) {
echo current_filter() == $action && $instance[ $param ] == $value ? $content : '';
}
/**
* Do action alias
*
* @param string $name Action name.
* @param array $instance The settings for the particular instance of the widget.
*/
public static function action( $name, $instance ) {
if ( 'gs_before_loop' == $name ) {
_deprecated_argument( 'GS_Featured_Content::action', '1.1.5', __( 'Please use gsfc_before_loop hook.','gsfc' ) );
}
do_action( $name, $instance );
}
/**
* Do widget framework.
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function framework( $instance ) {
global $gs_counter;
genesis_markup( array(
'html5' => '<article %s>',
'xhtml' => sprintf( '<div class="%s">', implode( ' ', get_post_class() ) ),
'context' => 'entry',
) );
GS_Featured_Content::action( 'gsfc_before_post_content', $instance );
GS_Featured_Content::action( 'gsfc_post_content', $instance );
GS_Featured_Content::action( 'gsfc_after_post_content', $instance );
$gs_counter++;
genesis_markup( array(
'html5' => '</article>',
'xhtml' => '</div>',
) );
}
/**
* Outputs Post Title if option is selected.
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_post_title( $instance ) {
//* Bail if empty show param
if ( empty( $instance['show_title'] ) ) return;
//* Custom Link or Permalink
$link = $instance['link_title'] && $instance['link_title_field'] && genesis_get_custom_field( 'link_title_field' ) ? genesis_get_custom_field( 'link_title_field' ) : get_permalink();
//* Add Link to Title?
$wrap_open = $instance['link_title'] == 1 ? sprintf( '<a href="%s" title="%s">', $link, the_title_attribute( 'echo=0' ) ) : '';
$wrap_close = $instance['link_title'] == 1 ? '</a>' : '';
if ( ! empty( $instance['title_limit'] ) )
$title = genesis_truncate_phrase( the_title_attribute( 'echo=0' ) , $instance['title_limit'] ) . $instance['title_cutoff'];
else
$title = the_title_attribute( 'echo=0' );
if ( genesis_html5() ) {
$hclass = apply_filters( 'gsfc_entry_title_class', ' class="entry-title"' );
} else {
$hclass = '';
}
$pattern = apply_filters( 'gsfc_post_title_pattern', '<h2%s>%s%s%s</h2>' );
printf( $pattern, $hclass, $wrap_open, $title, $wrap_close );
}
/**
* Outputs the selected content option, if any.
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_post_content( $instance ) {
//* Bail if empty show param
if ( empty( $instance['show_content'] ) ) {
return;
}
if ( '' !== $instance['show_content'] && ( $pre = apply_filters( 'gsfc_post_content_add_entry_content', false ) ) ) {
echo '<div class="entry-content">';
}
switch ( $instance['show_content'] ) {
case 'excerpt':
add_filter( 'excerpt_more', array( 'GS_Featured_Content', 'excerpt_more' ) );
the_excerpt();
remove_filter( 'excerpt_more', array( 'GS_Featured_Content', 'excerpt_more' ) );
break;
case 'content-limit':
the_content_limit( ( int ) $instance['content_limit'], esc_html( $instance['more_text'] ) );
break;
case 'content':
the_content( esc_html( $instance['more_text'] ) );
break;
default:
do_action( 'gsfc_show_content' );
break;
}
if ( '' !== $instance['show_content'] && ( $pre = apply_filters( 'gsfc_post_content_add_entry_content', false ) ) ) {
echo '</div>';
}
}
/**
* Outputs post meta if option is selected and anything is in the post meta field.
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_post_meta( $instance ) {
if ( ! empty( $instance['show_archive_line'] ) && ! empty( $instance['post_meta'] ) )
printf( '<p class="post-meta">%s</p>', do_shortcode( $instance['post_meta'] ) );
}
/**
* Form submit script.
*/
public static function admin_footer_script() {
if ( ! GS_Featured_Content::is_widgets_page() ) return; ?>
<script type="text/javascript">
function gsfcSave(t) {
wpWidgets.save( jQuery(t).closest('div.widget'), 0, 1, 0 );
}
</script>
<?php
}
/**
* Form submit script.
*/
public static function admin_scripts() {
if ( ! GS_Featured_Content::is_widgets_page() ) return;
$min = ( defined( 'WP_DEBUG' ) || defined( 'SCRIPT_DEBUG' ) ) ? '.' : '.min.';
// wp_enqueue_script( 'gsfc-admin-widget', plugins_url( GSFC_PLUGIN_NAME . '/js/gsfc-admin' . $min . 'js' ), array( 'jquery', ), GSFC_PLUGIN_VERSION );
wp_enqueue_style( 'gsfc-admin-widget', plugins_url( GSFC_PLUGIN_NAME . '/css/gsfc-admin' . $min . 'css' ), null, GSFC_PLUGIN_VERSION );
}
/**
* Inserts Author Gravatar if option is selected
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_gravatar( $instance ) {
if ( ! empty( $instance['show_gravatar'] ) ) {
$tag = 'a';
switch( $instance['link_gravatar'] ) {
case 'archive' :
$before = 'href="'. get_author_posts_url( get_the_author_meta( 'ID' ) ) .'"';
break;
case 'website' :
$before = 'href="'. get_the_author_meta( 'user_url' ) .'"';
break;
default :
$before = '';
$tag = 'span';
break;
}
printf( '<%1$s %2$s class="%3$s">%4$s</%1$s>',
$tag,
$before,
esc_attr( $instance['gravatar_alignment'] ),
get_avatar( get_the_author_meta( 'ID' ),
$instance['gravatar_size'] )
);
}
}
/**
* The Posts Navigation/Pagination.
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_posts_nav( $instance ) {
if ( ! empty( $instance['show_paged'] ) )
genesis_posts_nav();
}
/**
* Sanitizes transient name (to less than 40 characters)
*
* @param string $name Transient name.
* @return string $name Maybe modified transient name.
*/
public static function sanitize_transient( $name ) {
if ( 40 < strlen( $name ) )
$name = substr( $name, 0, 40 );
return $name;
}
/**
* Gets transient with multisite support.
* Due to multisite support, forces name < 40 chars
*
* @param string $name Transient name.
*/
protected static function get_transient( $name ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG || apply_filters( 'gsfc_debug', false ) ) {
GS_Featured_Content::delete_transient( $name );
return false;
}
$name = GS_Featured_Content::sanitize_transient( $name );
return get_transient( $name );
}
/**
* WP.com's VIP get_term by. Get all Term data from database by Term field and data.
*
* Warning: $value is not escaped for 'name' $field. You must do it yourself, if
* required.
*
* The default $field is 'id', therefore it is possible to also use null for
* field, but not recommended that you do so.
*
* If $value does not exist, the return value will be false. If $taxonomy exists
* and $field and $value combinations exist, the Term will be returned.
*
* @since 1.1.3
*
* @uses get_term_by()
* @uses wp_cache_get()
* @uses wp_cache_set()
*
* @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
* @param string|int $value Search for this term value
* @param string $taxonomy Taxonomy Name
* @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
* @param string $filter Optional, default is raw or no WordPress defined filter will applied.
* @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
*/
public static function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
// ID lookups are cached
if ( 'id' == $field ) {
return get_term_by( $field, $value, $taxonomy, $output, $filter );
}
$cache_key = $field . '_' . md5( $value );
$term_id = wp_cache_get( $cache_key, 'get_term_by' );
if ( false === $term_id ) {
$term = get_term_by( $field, $value, $taxonomy );
if ( $term && ! is_wp_error( $term ) )
wp_cache_set( $cache_key, $term->term_id, 'get_term_by' );
else
wp_cache_set( $cache_key, 0, 'get_term_by' ); // if we get an invalid value, let's cache it anyway
} else {
$term = get_term( $term_id, $taxonomy, $output, $filter );
}
if ( is_wp_error( $term ) ) {
$term = false;
}
return $term;
}
/**
* Sanitizes name & sets transient.
*
* @param string $name Transient name.
* @param mixed $value Transient value/data.
* @param int $time Time to store transient (default: 1 day)
*/
protected static function set_transient( $name, $value, $time = 86400 ) {
$name = GS_Featured_Content::sanitize_transient( $name );
set_transient( $name, $value, $time );
}
/**
* Deletes transient with multisite support.
*
* @param string $name Transient name.
*/
protected static function delete_transient( $name ) {
$name = GS_Featured_Content::sanitize_transient( $name );
delete_transient( $name );
}
/**
* The More Posts from Category.
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_more_from_category( $instance ) {
$posts_term = $instance['posts_term'];
$taxonomy = $instance['taxonomy'];
if ( ! empty( $instance['more_from_category'] ) && ! empty( $posts_term['0'] ) ) {
GS_Featured_Content::action( 'gsfc_category_more', $instance );
GS_Featured_Content::action( 'gsfc_taxonomy_more', $instance );
GS_Featured_Content::action( 'gsfc_' . $taxonomy . '_more', $instance );
$term = GS_Featured_Content::get_term_by( 'slug', $posts_term['1'], $taxonomy );
$link = $instance['archive_link'] ? $instance['archive_link'] : esc_url( get_term_link( $posts_term['1'], $taxonomy ) );
printf(
'<p class="more-from-%1$s"><a href="%2$s" title="%3$s">%4$s</a></p>',
$taxonomy,
$link,
esc_attr( $term->name ),
esc_html( $instance['more_from_category_text'] )
);
}
GS_Featured_Content::action( 'gsfc_after_category_more', $instance );
GS_Featured_Content::action( 'gsfc_after_taxonomy_more', $instance );
GS_Featured_Content::action( 'gsfc_after_' . $taxonomy . '_more', $instance );
}
/**
* The EXTRA Posts (list).
*
* @param array $instance The settings for the particular instance of the widget.
*/
public static function do_extra_posts( $instance ) {
if ( empty( $instance['extra_posts'] ) || empty( $instance['extra_num'] ) ) return;
global $wp_query, $_genesis_displayed_ids;
$before_title = $instance['widget_args']['before_title'];
$after_title = $instance['widget_args']['after_title'];
if ( ! empty( $instance['extra_title'] ) )
echo GS_Featured_Content::build_tag( $before_title ) . esc_html( $instance['extra_title'] ) . $after_title;;
$offset = intval( $instance['posts_num'] ) + intval( $instance['posts_offset'] );
$extra_posts_args = array_merge(
$instance['q_args'],
array(
'showposts' => $instance['extra_num'],
'offset' => $offset,
'post_type' => $instance['post_type'],
'orderby' => $instance['orderby'],
'order' => $instance['order'],
'meta_key' => $instance['meta_key'],
)
);
$extra_posts_args = apply_filters( 'gsfc_extra_post_args', $extra_posts_args, $instance );
if ( !empty( $instance['optimize'] ) && !empty( $instance['custom_field'] ) ) {
if ( ! empty( $instance['delete_transients'] ) )
GS_Featured_Content::delete_transient( 'gsfc_extra_' . $instance['custom_field'] );
if ( false === ( $gsfc_query = GS_Featured_Content::get_transient( 'gsfc_extra_' . $instance['custom_field'] ) ) ) {
$gsfc_query = new WP_Query( $extra_posts_args );
$time = !empty( $instance['transients_time'] ) ? (int)$instance['transients_time'] : 60 * 60 * 24;
GS_Featured_Content::set_transient( 'gsfc_extra_' . $instance['custom_field'], $gsfc_query, $time );
}
} else {
$gsfc_query = new WP_Query( $extra_posts_args );
}
$optitems = $listitems = '';
$items = array();
if ( $gsfc_query->have_posts() ) :
GS_Featured_Content::action( 'gsfc_before_list_items', $instance );
while ( $gsfc_query->have_posts() ) : $gsfc_query->the_post();
$_genesis_displayed_ids[] = $id = get_the_ID();
$listitems .= sprintf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
$optitems .= sprintf( '<option class="%s" value="%s">%s</option>', $id, get_permalink(), get_the_title() );
$items[] = get_post();
endwhile;
wp_reset_postdata();
if ( strlen( $listitems ) > 0 && ( 'drop_down' != $instance['extra_format'] ) )
echo apply_filters( 'gsfc_list_items', sprintf( '<%1$s>%2$s</%1$s>', $instance['extra_format'], $listitems ), $instance, $listitems, $items );
elseif ( strlen( $optitems ) > 0 ) {
printf(
'<select id="gsfc-%1$s-extras" onchange="window.location=document.getElementById(\'gsfc-%1$s-extras\').value;"><option value="none">%2$s</option>%3$s</select>',
$instance['custom_field'],
__( 'Select', 'gsfc' ),
$optitems
);
}
GS_Featured_Content::action( 'gsfc_after_list_items', $instance );
endif;
//* Restore original query
wp_reset_query();
}
/**
* Used to exclude taxonomies and related terms from list of available terms/taxonomies in widget form()
*
* @param string $taxonomy 'taxonomy' being tested
* @return string
*/
public static function exclude_taxonomies( $taxonomy ) {
$filters = array( '', 'nav_menu' );
$filters = apply_filters( 'gsfc_exclude_taxonomies', $filters );
return ( ! in_array( $taxonomy->name, $filters ) );
}
/**
* Used to exclude post types from list of available post_types in widget form()
*
* @param string $type 'post_type' being tested
* @return string
*/
public static function exclude_post_types( $type ) {
$filters = array( '', 'attachment', 'soliloquy', );
$filters = apply_filters( 'gsfc_exclude_post_types', $filters, GS_Featured_Content::$widget_instance );
return( !in_array( $type, $filters ) );
}
/**
* Obtains available post types
*
* @param string $type 'post_type' being tested
* @return string
*/
public static function get_post_types( $type = 'names', $args = array(), $operator = 'and' ) {
$defaults = array(
'public' => true
);
$args = wp_parse_args( $args, $defaults );
$post_types = get_post_types( $args, $type, $operator );
$post_types = array_filter( $post_types, array( __CLASS__, 'exclude_post_types' ) );
return $post_types;
}
/**
* Filters the Post Limit to allow pagination with offset
*
* @global int $paged
* @global string $myOffset 'integer'
* @param string $limit
* @return string
*/
public static function post_limit( $limit ) {
global $paged, $myOffset;
if ( empty( $paged ) ) {
$paged = 1;
}
$postperpage = intval( get_option( 'posts_per_page' ) );
$pgstrt = ((intval( $paged ) - 1) * $postperpage) + $myOffset . ', ';
$limit = 'LIMIT ' . $pgstrt . $postperpage;
return $limit;
}
/**
* Get image size options.
*
* @return array Array of image size options.
*/
public static function get_image_size_options() {
$sizes = genesis_get_image_sizes();
$image_size_opt['thumbnail'] = 'thumbnail ('. get_option( 'thumbnail_size_w' ) . 'x' . get_option( 'thumbnail_size_h' ) . ')';
foreach( ( array )$sizes as $name => $size )
$image_size_opt[ $name ] = esc_html( $name ) . ' (' . $size['width'] . 'x' . $size['height'] . ')';
return $image_size_opt;
}
/**
* Returns form fields in boxes in columns
*
* @return array $columns Array of form fields.
*/
protected static function get_form_fields() {
$pt_obj = get_post_type_object( GS_Featured_Content::$widget_instance['post_type'] );
$box = array(
'widget_title_link' => array(
'label' => __( 'Link Title?', 'gsfc' ),
'description' => '',
'type' => 'checkbox',
'requires' => '',
),
'widget_title_link_href' => array(
'label' => __( 'Link', 'gsfc' ),
'description' => __( 'Please include the entire link.', 'gsfc' ),
'type' => 'text',
'requires' => array(
'widget_title_link',
'',
true
),
),
);
$box_1 = array(
'post_type' => array(
'label' => __( 'Content Type', 'gsfc' ),
'description' => '',
'type' => 'post_type_select',
'requires' => '',
),
'page_id' => array(
'label' => __( 'Page', 'gsfc' ),
'description' => '',
'type' => 'page_select',
'requires' => array(
'post_type',
'page',
false
),
),
'posts_term' => array(
'label' => __( 'Taxonomy and Terms', 'gsfc' ),
'description' => '',
'type' => 'select_taxonomy',
'requires' => array(
'post_type',
'page',
true
),
),
'exclude_terms' => array(
'label' => sprintf( __( 'Exclude Terms by ID %s (comma separated list)', 'gsfc' ), '<br />' ),
'description' => '',
'type' => 'text',
'requires' => array(
'post_type',
'page',
true
),
),
'include_exclude' => array(
'label' => __( 'Include/Exclude', 'gsfc' ),
'description' => '',
'type' => 'select',
'options' => array(
'' => __( 'Select', 'gsfc' ),
'include' => __( 'Include', 'gsfc' ),
'exclude' => __( 'Exclude', 'gsfc' ),
),
'requires' => array(
'post_type',
'page',
true
),
),
'post_id' => array(
'label' => sprintf( '<span class="gs-post-type-label">%s</span>', $pt_obj->name ) . ' ' . __( 'ID', 'gsfc' ),
'description' => '',
'type' => 'text',
'requires' => array(
'include_exclude',
'',
true
),
),
'posts_num' => array(
'label' => sprintf( '%s %s %s', __( 'Number of', 'gsfc' ), $pt_obj->label, __( 'to Show', 'gsfc' ) ),
'description' => '',
'type' => 'text_small',
'requires' => array(
'post_type',
'page',
true
),
),
'posts_offset' => array(
'label' => sprintf( '%s %s %s', __( 'Number of', 'gsfc' ), $pt_obj->label, __( 'to Offset', 'gsfc' ) ),
'description' => '',
'type' => 'text_small',
'requires' => array(
'post_type',
'page',
true
),
),
'orderby' => array(
'label' => __( 'Order By', 'gsfc' ),
'description' => '',
'type' => 'select',