Plugin Directory

Changeset 3096201


Ignore:
Timestamp:
06/02/2024 01:43:16 AM (22 months ago)
Author:
razorfrog
Message:

Easy Content Lists v1.0.1

Location:
easy-content-lists
Files:
4 added
2 edited

Legend:

Unmodified
Added
Removed
  • easy-content-lists/trunk/easy-content-lists.php

    r3038218 r3096201  
    44 * Plugin URI:        https://razorfrog.com/easy-content-lists/
    55 * Description:       Shortcodes for easily listing all your pages, posts, taxonomies, and tags.
    6  * Version:           1.0.0
     6 * Version:           1.0.1
    77 * Requires at least: 5.2
    88 * Requires PHP:      7.2
     
    2222 */
    2323function rflists_enqueue_styles() {
    24     $css_url     = plugin_dir_url( __FILE__ ) . 'style.css';
    25     $css_version = '1.0.0';
    26     wp_enqueue_style( 'razorfrog-plugin-styles', $css_url, array(), $css_version );
     24        $css_url     = plugin_dir_url( __FILE__ ) . 'style.css';
     25        $css_version = '1.0.0';
     26        wp_enqueue_style( 'razorfrog-plugin-styles', $css_url, array(), $css_version );
    2727}
    2828add_action( 'wp_enqueue_scripts', 'rflists_enqueue_styles' );
    2929
    3030
    31 // Pages list shortcode [razorfrog_pages_list].
    32 
    33 /**
    34  * Sorts pages into a hierarchical structure.
    35  *
    36  * @param array &$pages Array of pages to be sorted.
    37  * @param array &$hierarchy The sorted hierarchical array of pages.
    38  * @param int   $parent_id The ID of the parent page. Default is 0.
    39  */
    40 function rflists_sort_pages_hierarchically( &$pages, &$hierarchy, $parent_id = 0 ) {
    41     foreach ( $pages as $i => $page ) {
    42         if ( $page->post_parent === $parent_id ) {
    43             $hierarchy[] = $page;
    44             unset( $pages[ $i ] );
    45         }
    46     }
    47 
    48     foreach ( $hierarchy as $top_page ) {
    49         $top_page->children = array();
    50         rflists_sort_pages_hierarchically( $pages, $top_page->children, $top_page->ID );
    51     }
     31/******************************************************************************************************************/
     32/* Pages list shortcode [razorfrog_pages_list]. */
     33/******************************************************************************************************************/
     34
     35if ( ! shortcode_exists( 'razorfrog_pages_list' ) ) {
     36        /**
     37        * Sorts pages into a hierarchical structure.
     38        *
     39        * @param array &$pages Array of pages to be sorted.
     40        * @param array &$hierarchy The sorted hierarchical array of pages.
     41        * @param int   $parent_id The ID of the parent page. Default is 0.
     42        */
     43        function rflists_sort_pages_hierarchically( &$pages, &$hierarchy, $parent_id = 0 ) {
     44                foreach ( $pages as $i => $page ) {
     45                        if ( $page->post_parent === $parent_id ) {
     46                                $hierarchy[] = $page;
     47                                unset( $pages[ $i ] );
     48                        }
     49                }
     50
     51                foreach ( $hierarchy as $top_page ) {
     52                        $top_page->children = array();
     53                        rflists_sort_pages_hierarchically( $pages, $top_page->children, $top_page->ID );
     54                }
     55        }
     56
     57        /**
     58        * Recursively generates HTML markup to display pages in a hierarchical structure.
     59        *
     60        * @param array $pages Array of pages to display.
     61        * @param int   $col Column number for CSS class. Used for styling purposes.
     62        * @param int   $loop Loop iteration counter to determine the level of nesting.
     63        * @return string HTML markup of the hierarchical page structure.
     64        */
     65        function rflists_print_pages_hierarchically( $pages, $col, $loop ) {
     66                $class = 1 === $loop ? "rf-page-list-parent rf-page-col-$col" : 'rf-page-list-child';
     67                $html  = "<ul class='rf-page-list $class list-pages'>";
     68
     69                foreach ( $pages as $page ) {
     70                        $title = esc_html( $page->post_title );
     71                        $link  = esc_url( get_page_link( $page ) );
     72                        $html .= "<li class='page-item page-item-{$page->ID}'><a href='{$link}' rel='bookmark'>{$title}</a></li>";
     73
     74                        if ( is_array( $page->children ) && ! empty( $page->children ) ) {
     75                                $html .= rflists_print_pages_hierarchically( $page->children, 0, $loop + 1 );
     76                        }
     77                }
     78
     79                $html .= '</ul>';
     80                return $html;
     81        }
     82
     83        /**
     84        * Shortcode function to display a list of pages in a hierarchical structure.
     85        *
     86        * @param array $atts Shortcode attributes.
     87        * @return string HTML output of the pages list.
     88        */
     89        function rflists_pages_list_function( $atts ) {
     90                // Get shortcode parameters with defaults.
     91                $atts = shortcode_atts(
     92                        array(
     93                                'orderby'       => 'title',
     94                                'order'         => 'ASC',
     95                                'exclude'       => '',
     96                                'child_of'      => 0,
     97                                'col'           => 1,
     98                                'error_message' => 'ERROR: Can\'t find pages.',
     99                        ),
     100                        $atts,
     101                        'razorfrog_pages_list'
     102                );
     103
     104                // Prepare arguments for get_pages.
     105                $args = array(
     106                        'post_status' => 'publish',
     107                        'orderby'     => sanitize_text_field( $atts['orderby'] ),
     108                        'order'       => sanitize_text_field( $atts['order'] ),
     109                        'exclude'     => array_filter( array_map( 'intval', explode( ',', $atts['exclude'] ) ) ),
     110                        'child_of'    => intval( $atts['child_of'] ),
     111                );
     112
     113                // Retrieve pages based on arguments.
     114                $pages = get_pages( $args );
     115
     116                // Check for pages and generate output.
     117                if ( $pages && ! is_wp_error( $pages ) ) {
     118                        $pages_hierarchical = array();
     119                        rflists_sort_pages_hierarchically( $pages, $pages_hierarchical, intval( $atts['child_of'] ) );
     120                        $return_string = rflists_print_pages_hierarchically( $pages_hierarchical, intval( $atts['col'] ), 1 );
     121                } else {
     122                        $return_string = "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
     123                }
     124
     125                return $return_string;
     126        }
     127        add_shortcode( 'razorfrog_pages_list', 'rflists_pages_list_function' );
    52128}
    53129
    54 /**
    55  * Recursively generates HTML markup to display pages in a hierarchical structure.
    56  *
    57  * @param array $pages Array of pages to display.
    58  * @param int   $col Column number for CSS class. Used for styling purposes.
    59  * @param int   $loop Loop iteration counter to determine the level of nesting.
    60  * @return string HTML markup of the hierarchical page structure.
    61  */
    62 function rflists_print_pages_hierarchically( $pages, $col, $loop ) {
    63     $class = 1 === $loop ? "rf-page-list-parent rf-page-col-$col" : 'rf-page-list-child';
    64     $html  = "<ul class='rf-page-list $class list-pages'>";
    65 
    66     foreach ( $pages as $page ) {
    67         $title = esc_html( $page->post_title );
    68         $link  = esc_url( get_page_link( $page ) );
    69         $html .= "<li class='page-item page-item-{$page->ID}'><a href='{$link}' rel='bookmark'>{$title}</a></li>";
    70 
    71         if ( is_array( $page->children ) && ! empty( $page->children ) ) {
    72             $html .= rflists_print_pages_hierarchically( $page->children, 0, $loop + 1 );
    73         }
    74     }
    75 
    76     $html .= '</ul>';
    77     return $html;
     130
     131/******************************************************************************************************************/
     132/* Posts list shortcode [razorfrog_posts_list]. */
     133/******************************************************************************************************************/
     134
     135if ( ! shortcode_exists( 'razorfrog_posts_list' ) ) {
     136        /**
     137        * Shortcode function to display a list of posts with various filtering options.
     138        *
     139        * @param array $atts Shortcode attributes.
     140        * @return string HTML output of the posts list.
     141        */
     142        function rflists_posts_list_function( $atts ) {
     143                // Get shortcode parameters with defaults.
     144                $atts = shortcode_atts(
     145                        array(
     146                                'post_type'      => 'post',
     147                                'orderby'        => 'date',
     148                                'order'          => 'DESC',
     149                                'exclude'        => '',
     150                                'posts_per_page' => 100,
     151                                'col'            => 1,
     152                                'taxonomy'       => '',
     153                                'terms'          => '',
     154                                'meta_key'       => '',
     155                                'meta_value'     => '',
     156                                'error_message'  => 'ERROR: Can\'t find posts.',
     157                        ),
     158                        $atts,
     159                        'razorfrog_posts_list'
     160                );
     161
     162                // Prepare query arguments.
     163                $args = array(
     164                        'post_status'    => 'publish',
     165                        'post_type'      => sanitize_text_field( $atts['post_type'] ),
     166                        'orderby'        => sanitize_text_field( $atts['orderby'] ),
     167                        'order'          => sanitize_text_field( $atts['order'] ),
     168                        'post__not_in'   => array_filter( array_map( 'intval', explode( ',', $atts['exclude'] ) ) ),
     169                        'posts_per_page' => intval( $atts['posts_per_page'] ),
     170                        'meta_key'       => sanitize_text_field( $atts['meta_key'] ),
     171                );
     172
     173                // Taxonomy query.
     174                if ( ! empty( $atts['taxonomy'] ) && ! empty( $atts['terms'] ) ) {
     175                        $args['tax_query'] = array(
     176                                array(
     177                                        'taxonomy' => sanitize_text_field( $atts['taxonomy'] ),
     178                                        'field'    => 'slug',
     179                                        'terms'    => explode( ',', sanitize_text_field( $atts['terms'] ) ),
     180                                        'operator' => 'IN',
     181                                ),
     182                        );
     183                }
     184
     185                // Meta query.
     186                if ( ! empty( $atts['meta_key'] ) && ! empty( $atts['meta_value'] ) ) {
     187                        $args['meta_query'] = array(
     188                                array(
     189                                        'key'     => sanitize_text_field( $atts['meta_key'] ),
     190                                        'value'   => sanitize_text_field( $atts['meta_value'] ),
     191                                        'compare' => '=',
     192                                ),
     193                        );
     194                }
     195
     196                // Custom query for posts.
     197                $query  = new WP_Query( $args );
     198                $col    = intval( $atts['col'] );
     199                $active = get_the_ID();
     200
     201                // Generates output.
     202                if ( $query->have_posts() ) {
     203                        $return_string = "<ul class='rf-post-list rf-post-list-type-{$atts['post_type']} rf-post-col-$col'>";
     204                        while ( $query->have_posts() ) :
     205                                $query->the_post();
     206
     207                                $class = ( get_the_ID() === $active ) ? ' active' : '';
     208                                $title = esc_html( get_the_title() );
     209                                $link  = esc_url( get_permalink() );
     210
     211                                $return_string .= "<li class='post-item post-item-" . get_the_ID() . "$class'><a href='$link' rel='bookmark'>$title</a></li>";
     212
     213                        endwhile;
     214                        $return_string .= '</ul>';
     215                } else {
     216                        $return_string = "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
     217                }
     218
     219                // Reset postdata.
     220                wp_reset_postdata();
     221                return $return_string;
     222        }
     223        add_shortcode( 'razorfrog_posts_list', 'rflists_posts_list_function' );
    78224}
    79225
    80 /**
    81  * Shortcode function to display a list of pages in a hierarchical structure.
    82  *
    83  * @param array $atts Shortcode attributes.
    84  * @return string HTML output of the pages list.
    85  */
    86 function rflists_pages_list_function( $atts ) {
    87     // Get shortcode parameters with defaults.
    88     $atts = shortcode_atts(
    89         array(
    90             'orderby'       => 'title',
    91             'order'         => 'ASC',
    92             'exclude'       => '',
    93             'child_of'      => 0,
    94             'col'           => 1,
    95             'error_message' => 'ERROR: Can\'t find pages.',
    96         ),
    97         $atts,
    98         'razorfrog_pages_list'
    99     );
    100 
    101     // Prepare arguments for get_pages.
    102     $args = array(
    103         'post_status' => 'publish',
    104         'orderby'     => $atts['orderby'],
    105         'order'       => $atts['order'],
    106         'exclude'     => array_filter( array_map( 'intval', explode( ',', $atts['exclude'] ) ) ),
    107         'child_of'    => intval( $atts['child_of'] ),
    108     );
    109 
    110     // Retrieve pages based on arguments.
    111     $pages = get_pages( $args );
    112 
    113     // Check for pages and generate output.
    114     if ( $pages && ! is_wp_error( $pages ) ) {
    115         $pages_hierarchical = array();
    116         rflists_sort_pages_hierarchically( $pages, $pages_hierarchical, intval( $atts['child_of'] ) );
    117         $return_string = rflists_print_pages_hierarchically( $pages_hierarchical, intval( $atts['col'] ), 1 );
    118     } else {
    119         $return_string = "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
    120     }
    121 
    122     return $return_string;
     226
     227/******************************************************************************************************************/
     228/* Taxonomy list shortcode [razorfrog_taxonomy_list]. */
     229/******************************************************************************************************************/
     230
     231if ( ! shortcode_exists( 'razorfrog_taxonomy_list' ) ) {
     232        /**
     233        * Sorts terms into a hierarchical structure.
     234        *
     235        * @param array &$terms Array of terms to be sorted.
     236        * @param array &$hierarchy The sorted hierarchical array of terms.
     237        * @param int   $parent_id The ID of the parent term. Default is 0.
     238        */
     239        function rflists_sort_terms_hierarchically( &$terms, &$hierarchy, $parent_id = 0 ) {
     240                foreach ( $terms as $i => $term ) {
     241                        if ( $term->parent === $parent_id ) {
     242                                $hierarchy[] = $term;
     243                                unset( $terms[ $i ] );
     244                        }
     245                }
     246
     247                foreach ( $hierarchy as $top_term ) {
     248                        $top_term->children = array();
     249                        rflists_sort_terms_hierarchically( $terms, $top_term->children, $top_term->term_id );
     250                }
     251        }
     252
     253        /**
     254        * Recursively generates HTML markup to display taxonomy terms in a hierarchical structure.
     255        *
     256        * @param array $terms Array of terms to display.
     257        * @param int   $col Column number for CSS class.
     258        * @param bool  $count Whether to show term count.
     259        * @param int   $loop Loop iteration counter to determine the level of nesting.
     260        * @return string HTML markup of the hierarchical term structure.
     261        */
     262        function rflists_print_terms_hierarchically( $terms, $col, $count, $loop ) {
     263                $class  = 1 === $loop ? "rf-tax-list-parent rf-tax-col-$col" : 'rf-tax-child';
     264                $class .= $count ? ' show-count' : '';
     265                $html   = "<ul class='rf-tax-list $class'>";
     266
     267                foreach ( $terms as $term ) {
     268                        $name       = esc_html( $term->name );
     269                        $link       = esc_url( get_term_link( $term ) );
     270                        $count_span = $count ? "<span class='term-count'>" . esc_html( $term->count ) . '</span>' : '';
     271
     272                        $html .= "<li class='term-item term-item-{$term->term_id}'>";
     273                        $html .= "<span class='term-item-inner'>";
     274                        $html .= "<a href='$link'>$name</a>$count_span";
     275                        $html .= '</span></li>';
     276
     277                        if ( is_array( $term->children ) && ! empty( $term->children ) ) {
     278                                $html .= rflists_print_terms_hierarchically( $term->children, 0, $count, $loop + 1 );
     279                        }
     280                }
     281
     282                $html .= '</ul>';
     283                return $html;
     284        }
     285
     286        /**
     287        * Shortcode function to display a list of taxonomy terms.
     288        *
     289        * @param array $atts Shortcode attributes.
     290        * @return string HTML output of the taxonomy list.
     291        */
     292        function rflists_taxonomy_list_function( $atts ) {
     293                $atts = shortcode_atts(
     294                        array(
     295                                'taxonomy'      => 'category',
     296                                'orderby'       => 'title',
     297                                'order'         => 'ASC',
     298                                'col'           => 1,
     299                                'count'         => 'true',
     300                                'error_message' => 'ERROR: Can\'t find taxonomy.',
     301                        ),
     302                        $atts,
     303                        'razorfrog_taxonomy_list'
     304                );
     305
     306                $col   = intval( $atts['col'] );
     307                $count = 'true' === $atts['count'] || '1' === $atts['count'];
     308
     309                $terms = get_terms(
     310                        array(
     311                                'taxonomy'   => sanitize_text_field( $atts['taxonomy'] ),
     312                                'orderby'    => sanitize_text_field( $atts['orderby'] ),
     313                                'order'      => sanitize_text_field( $atts['order'] ),
     314                                'hide_empty' => true,
     315                        )
     316                );
     317
     318                if ( $terms && ! is_wp_error( $terms ) ) {
     319                        $terms_hierarchical = array();
     320                        rflists_sort_terms_hierarchically( $terms, $terms_hierarchical );
     321                        $return_string = rflists_print_terms_hierarchically( $terms_hierarchical, $col, $count, 1 );
     322                } else {
     323                        $return_string = "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
     324                }
     325
     326                return $return_string;
     327        }
     328        add_shortcode( 'razorfrog_taxonomy_list', 'rflists_taxonomy_list_function' );
    123329}
    124 add_shortcode( 'razorfrog_pages_list', 'rflists_pages_list_function' );
    125 
    126 
    127 // Posts list shortcode [razorfrog_posts_list].
    128 
    129 /**
    130  * Shortcode function to display a list of posts with various filtering options.
    131  *
    132  * @param array $atts Shortcode attributes.
    133  * @return string HTML output of the posts list.
    134  */
    135 function rflists_posts_list_function( $atts ) {
    136     // Get shortcode parameters with defaults.
    137     $atts = shortcode_atts(
    138         array(
    139             'post_type'      => 'post',
    140             'orderby'        => 'date',
    141             'order'          => 'DESC',
    142             'exclude'        => '',
    143             'posts_per_page' => 100,
    144             'col'            => 1,
    145             'taxonomy'       => '',
    146             'terms'          => '',
    147             'meta_key'       => '',
    148             'meta_value'     => '',
    149             'error_message'  => 'ERROR: Can\'t find posts.',
    150         ),
    151         $atts,
    152         'razorfrog_posts_list'
    153     );
    154 
    155     // Prepare query arguments.
    156     $args = array(
    157         'post_status'    => 'publish',
    158         'post_type'      => $atts['post_type'],
    159         'orderby'        => $atts['orderby'],
    160         'order'          => $atts['order'],
    161         'post__not_in'   => array_filter( array_map( 'intval', explode( ',', $atts['exclude'] ) ) ),
    162         'posts_per_page' => intval( $atts['posts_per_page'] ),
    163         'meta_key'       => $atts['meta_key'],
    164     );
    165 
    166     // Taxonomy query.
    167     if ( ! empty( $atts['taxonomy'] ) && ! empty( $atts['terms'] ) ) {
    168         $args['tax_query'] = array(
    169             array(
    170                 'taxonomy' => $atts['taxonomy'],
    171                 'field'    => 'slug',
    172                 'terms'    => explode( ',', $atts['terms'] ),
    173                 'operator' => 'IN',
    174             ),
    175         );
    176     }
    177 
    178     // Meta query.
    179     if ( ! empty( $atts['meta_key'] ) && ! empty( $atts['meta_value'] ) ) {
    180         $args['meta_query'] = array(
    181             array(
    182                 'key'     => $atts['meta_key'],
    183                 'value'   => $atts['meta_value'],
    184                 'compare' => '=',
    185             ),
    186         );
    187     }
    188 
    189     // Custom query for posts.
    190     $query  = new WP_Query( $args );
    191     $col    = intval( $atts['col'] );
    192     $active = get_the_ID();
    193 
    194     // Generates output.
    195     if ( $query->have_posts() ) {
    196         $return_string = "<ul class='rf-post-list rf-post-list-type-{$atts['post_type']} rf-post-col-$col'>";
    197         while ( $query->have_posts() ) :
    198             $query->the_post();
    199 
    200             $class = ( get_the_ID() === $active ) ? ' active' : '';
    201             $title = esc_html( get_the_title() );
    202             $link  = esc_url( get_permalink() );
    203 
    204             $return_string .= "<li class='post-item post-item-" . get_the_ID() . "$class'><a href='$link' rel='bookmark'>$title</a></li>";
    205 
    206         endwhile;
    207         $return_string .= '</ul>';
    208     } else {
    209         $return_string = "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
    210     }
    211 
    212     // Reset postdata.
    213     wp_reset_postdata();
    214     return $return_string;
     330
     331
     332/******************************************************************************************************************/
     333/* Tags list shortcode [razorfrog_tags_list]. */
     334/******************************************************************************************************************/
     335
     336if ( ! shortcode_exists( 'razorfrog_tags_list' ) ) {
     337        /**
     338        * Shortcode function to display a list of tags, styled as a tag cloud.
     339        *
     340        * @param array $atts Shortcode attributes.
     341        * @return string HTML output of the tag list.
     342        */
     343        function rflists_tags_list_function( $atts ) {
     344                $atts = shortcode_atts(
     345                        array(
     346                                'query_type'    => 'exclude',
     347                                'tags_ids'      => '',
     348                                'max_count'     => 100,
     349                                'size_from'     => 16,
     350                                'size_to'       => 36,
     351                                'error_message' => 'ERROR: Can\'t find tags.',
     352                        ),
     353                        $atts,
     354                        'razorfrog_tags_list'
     355                );
     356
     357                $tag_args = array(
     358                        'taxonomy'   => 'post_tag',
     359                        'orderby'    => 'count',
     360                        'order'      => 'DESC',
     361                        'hide_empty' => true,
     362                );
     363
     364                if ( 'exclude' === $atts['query_type'] ) {
     365                        $tag_args['exclude'] = array_map( 'intval', explode( ',', $atts['tags_ids'] ) );
     366                } elseif ( 'include' === $atts['query_type'] ) {
     367                        $tag_args['include'] = array_map( 'intval', explode( ',', $atts['tags_ids'] ) );
     368                } else {
     369                        return "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
     370                }
     371
     372                $tags = get_tags( $tag_args );
     373
     374                if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
     375                        $cloud = rflists_build_tag_cloud( $tags, $atts );
     376                        return '<div class="rf-tag-list">' . implode( '', $cloud ) . '</div>';
     377                } else {
     378                        return "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
     379                }
     380        }
     381        add_shortcode( 'razorfrog_tags_list', 'rflists_tags_list_function' );
     382
     383        /**
     384        * Builds a tag cloud with specified font size settings.
     385        *
     386        * @param array $tags Array of tag objects.
     387        * @param array $atts Attributes from the shortcode.
     388        * @return array Array of HTML elements for each tag.
     389        */
     390        function rflists_build_tag_cloud( $tags, $atts ) {
     391                // Font size calculation.
     392                $max_count    = intval( $atts['max_count'] );
     393                $count_spread = $tags[0]->count - end( $tags )->count;
     394                $font_spread  = intval( $atts['size_to'] ) - intval( $atts['size_from'] );
     395                $step         = $count_spread ? ( $font_spread / $count_spread ) : 0;
     396
     397                $cloud = array();
     398                foreach ( $tags as $index => $tag ) {
     399                        if ( $index >= $max_count ) {
     400                                break;
     401                        }
     402
     403                        $size = intval( $atts['size_from'] ) + ( ( $tag->count - end( $tags )->count ) * $step );
     404                        $link = esc_url( get_tag_link( $tag->term_id ) );
     405                        $name = esc_html( $tag->name );
     406
     407                        $cloud[] = "<p class='tag-item tag-item-{$tag->term_id}' style='font-size:{$size}px;'><a href='{$link}'>{$name}</a></p>";
     408                }
     409
     410                sort( $cloud ); // Sort alphabetically.
     411                return $cloud;
     412        }
    215413}
    216 add_shortcode( 'razorfrog_posts_list', 'rflists_posts_list_function' );
    217 
    218 
    219 // Taxonomy list shortcode [razorfrog_taxonomy_list].
    220 
    221 /**
    222  * Sorts terms into a hierarchical structure.
    223  *
    224  * @param array &$terms Array of terms to be sorted.
    225  * @param array &$hierarchy The sorted hierarchical array of terms.
    226  * @param int   $parent_id The ID of the parent term. Default is 0.
    227  */
    228 function rflists_sort_terms_hierarchically( &$terms, &$hierarchy, $parent_id = 0 ) {
    229     foreach ( $terms as $i => $term ) {
    230         if ( $term->parent === $parent_id ) {
    231             $hierarchy[] = $term;
    232             unset( $terms[ $i ] );
    233         }
    234     }
    235 
    236     foreach ( $hierarchy as $top_term ) {
    237         $top_term->children = array();
    238         rflists_sort_terms_hierarchically( $terms, $top_term->children, $top_term->term_id );
    239     }
    240 }
    241 
    242 /**
    243  * Recursively generates HTML markup to display taxonomy terms in a hierarchical structure.
    244  *
    245  * @param array $terms Array of terms to display.
    246  * @param int   $col Column number for CSS class.
    247  * @param bool  $count Whether to show term count.
    248  * @param int   $loop Loop iteration counter to determine the level of nesting.
    249  * @return string HTML markup of the hierarchical term structure.
    250  */
    251 function rflists_print_terms_hierarchically( $terms, $col, $count, $loop ) {
    252     $class  = 1 === $loop ? "rf-tax-list-parent rf-tax-col-$col" : 'rf-tax-child';
    253     $class .= $count ? ' show-count' : '';
    254     $html   = "<ul class='rf-tax-list $class'>";
    255 
    256     foreach ( $terms as $term ) {
    257         $name       = esc_html( $term->name );
    258         $link       = esc_url( get_term_link( $term ) );
    259         $count_span = $count ? "<span class='term-count'>" . esc_html( $term->count ) . '</span>' : '';
    260 
    261         $html .= "<li class='term-item term-item-{$term->term_id}'>";
    262         $html .= "<span class='term-item-inner'>";
    263         $html .= "<a href='$link'>$name</a>$count_span";
    264         $html .= '</span></li>';
    265 
    266         if ( is_array( $term->children ) && ! empty( $term->children ) ) {
    267             $html .= rflists_print_terms_hierarchically( $term->children, 0, $count, $loop + 1 );
    268         }
    269     }
    270 
    271     $html .= '</ul>';
    272     return $html;
    273 }
    274 
    275 /**
    276  * Shortcode function to display a list of taxonomy terms.
    277  *
    278  * @param array $atts Shortcode attributes.
    279  * @return string HTML output of the taxonomy list.
    280  */
    281 function rflists_taxonomy_list_function( $atts ) {
    282     $atts = shortcode_atts(
    283         array(
    284             'taxonomy'      => 'category',
    285             'orderby'       => 'title',
    286             'order'         => 'ASC',
    287             'col'           => 1,
    288             'count'         => 'true',
    289             'error_message' => 'ERROR: Can\'t find taxonomy.',
    290         ),
    291         $atts,
    292         'razorfrog_taxonomy_list'
    293     );
    294 
    295     $col   = intval( $atts['col'] );
    296     $count = 'true' === $atts['count'] || '1' === $atts['count'];
    297 
    298     $terms = get_terms(
    299         array(
    300             'taxonomy'   => $atts['taxonomy'],
    301             'orderby'    => $atts['orderby'],
    302             'order'      => $atts['order'],
    303             'hide_empty' => true,
    304         )
    305     );
    306 
    307     if ( $terms && ! is_wp_error( $terms ) ) {
    308         $terms_hierarchical = array();
    309         rflists_sort_terms_hierarchically( $terms, $terms_hierarchical );
    310         $return_string = rflists_print_terms_hierarchically( $terms_hierarchical, $col, $count, 1 );
    311     } else {
    312         $return_string = "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
    313     }
    314 
    315     return $return_string;
    316 }
    317 add_shortcode( 'razorfrog_taxonomy_list', 'rflists_taxonomy_list_function' );
    318 
    319 
    320 // Tags list shortcode [razorfrog_tags_list].
    321 
    322 /**
    323  * Shortcode function to display a list of tags, styled as a tag cloud.
    324  *
    325  * @param array $atts Shortcode attributes.
    326  * @return string HTML output of the tag list.
    327  */
    328 function rflists_tags_list_function( $atts ) {
    329     $atts = shortcode_atts(
    330         array(
    331             'query_type'    => 'exclude',
    332             'tags_ids'      => '',
    333             'max_count'     => 100,
    334             'size_from'     => 16,
    335             'size_to'       => 36,
    336             'error_message' => 'ERROR: Can\'t find tags.',
    337         ),
    338         $atts,
    339         'razorfrog_tags_list'
    340     );
    341 
    342     $tag_args = array(
    343         'taxonomy'   => 'post_tag',
    344         'orderby'    => 'count',
    345         'order'      => 'DESC',
    346         'hide_empty' => true,
    347     );
    348 
    349     if ( 'exclude' === $atts['query_type'] ) {
    350         $tag_args['exclude'] = explode( ',', $atts['tags_ids'] );
    351     } elseif ( 'include' === $atts['query_type'] ) {
    352         $tag_args['include'] = explode( ',', $atts['tags_ids'] );
    353     } else {
    354         return "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
    355     }
    356 
    357     $tags = get_tags( $tag_args );
    358 
    359     if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
    360         $cloud = rflists_build_tag_cloud( $tags, $atts );
    361         return '<div class="rf-tag-list">' . implode( '', $cloud ) . '</div>';
    362     } else {
    363         return "<p class='rf-list-error'>" . esc_html( $atts['error_message'] ) . '</p>';
    364     }
    365 }
    366 add_shortcode( 'razorfrog_tags_list', 'rflists_tags_list_function' );
    367 
    368 /**
    369  * Builds a tag cloud with specified font size settings.
    370  *
    371  * @param array $tags Array of tag objects.
    372  * @param array $atts Attributes from the shortcode.
    373  * @return array Array of HTML elements for each tag.
    374  */
    375 function rflists_build_tag_cloud( $tags, $atts ) {
    376     // Font size calculation.
    377     $max_count    = intval( $atts['max_count'] );
    378     $count_spread = $tags[0]->count - end( $tags )->count;
    379     $font_spread  = intval( $atts['size_to'] ) - intval( $atts['size_from'] );
    380     $step         = $count_spread ? ( $font_spread / $count_spread ) : 0;
    381 
    382     $cloud = array();
    383     foreach ( $tags as $index => $tag ) {
    384         if ( $index >= $max_count ) {
    385             break;
    386         }
    387 
    388         $size = intval( $atts['size_from'] ) + ( ( $tag->count - end( $tags )->count ) * $step );
    389         $link = esc_url( get_tag_link( $tag->term_id ) );
    390         $name = esc_html( $tag->name );
    391 
    392         $cloud[] = "<p class='tag-item tag-item-{$tag->term_id}' style='font-size:{$size}px;'><a href='{$link}'>{$name}</a></p>";
    393     }
    394 
    395     sort( $cloud ); // Sort alphabetically.
    396     return $cloud;
    397 }
  • easy-content-lists/trunk/readme.txt

    r3038218 r3096201  
    33Tags: pages, posts, taxonomies, tags, lists
    44Requires at least: 5.0
    5 Tested up to: 6.4.3
     5Tested up to: 6.5.3
    66Requires PHP: 7.2
    7 Stable tag: 1.0.0
     7Stable tag: 1.0.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6666
    6767== Changelog ==
     68= 1.0.1 =
     69- Added conditions to check if shortcodes already exist to avoid conflicts.
     70- Improved data sanitization and escaping.
     71
    6872= 1.0.0 =
    6973- Initial release.
Note: See TracChangeset for help on using the changeset viewer.