Shortcode
1 month ago
Alphabet.php
1 month ago
Extension.php
1 month ago
Grouping.php
1 month ago
GutenBlock.php
1 month ago
Indices.php
1 month ago
Numbers.php
1 month ago
Query.php
1 month ago
Shortcode.php
1 month ago
Singleton.php
1 month ago
Strings.php
1 month ago
Shortcode.php
116 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 | 'back-to-top' => 'true', |
| 52 | 'display' => 'posts', |
| 53 | 'get-all-children' => 'false', |
| 54 | 'group-numbers' => '', |
| 55 | 'grouping' => '', |
| 56 | 'numbers' => 'hide', |
| 57 | 'return' => 'listing', |
| 58 | 'target' => '', |
| 59 | ) |
| 60 | ); |
| 61 | $attributes = shortcode_atts( |
| 62 | $defaults, |
| 63 | $attributes, |
| 64 | 'alphalisting' |
| 65 | ); |
| 66 | |
| 67 | foreach ( $attributes as $attribute => &$value ) { |
| 68 | $value = apply_filters( "alphalisting_sanitize_shortcode_attribute__$attribute", $value, $attributes ); |
| 69 | } |
| 70 | $attributes = apply_filters( 'alphalisting_sanitize_shortcode_attributes', $attributes ); |
| 71 | |
| 72 | $grouping = $attributes['grouping']; |
| 73 | $group_numbers = false; |
| 74 | if ( ! empty( $attributes['group-numbers'] ) && alphalisting_is_truthy( $attributes['group-numbers'] ) ) { |
| 75 | $group_numbers = true; |
| 76 | } |
| 77 | |
| 78 | if ( 'numbers' === $grouping ) { |
| 79 | $group_numbers = true; |
| 80 | $grouping = 0; |
| 81 | } else { |
| 82 | $grouping = intval( $grouping ); |
| 83 | if ( 1 < $grouping && empty( $attributes['group-numbers'] ) ) { |
| 84 | $group_numbers = true; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | $grouping_obj = new Grouping( $grouping ); |
| 89 | $numbers_obj = new Numbers( $attributes['numbers'], $group_numbers ); |
| 90 | |
| 91 | $a_z_query = new Query( null, '', true, $attributes ); |
| 92 | |
| 93 | $target = ''; |
| 94 | if ( ! empty( $attributes['target'] ) ) { |
| 95 | if ( intval( $attributes['target'] ) > 0 ) { |
| 96 | $target = get_permalink( $attributes['target'] ); |
| 97 | } else { |
| 98 | $target = $attributes['target']; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if ( 'letters' === $attributes['return'] ) { |
| 103 | $ret = '<div class="az-letters">' . $a_z_query->get_the_letters( $target ) . '</div>'; |
| 104 | } else { |
| 105 | $ret = $a_z_query->get_the_listing(); |
| 106 | } |
| 107 | |
| 108 | $grouping_obj->teardown(); |
| 109 | $numbers_obj->teardown(); |
| 110 | |
| 111 | do_action( 'alphalisting_shortcode_end', $attributes ); |
| 112 | |
| 113 | return $ret; |
| 114 | } |
| 115 | } |
| 116 |