PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.3.6
AlphaListing v4.3.6
trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Shortcode.php
alphalisting / src Last commit date
Shortcode 9 months ago Alphabet.php 9 months ago Extension.php 9 months ago Grouping.php 9 months ago GutenBlock.php 9 months ago Indices.php 9 months ago Numbers.php 9 months ago Query.php 9 months ago Shortcode.php 9 months ago Singleton.php 9 months ago Strings.php 9 months ago
Shortcode.php
115 lines
1 <?php
2 /**
3 * Contains the A-Z Index shortcode functionality
4 *
5 * @package alphalisting
6 */
7
8 declare(strict_types=1);
9
10 namespace eslin87\AlphaListing;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Shortcode handler.
18 */
19 class Shortcode extends Singleton implements Extension {
20 /**
21 * Bind the shortcode to the handler.
22 *
23 * @return void
24 */
25 final public function initialize() {
26 add_shortcode( 'alphalisting', array( $this, 'handle' ) );
27 }
28
29 /**
30 * Handle the alphalisting shortcode
31 *
32 * @since 1.0.0
33 * @since 1.7.0 Add numbers attribute to append or prepend numerics to the listing.
34 * @since 1.8.0 Fix numbers attribute when selecting to display terms. Add grouping to numbers via attribute. Add alphabet override via new attribute.
35 * @since 2.0.0 Add parent-term and hide-empty parameters.
36 * @since 3.0.0 Move into a class and namespace.
37 * @since 4.0.0 Abstract away most of the specifics into separate classes.
38 * @param string|array<string,mixed> $attributes Provided by WordPress core. Contains the shortcode attributes.
39 * @return string The AlphaListing HTML.
40 * @suppress PhanPluginPossiblyStaticPublicMethod
41 */
42 public function handle( $attributes = array() ): string {
43 /**
44 * Run extensions.
45 */
46 do_action( 'alphalisting_shortcode_start', $attributes );
47
48 $defaults = apply_filters(
49 'alphalisting_get_shortcode_attributes',
50 array(
51 'display' => 'posts',
52 'get-all-children' => 'false',
53 'group-numbers' => '',
54 'grouping' => '',
55 'numbers' => 'hide',
56 'return' => 'listing',
57 'target' => '',
58 )
59 );
60 $attributes = shortcode_atts(
61 $defaults,
62 $attributes,
63 'alphalisting'
64 );
65
66 foreach ( $attributes as $attribute => &$value ) {
67 $value = apply_filters( "alphalisting_sanitize_shortcode_attribute__$attribute", $value, $attributes );
68 }
69 $attributes = apply_filters( 'alphalisting_sanitize_shortcode_attributes', $attributes );
70
71 $grouping = $attributes['grouping'];
72 $group_numbers = false;
73 if ( ! empty( $attributes['group-numbers'] ) && alphalisting_is_truthy( $attributes['group-numbers'] ) ) {
74 $group_numbers = true;
75 }
76
77 if ( 'numbers' === $grouping ) {
78 $group_numbers = true;
79 $grouping = 0;
80 } else {
81 $grouping = intval( $grouping );
82 if ( 1 < $grouping && empty( $attributes['group-numbers'] ) ) {
83 $group_numbers = true;
84 }
85 }
86
87 $grouping_obj = new Grouping( $grouping );
88 $numbers_obj = new Numbers( $attributes['numbers'], $group_numbers );
89
90 $a_z_query = new Query( null, '', true, $attributes );
91
92 $target = '';
93 if ( ! empty( $attributes['target'] ) ) {
94 if ( intval( $attributes['target'] ) > 0 ) {
95 $target = get_permalink( $attributes['target'] );
96 } else {
97 $target = $attributes['target'];
98 }
99 }
100
101 if ( 'letters' === $attributes['return'] ) {
102 $ret = '<div class="az-letters">' . $a_z_query->get_the_letters( $target ) . '</div>';
103 } else {
104 $ret = $a_z_query->get_the_listing();
105 }
106
107 $grouping_obj->teardown();
108 $numbers_obj->teardown();
109
110 do_action( 'alphalisting_shortcode_end', $attributes );
111
112 return $ret;
113 }
114 }
115