Plugin Directory

Changeset 3193532


Ignore:
Timestamp:
11/20/2024 10:57:26 PM (16 months ago)
Author:
nvwd
Message:

Update to version 2.1.0 from GitHub

Location:
a2z-alphabetical-archive-links
Files:
38 added
8 deleted
17 edited
1 copied

Legend:

Unmodified
Added
Removed
  • a2z-alphabetical-archive-links/assets/screenshot-1.png

    • Property svn:mime-type changed from application/octet-stream to image/png
  • a2z-alphabetical-archive-links/assets/screenshot-2.png

    • Property svn:mime-type changed from application/octet-stream to image/png
  • a2z-alphabetical-archive-links/assets/screenshot-3.png

    • Property svn:mime-type changed from application/octet-stream to image/png
  • a2z-alphabetical-archive-links/tags/2.1.0/a2z-alphabetical-archive-links.php

    r2019541 r3193532  
    11<?php
    2     /*
    3         Plugin Name: A2Z Alphabetical Archive Links
    4         Plugin URI: https://github.com/NowellVanHoesen/a2z-alphabetical-archive-links/wiki
    5         Description: Get a list of characters, A to Z, from the initial character of a post or CPT title. The Initials will link to an archive page of posts/CPTs that begin with that character.
    6         Version: 2.0.2
    7         Author: Nowell VanHoesen
    8         Author URI: http://nvwebdev.com/
    9         Author Email: nowell@nvwebdev.com
    10         License: GPL-2.0+
    11         Licanse URI: https://www.gnu.org/licenses/gpl.html
    12         Text Domain: nvwd-a2zaal
    13     */
    14 
    15     if ( ! defined( 'WPINC' ) ) {
    16         die;
    17     }
    18 
    19     define( 'A2ZAAL_PLUGIN', __FILE__ );
    20     define( 'A2ZAAL_BASENAME', plugin_basename( A2ZAAL_PLUGIN ) );
    21     define( 'A2ZAAL_VERSION', '2.0.2' );
    22     define( 'A2ZAAL_PLUGIN_ROOT_DIR', trailingslashit( __DIR__ ) );
    23 
    24     $a2zaal_url = plugin_dir_url( A2ZAAL_PLUGIN );
    25     if ( is_ssl() ) {
    26         $a2zaal_url = str_replace( 'http://', 'https://', $a2zaal_url );
    27     }
    28 
    29     define( 'A2ZAAL_ROOT_URL', $a2zaal_url );
    30     define( 'A2ZAAL_TEXT_DOMAIN', 'nvwd-a2zaal' );
    31 
    32     define( 'A2ZAAL_PHP_MIN_VERIONS', '5.6' );
    33     define( 'A2ZAAL_WP_MIN_VERSIONS', '4.6.0' );
    34 
    35     register_activation_hook( A2ZAAL_PLUGIN, 'a2zaal_activation_check' );
    36     register_deactivation_hook( A2ZAAL_PLUGIN, 'a2zaal_deactivate' );
    37     register_uninstall_hook( A2ZAAL_PLUGIN, 'a2zaal_uninstall' );
    38 
    39     add_action( 'admin_init', 'a2zaal_verify_versions' );
    40 
    41     /* do version checks before including the rest of the plugin code */
    42     if ( is_wp_error( a2zaal_version_checks() ) ) {
     2/*
     3    Plugin Name: A2Z Alphabetical Archive Links
     4    Plugin URI: https://github.com/NowellVanHoesen/a2z-alphabetical-archive-links/wiki
     5    Description: Get a list of characters, A to Z, from the initial character of a post or CPT title.
     6                The Initials will link to an archive page of posts/CPTs that begin with that character.
     7    Version: 2.1.0
     8    Requires at least: 5.8
     9    Requires PHP: 7.4
     10    Author: Nowell VanHoesen
     11    Author URI: http://nvwebdev.com/
     12    Author Email: nowell@nvwebdev.com
     13    License: GPL-2.0+
     14    License URI: https://www.gnu.org/licenses/gpl.html
     15    Text Domain: nvwd-a2zaal
     16*/
     17
     18namespace NVWD\A2ZAAL;
     19
     20if ( ! defined( 'WPINC' ) ) {
     21    die;
     22}
     23
     24define( 'A2ZAAL_PLUGIN', __FILE__ );
     25define( 'A2ZAAL_BASENAME', plugin_basename( A2ZAAL_PLUGIN ) );
     26define( 'A2ZAAL_VERSION', '2.1.0' );
     27define( 'A2ZAAL_PLUGIN_ROOT_DIR', trailingslashit( __DIR__ ) );
     28
     29$a2zaal_url = plugin_dir_url( A2ZAAL_PLUGIN );
     30
     31if ( is_ssl() ) {
     32    $a2zaal_url = str_replace( 'http://', 'https://', $a2zaal_url );
     33}
     34
     35define( 'A2ZAAL_ROOT_URL', $a2zaal_url );
     36
     37define( 'A2ZAAL_PHP_MIN_VERSION', '7.4' );
     38define( 'A2ZAAL_WP_MIN_VERSION', '5.8' );
     39
     40register_activation_hook( A2ZAAL_PLUGIN, 'a2zaal_activation_check' );
     41register_deactivation_hook( A2ZAAL_PLUGIN, 'a2zaal_deactivate' );
     42register_uninstall_hook( A2ZAAL_PLUGIN, 'a2zaal_uninstall' );
     43
     44add_filter( 'a2zaal_do_version_checks', __NAMESPACE__ . '\a2zaal_check_php_version' );
     45add_filter( 'a2zaal_do_version_checks', __NAMESPACE__ . '\a2zaal_check_wp_version' );
     46add_action( 'admin_init', __NAMESPACE__ . '\a2zaal_verify_versions' );
     47
     48/* do version checks before including the rest of the plugin code */
     49if ( is_wp_error( a2zaal_version_checks() ) ) {
     50    return;
     51}
     52
     53/* version checks complete let's get the party started */
     54require A2ZAAL_PLUGIN_ROOT_DIR . 'src/plugin.php';
     55
     56/**
     57 * Plugin activation script
     58 * initiate version checks, disable plugin if any fail
     59 *
     60 * @author nvwd
     61 *
     62 * @since 2.0.0
     63 */
     64function a2zaal_activation_check() {
     65    $a2zaal_activation_check = a2zaal_version_checks();
     66
     67    if ( ! is_wp_error( $a2zaal_activation_check ) ) {
    4368        return;
    4469    }
    4570
    46     /* version checks complete let's get the party started */
    47     include( A2ZAAL_PLUGIN_ROOT_DIR . 'src/plugin.php' );
    48 
     71    deactivate_plugins( A2ZAAL_BASENAME );
     72}
     73
     74/**
     75 * Plugin deactivate script
     76 *
     77 * @author nvwd
     78 *
     79 * @since 2.0.0
     80 */
     81function a2zaal_deactivate() {
    4982    /**
    50      * plugin activation script
    51      * initiate version checks, disable plugin if any fail
    52      *
    53      * @author: nvwd
    54      *
    55      * @since: 2.0.0
    56      *
    57      * @return void
     83     * Remove background processing
     84     * remove any options
     85     * remove any activated post type data
    5886     */
    59     function a2zaal_activation_check() {
    60         $a2zaal_activation_check = a2zaal_version_checks();
    61 
    62         if ( is_wp_error( $a2zaal_activation_check ) ) {
    63             deactivate_plugins( A2ZAAL_BASENAME );
    64             return;
     87    do_action( 'a2zaal_deactivation' );
     88    a2zaal_clear_rewrite_rules();
     89}
     90
     91/**
     92 * Plugin uninstall script
     93 *
     94 * @author nvwd
     95 *
     96 * @since 2.0.0
     97 */
     98function a2zaal_uninstall() {
     99
     100    $a2zaal_active_post_types = get_a2zaal_active_post_types();
     101
     102    foreach ( $a2zaal_active_post_types as $active_cpt ) {
     103        remove_disabled_cpts( $active_cpt );
     104    }
     105
     106    delete_option( 'a2zaal_post_types' );
     107}
     108
     109/**
     110 * Delete rewrite rules option wrapper
     111 *
     112 * @author nvwd
     113 *
     114 * @since 2.0.0
     115 */
     116function a2zaal_clear_rewrite_rules() {
     117    delete_option( 'rewrite_rules' );
     118}
     119
     120/**
     121 * Main version check function
     122 * disable plugin if version check(s) fail
     123 *
     124 * @author nvwd
     125 *
     126 * @since 2.0.0
     127 */
     128function a2zaal_verify_versions() {
     129    $a2zaal_activation_check = a2zaal_version_checks();
     130
     131    if ( ! is_wp_error( $a2zaal_activation_check ) || ! is_plugin_active( A2ZAAL_BASENAME ) ) {
     132        return;
     133    }
     134
     135    deactivate_plugins( A2ZAAL_BASENAME );
     136
     137    // phpcs:disable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable, WordPress.Security.NonceVerification.Recommended -- plugin activation failed, need to remove the param so nothing else is processed.
     138    if ( isset( $_GET['activate'] ) ) {
     139        unset( $_GET['activate'] );
     140    }
     141    // phpcs:enable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
     142}
     143
     144/**
     145 * Version check controller
     146 *
     147 * @author nvwd
     148 *
     149 * @since 2.0.0
     150 *
     151 * @return bool|\WP_Error
     152 */
     153function a2zaal_version_checks() {
     154    // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName -- Slevomat can't make up its mind with or without the '\', so I'm opting for with.
     155    $version_check_errors = new \WP_Error();
     156
     157    $version_check_errors = apply_filters( 'a2zaal_do_version_checks', $version_check_errors );
     158
     159    $error_codes = $version_check_errors->get_error_codes();
     160
     161    if ( ! empty( $error_codes ) ) {
     162        return $version_check_errors;
     163    }
     164
     165    return true;
     166}
     167
     168/**
     169 * Check WP version
     170 *
     171 * @author nvwd
     172 *
     173 * @since 2.0.0
     174 *
     175 * @param \WP_Error $version_check_errors version check errors.
     176 *
     177 * @return \WP_Error
     178 *
     179 * phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName -- Slevomat can't make up its mind with or without the '\', so I'm opting for with.
     180 */
     181function a2zaal_check_wp_version( \WP_Error $version_check_errors ) {
     182    global $wp_version;
     183
     184    if ( version_compare( $wp_version, A2ZAAL_WP_MIN_VERSION, '>=' ) ) {
     185        return $version_check_errors;
     186    }
     187
     188    add_action( 'admin_notices', 'a2zaal_wp_version_failure_message' );
     189
     190    $version_check_errors->add(
     191        'wp_version',
     192        esc_html__(
     193            'WordPress version check for A2Z Alphabetical Archive Links failed.
     194            A2Z Alphabetical Archive Links should not be active.',
     195            'nvwd-a2zaal'
     196        )
     197    );
     198
     199    return $version_check_errors;
     200}
     201// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
     202
     203/**
     204 * Check PHP version
     205 *
     206 * @author nvwd
     207 *
     208 * @since 2.0.0
     209 *
     210 * @param \WP_Error $version_check_errors version check errors.
     211 *
     212 * @return \WP_Error
     213 *
     214 * phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName -- Slevomat can't make up its mind with or without the '\', so I'm opting for with.
     215 */
     216function a2zaal_check_php_version( \WP_Error $version_check_errors ) {
     217    if ( version_compare( PHP_VERSION, A2ZAAL_PHP_MIN_VERSION, '>=' ) ) {
     218        return $version_check_errors;
     219    }
     220
     221    add_action( 'admin_notices', 'a2zaal_php_version_failure_message' );
     222
     223    $version_check_errors->add(
     224        'php_version',
     225        esc_html__(
     226            'PHP version check for A2Z Alphabetical Archive Links failed.
     227            A2Z Alphabetical Archive Links should not be active.',
     228            'nvwd-a2zaal'
     229        )
     230    );
     231
     232    return $version_check_errors;
     233}
     234// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
     235
     236/**
     237 * Create admin notice of WP version check failure
     238 *
     239 * @author nvwd
     240 *
     241 * @since 2.0.0
     242 */
     243function a2zaal_wp_version_failure_message() {
     244    $css_class = 'notice notice-error';
     245    $message   = sprintf(
     246        /* translators: 1: Minimum WordPress version */
     247        __(
     248            'A2Z Alphabetical Archive Links requires WordPress %1$s to function properly. Please upgrade WordPress.
     249            A2Z Alphabetical Archive Links has been auto-deactivated.',
     250            'nvwd-a2zaal'
     251        ),
     252        A2ZAAL_WP_MIN_VERSION
     253    );
     254
     255    a2zaal_output_admin_notice( $css_class, $message );
     256}
     257
     258/**
     259 * Create admin notice of PHP version check failure
     260 *
     261 * @author nvwd
     262 *
     263 * @since 2.0.0
     264 */
     265function a2zaal_php_version_failure_message() {
     266    $css_class = 'notice notice-error';
     267    $message   = sprintf(
     268        /* translators: 1: Minimum php version */
     269        __(
     270            'A2Z Alphabetical Archive Links requires PHP %1$s to function properly. Please upgrade PHP.
     271            A2Z Alphabetical Archive Links has been auto-deactivated.',
     272            'nvwd-a2zaal'
     273        ),
     274        A2ZAAL_PHP_MIN_VERSION
     275    );
     276
     277    a2zaal_output_admin_notice( $css_class, $message );
     278}
     279
     280/**
     281 * Display admin notice
     282 *
     283 * @author nvwd
     284 *
     285 * @since 2.0.0
     286 *
     287 * @param string $css_class css class for the message output.
     288 * @param string $message message to display.
     289 */
     290function a2zaal_output_admin_notice( string $css_class, string $message ) {
     291    printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $css_class ), esc_html( $message ) );
     292}
     293
     294if ( function_exists( 'register_block_type' ) ) {
     295    add_action(
     296        'init',
     297        static function () {
     298            register_block_type( __DIR__ . '/build/blocks/a2z-links' );
    65299        }
    66 
    67         return;
    68     }
    69 
    70     /**
    71      * plugin deactivate script
    72      *
    73      * @author: nvwd
    74      *
    75      * @since: 2.0.0
    76      *
    77      * @return void
    78      */
    79     function a2zaal_deactivate() {
    80         /**
    81          * remove background processing
    82          * remove any options
    83          * remove any activated post type data
    84          */
    85         do_action( 'a2zaal_deactivation' );
    86         a2zaal_clear_rewrite_rules();
    87     }
    88 
    89     /**
    90      * plugin uninstall script
    91      *
    92      * @author: nvwd
    93      *
    94      * @since: 2.0.0
    95      *
    96      * @return void
    97      */
    98     function a2zaal_uninstall() {
    99 
    100         $a2zaal_active_post_types = NVWD\A2ZAAL\get_a2zaal_active_post_types();
    101 
    102         foreach ( $a2zaal_active_post_types AS $active_cpt ) {
    103             NVWD\A2ZAAL\remove_disabled_taxonomy_terms( $active_cpt );
    104         }
    105 
    106         delete_option( 'a2zaal_post_types' );
    107 
    108     }
    109 
    110     /**
    111      * delete rewrite rules option wrapper
    112      *
    113      * @author: nvwd
    114      *
    115      * @since: 2.0.0
    116      *
    117      * @return void
    118      */
    119     function a2zaal_clear_rewrite_rules() {
    120         delete_option( 'rewrite_rules' );
    121     }
    122 
    123     /**
    124      * main version check function
    125      * disable plugin if version check(s) fail
    126      *
    127      * @author: nvwd
    128      *
    129      * @since: 2.0.0
    130      *
    131      * @return void
    132      */
    133     function a2zaal_verify_versions() {
    134         $a2zaal_activation_check = a2zaal_version_checks();
    135 
    136         if ( ! is_wp_error( $a2zaal_activation_check ) ) {
    137             return;
    138         }
    139 
    140         if ( ! is_plugin_active( A2ZAAL_BASENAME ) ) {
    141             return;
    142         }
    143 
    144         deactivate_plugins( A2ZAAL_BASENAME );
    145 
    146         if ( isset( $_GET['activate'] ) ) {
    147             unset( $_GET['activate'] );
    148         }
    149     }
    150 
    151     /**
    152      * version check controller
    153      *
    154      * @author: nvwd
    155      *
    156      * @since: 2.0.0
    157      *
    158      * @return bool|WP_Error
    159      */
    160     function a2zaal_version_checks() {
    161         $version_check_errors = new WP_Error();
    162 
    163         $version_check_errors = apply_filters( 'a2zaal_do_version_checks', $version_check_errors );
    164 
    165         $error_codes = $version_check_errors->get_error_codes();
    166         if ( ! empty( $error_codes ) ) {
    167             return $version_check_errors;
    168         }
    169 
    170         return true;
    171     }
    172 
    173     add_filter( 'a2zaal_do_version_checks', 'a2zaal_check_wp_version' );
    174     /**
    175      * Check WP version
    176      *
    177      * @author: nvwd
    178      *
    179      * @since: 2.0.0
    180      *
    181      * @param $version_check_errors
    182      *
    183      * @return WP_Error
    184      */
    185     function a2zaal_check_wp_version( $version_check_errors ) {
    186         global $wp_version;
    187 
    188         if ( version_compare( $wp_version, A2ZAAL_WP_MIN_VERSIONS, '>=' ) ) {
    189             return $version_check_errors;
    190         }
    191 
    192         add_action( 'admin_notices', 'a2zaal_wp_version_failure_message' );
    193 
    194         $version_check_errors->add(
    195             'wp_version',
    196             esc_html__('WordPress version check for A2Z Alphabetical Archive Links failed. A2Z Alphabetical Archive Links should not be active.', A2ZAAL_TEXT_DOMAIN )
    197         );
    198 
    199         return $version_check_errors;
    200     }
    201 
    202     add_filter( 'a2zaal_do_version_checks', 'a2zaal_check_php_version' );
    203     /**
    204      * Check PHP version
    205      *
    206      * @author: nvwd
    207      *
    208      * @since: 2.0.0
    209      *
    210      * @param $version_check_errors
    211      *
    212      * @return WP_Error
    213      */
    214     function a2zaal_check_php_version( $version_check_errors ) {
    215         if ( version_compare( PHP_VERSION, A2ZAAL_PHP_MIN_VERIONS, '>=' ) ) {
    216             return $version_check_errors;
    217         }
    218 
    219         add_action( 'admin_notices', 'a2zaal_php_version_failure_message' );
    220 
    221         $version_check_errors->add(
    222             'php_version',
    223             esc_html__('PHP version check for A2Z Alphabetical Archive Links failed. A2Z Alphabetical Archive Links should not be active.', A2ZAAL_TEXT_DOMAIN )
    224         );
    225 
    226         return $version_check_errors;
    227     }
    228 
    229     /**
    230      * Create admin notice of WP version check failure
    231      *
    232      * @author: nvwd
    233      *
    234      * @since: 2.0.0
    235      *
    236      * @return void
    237      */
    238     function a2zaal_wp_version_failure_message() {
    239         $class = 'notice notice-error';
    240         $message = __( 'A2Z Alphabetical Archive Links requires WordPress ' . A2ZAAL_WP_MIN_VERSIONS . ' to function properly. Please upgrade WordPress. A2Z Alphabetical Archive Links has been auto-deactivated.', A2ZAAL_TEXT_DOMAIN );
    241 
    242         a2zaal_output_admin_notice( $class, $message );
    243     }
    244 
    245     /**
    246      * Create admin notice of PHP version check failure
    247      *
    248      * @author: nvwd
    249      *
    250      * @since: 2.0.0
    251      *
    252      * @return void
    253      */
    254     function a2zaal_php_version_failure_message() {
    255         $class = 'notice notice-error';
    256         $message = __( 'A2Z Alphabetical Archive Links requires PHP ' . A2ZAAL_PHP_MIN_VERIONS . ' to function properly. Please upgrade PHP. A2Z Alphabetical Archive Links has been auto-deactivated.', A2ZAAL_TEXT_DOMAIN );
    257 
    258         a2zaal_output_admin_notice( $class, $message );
    259     }
    260 
    261     /**
    262      * Display admin notice
    263      *
    264      * @author: nvwd
    265      *
    266      * @since: 2.0.0
    267      *
    268      * @param $class
    269      * @param $message
    270      *
    271      * @return void
    272      */
    273     function a2zaal_output_admin_notice( $class, $message ) {
    274         printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
    275     }
     300    );
     301}
  • a2z-alphabetical-archive-links/tags/2.1.0/css/display.css

    r2015868 r3193532  
    11/* CSS Document */
     2ul.wp-block-nvwda2zaal-a2z-links,
    23.a2zaal_widget ul {
    34    display: grid;
     
    78    margin: 0;
    89}
     10ul.wp-block-nvwda2zaal-a2z-links.counts,
    911.a2zaal_widget ul.counts {
    1012    grid-template-columns: repeat( auto-fill, minmax( 3rem, 1fr ) );
    1113}
     14.wp-block-nvwda2zaal-a2z-links li,
    1215.a2zaal_widget li {
    1316    position: relative;
    1417}
    15 
     18.wp-block-nvwda2zaal-a2z-links a,
    1619.a2zaal_widget a {
    1720    border-bottom: 1px solid transparent;
     
    2023    text-align: center;
    2124}
    22 
     25.wp-block-nvwda2zaal-a2z-links a:hover,
    2326.a2zaal_widget a:hover {
    2427    border-bottom: 1px solid blue;
     
    2629    text-decoration: underline;
    2730}
     31.wp-block-nvwda2zaal-a2z-links a.count > span,
    2832.a2zaal_widget a.count > span {
    2933    font-size: 75%;
     
    3236    white-space: nowrap;
    3337}
    34 /* premium styles for sliding counts
     38/*
     39ul.wp-block-nvwda2zaal-a2z-links.counts,
    3540.a2zaal_widget ul.counts {
    3641    grid-template-columns: repeat( auto-fit, minmax( 2.5em, 1fr ) );
     42    gap: 5px;
    3743}
     44.wp-block-nvwda2zaal-a2z-links li.count:after,
    3845.a2zaal_widget li.count:after {
    3946    content: '';
     
    5057    box-shadow: inset 1px 0 #99400e;
    5158}
    52 .a2zaal_widget li a, .a2zaal_widget li a span {
     59.wp-block-nvwda2zaal-a2z-links li a,
     60.wp-block-nvwda2zaal-a2z-links li a span,
     61.a2zaal_widget li a,
     62.a2zaal_widget li a span {
    5363    display: block;
    5464    -webkit-box-sizing: border-box;
     
    5767    white-space: nowrap;
    5868}
     69.wp-block-nvwda2zaal-a2z-links li a,
    5970.a2zaal_widget li a {
    6071    padding: 0.25em 0.5em;
     
    7687    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.7), 0 1px 2px rgba(0, 0, 0, 0.05);
    7788}
     89.wp-block-nvwda2zaal-a2z-links a:hover span,
    7890.a2zaal_widget a:hover span {
    7991    padding: 0.2em 7px 0.2em 6px;
     
    8294    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.15), 1px 1px 2px rgba(0, 0, 0, 0.2);
    8395}
     96.wp-block-nvwda2zaal-a2z-links span,
    8497.a2zaal_widget span {
    8598    position: absolute;
  • a2z-alphabetical-archive-links/tags/2.1.0/readme.txt

    r2019541 r3193532  
    22Contributors: nvwd
    33Donate link: http://nvwebdev.com
    4 Tags: post title, custom post type title, cpt title, title, alphabetical, alphabatized
    5 Requires at least: 4.6.0
    6 Tested up to: 5.0.3
    7 Requires PHP: 5.6
    8 Stable tag: 2.0.2
     4Tags: post title, custom post type title, cpt title, title, alphabetical, alphabetized
     5Requires at least: 5.8.0
     6Tested up to: 6.7
     7Requires PHP: 7.4
     8Stable tag: 2.1.0
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    36361. Activate the plugin through the 'Plugins' menu in WordPress
    37371. Go to the settings page to activate which post type(s) to have
    38 1. Add the 'A2Z Alphabetical Archive Links' widget to a widget area
     381. Add the 'A2Z Alphabetical Archive Links' widget to a widget area or add the A2Z Links block to a block area
    3939
    4040== Frequently Asked Questions ==
     
    4444== Screenshots ==
    4545
    46 1. Widget config
    47 1. Links without count
    48 1. Links with count (top), hover example ( bottom )
     461. Legacy widget config
     471. A2Z Links block editor with counts disabled
     481. A2Z Links block editor with counts enabled
     491. A2Z Links block output with counts disabled
     501. A2Z Links block output with counts enabled
    4951
    5052== Changelog ==
     53
     54= 2.1.0 =
     55
     56* Added links block to replace legacy widget
     57* Updated minimum php version to 7.4
     58* Updated minimum WordPress version to 5.8
     59* Resolved RegEx issue that incorrectly grouped some titles in the group that begin with numbers.
    5160
    5261= 2.0.2 =
     
    5665= 2.0.1 =
    5766
    58 * fixed bug with earlier versions of php and version checking
     67fixed bug with earlier versions of php and version checking
    5968
    6069= 2.0.0 =
    6170
    62 Complete rewrite. In an effort to make the code scale better.
     71* Complete rewrite. In an effort to make the code scale better.
    6372* Removed slow query
    6473* Added minimum WordPress version check
     
    7382= 1.0.1 =
    7483
    75 * Changed widget option for post type to include only publically queriable post types ( Pages not an option anymore )
     84* Changed widget option for post type to include only publicly queryable post types ( Pages not an option anymore )
    7685* Fixed links generated when using the post type of Post
    7786
  • a2z-alphabetical-archive-links/tags/2.1.0/src/helpers.php

    r2015868 r3193532  
    11<?php
    2 /**
    3  * A2Z Alphabetical Archive Links Helper Functions
    4  *
    5  * @package     NVWD\A2ZAAL
    6  * @since       2.0.0
    7  * @author      nvwd
    8  * @link        http://nvwebdev.com/
    9  * @license     GPL-2.0+
    10  */
    112
    123namespace NVWD\A2ZAAL;
    134
    14 /**
    15  * return an array of a2zaal active post types or an empty array when none are active
    16  *
    17  * @author: nvwd
    18  * @since: 2.0.0
     5use WP_Post;
     6
     7add_filter( 'widget_block_dynamic_classname', __NAMESPACE__ . '\add_widget_classname', 10, 2 );
     8
     9/**
     10 * Return an array of a2zaal active post types or an empty array when none are active
     11 *
     12 * @author nvwd
     13 *
     14 * @since 2.0.0
     15 *
    1916 * @return array
    2017 */
    2118function get_a2zaal_active_post_types() {
    22     $a2zaal_active_post_types = get_option( 'a2zaal_post_types', array() );
     19    $a2zaal_active_post_types = \get_option( 'a2zaal_post_types', [] );
    2320
    2421    if ( ! is_array( $a2zaal_active_post_types ) ) {
    25         return array();
     22        return [];
    2623    }
    2724
    2825    return $a2zaal_active_post_types;
    29 
    30 }
    31 
    32 /**
    33  * return an array of a2zaal post types that are currently processing or an empty array
    34  *
    35  * @author: nvwd
    36  * @since: 2.0.0
     26}
     27
     28/**
     29 * Return an array of a2zaal post types that are currently processing or an empty array
     30 *
     31 * @author nvwd
     32 *
     33 * @since 2.0.0
     34 *
    3735 * @return array
    3836 */
    3937function get_a2zaal_processing_post_types() {
    40     $a2zaal_processing_objects = get_option( 'a2zaal_processing_objects', array() );
     38    $a2zaal_processing_objects = \get_option( 'a2zaal_processing_objects', [] );
    4139
    4240    if ( empty( $a2zaal_processing_objects['post_types'] ) ) {
    43         return array();
     41        return [];
    4442    }
    4543
    4644    return $a2zaal_processing_objects['post_types'];
    47 
    48 }
    49 
    50 /**
    51  * this is where the magic happens, return the first character of the provided title and the string used for sorting
     45}
     46
     47/**
     48 * This is where the magic happens, return the first character of the provided title and the string used for sorting
    5249 * if the title begins with one or more 'a', 'an', 'and', and/or 'the', ignore them and goto the next word
    5350 * if the title then begins with a number return '#', otherwise return the first alpha character, capitalized.
    5451 * return the adjusted title for proper sorting when the links are retireved
    5552 *
    56  * @author: nvwd
    57  * @since: 2.0.0
    58  * @param string    $post_title
     53 * @author nvwd
     54 *
     55 * @since 2.0.0
     56 *
     57 * @param string $post_title Post title being processed for its grouping initial.
     58 *
    5959 * @return bool|array
    6060 */
    61 function get_post_a2zaal_info( $post_title ) {
     61function get_post_a2zaal_info( string $post_title ) {
    6262    if ( empty( $post_title ) ) {
    6363        return false;
    6464    }
    65     preg_match( '/\A[(a|an|and|the) ]*\b(([a-zA-Z]{1}|[\d]{1,}).*)\z/i', $post_title, $a2zaal_char );
    66     $post_a2zaal_info = array( 'sort_title' => $a2zaal_char[1], 'initial' => strtoupper( $a2zaal_char[2] ) );
     65
     66    // Regex explained: https://regex101.com/r/B8vbdX/1.
     67    preg_match( '/\A(?|a |an |and |the )*(([a-zA-Z]{1}|[\d]{1,}).*)\z/i', $post_title, $a2zaal_char );
     68    $post_a2zaal_info = [
     69        'sort_title' => $a2zaal_char[1],
     70        'initial'    => strtoupper( $a2zaal_char[2] ),
     71    ];
    6772
    6873    if ( is_numeric( $post_a2zaal_info['initial'] ) ) {
     
    7479
    7580/**
    76  * check if the query is an a2z query needing some extra stuff
    77  *
    78  * @author: nvwd
    79  * @since: 2.0.0
    80  * @return bool
     81 * Check if the query is an a2z query needing some extra stuff
     82 *
     83 * @author nvwd
     84 *
     85 * @since 2.0.0
     86 *
     87 * @return bool|string
    8188 */
    8289function is_a2zaal_query() {
    83 
    8490    if ( ! \is_main_query() || ! \is_post_type_archive() ) {
    8591        return false;
    8692    }
    8793
    88     if ( false === \get_query_var( A2ZAAL_REWRITE_TAG, false ) ) {
    89         return false;
    90     }
    91 
    92     return true;
    93 
    94 }
    95 
    96 /**
    97  * add sortable title and post ID to the structure used to order the posts alphabetically
    98  *
    99  * @author: nvwd
    100  * @since: 2.0.0
    101  * @param   WP_Post   $post     post being saved
    102  * @return void
    103  */
    104 function add_post_a2zaal_info( $post ) {
    105     $a2zaal_cpt_option = \get_option( $post->post_type . A2ZAAL_POSTS_SUFFIX, array() );
    106     $post_a2zaal_info = namespace\get_post_a2zaal_info( $post->post_title );
    107 
    108     if ( ! is_array( $post_a2zaal_info ) || ( array_key_exists( $post_a2zaal_info['initial'], $a2zaal_cpt_option ) && array_key_exists( $post->ID, $a2zaal_cpt_option[ $post_a2zaal_info['initial'] ] ) ) ) {
     94    /* phpcs:ignore SlevomatCodingStandard.ControlStructures.UselessTernaryOperator.UselessTernaryOperator -- returning the results of get_query_var is not reliable due to one of the values being 0, making it false. */
     95    return false === \get_query_var( A2ZAAL_REWRITE_TAG, false ) ? false : true;
     96}
     97
     98/**
     99 * Add sortable title and post ID to the structure used to order the posts alphabetically
     100 *
     101 * @author nvwd
     102 *
     103 * @since 2.0.0
     104 *
     105 * @param \WP_Post $post post being saved.
     106 */
     107function add_post_a2zaal_info( WP_Post $post ) {
     108    $a2zaal_cpt_option = \get_option( $post->post_type . A2ZAAL_POSTS_SUFFIX, [] );
     109    $post_a2zaal_info  = get_post_a2zaal_info( $post->post_title );
     110
     111    if (
     112        ! is_array( $post_a2zaal_info )
     113        || (
     114            array_key_exists( $post_a2zaal_info['initial'], $a2zaal_cpt_option )
     115            && array_key_exists( $post->ID, $a2zaal_cpt_option[ $post_a2zaal_info['initial'] ] )
     116        )
     117    ) {
    109118        return;
    110119    }
    111120
    112     // in the case of an update, the post may already exist in the a2z post info structure
    113     // ensure that the post doesn't already exist in the structure
    114     namespace\remove_prior_post_a2zaal_data( $a2zaal_cpt_option, $post->ID, $post_a2zaal_info['initial'] );
     121    // in the case of an update, the post may already exist in the a2z post info structure.
     122    // ensure that the post doesn't already exist in the structure.
     123    remove_prior_post_a2zaal_data( $a2zaal_cpt_option, $post->ID, $post_a2zaal_info['initial'] );
    115124
    116125    $a2zaal_cpt_option[ $post_a2zaal_info['initial'] ][ $post->ID ] = $post_a2zaal_info['sort_title'];
    117126
    118127    \update_option( $post->post_type . A2ZAAL_POSTS_SUFFIX, $a2zaal_cpt_option, true );
    119 
    120 }
    121 
    122 /**
    123  * in the case of a post update, check to see if the post already exists within the structure
     128}
     129
     130/**
     131 * In the case of a post update, check to see if the post already exists within the structure
    124132 * remove it if necessary
    125133 *
    126  * @author: nvwd
    127  * @since: 2.0.0
    128  * @param   array   $a2zaal_cpt_option  contains the post alphabetical detail for this post type
    129  * @param   int     $post_id            current post ID to possibly remove a previous entry
    130  * @param   string  $post_initial       initial of the current post title
    131  * @return void
    132  */
    133 function remove_prior_post_a2zaal_data( &$a2zaal_cpt_option, $post_id, $post_initial ) {
    134     foreach( $a2zaal_cpt_option AS $key => $initial_posts ) {
    135         if ( $key == $post_initial ) {
     134 * @author nvwd
     135 *
     136 * @since 2.0.0
     137 *
     138 * @param array  $a2zaal_cpt_option contains the post alphabetical detail for this post type.
     139 * @param int    $post_id current post ID to possibly remove a previous entry.
     140 * @param string $post_initial initial of the current post title.
     141 */
     142function remove_prior_post_a2zaal_data( array &$a2zaal_cpt_option, int $post_id, string $post_initial ) {
     143    foreach ( $a2zaal_cpt_option as $key => $initial_posts ) {
     144        if ( $key === $post_initial || ! array_key_exists( $post_id, $initial_posts ) ) {
    136145            continue;
    137146        }
    138         if ( array_key_exists( $post_id, $initial_posts ) ) {
    139             unset( $a2zaal_cpt_option[$key][$post_id] );
     147
     148        unset( $a2zaal_cpt_option[ $key ][ $post_id ] );
     149    }
     150}
     151
     152/**
     153 * Returns the array of a2z links for the widget/block to display.
     154 *
     155 * @author nvwd
     156 *
     157 * @since 2.1.0
     158 *
     159 * @param string $selected_post_type selected post type for the links.
     160 * @param bool   $show_counts whether to display counts for the initial groups.
     161 *
     162 * @return array
     163 */
     164function get_a2zaal_display_links( string $selected_post_type, bool $show_counts ) {
     165    $post_type_titles_struct = \get_option( $selected_post_type . A2ZAAL_POSTS_SUFFIX, [] );
     166
     167    if ( empty( $post_type_titles_struct ) ) {
     168        return [ '<p>' . \__( 'No links to display.', 'nvwd-a2zaal' ) . '</p>' ];
     169    }
     170
     171    $post_type_labels = \get_post_type_object( $selected_post_type )->labels;
     172
     173    ksort( $post_type_titles_struct, SORT_NATURAL );
     174
     175    /* translators: %s: plural post type name */
     176    $link_prefix = sprintf( \__( '%s beginning with ', 'nvwd-a2zaal' ), $post_type_labels->name );
     177
     178    foreach ( $post_type_titles_struct as $title_initial => $grouped_titles ) {
     179        $group_link          = '/' . $selected_post_type . '/' . A2ZAAL_REWRITE_TAG . '/' . $title_initial;
     180        $group_count_display = ! empty( $show_counts )
     181            ? '<span>' . \number_format_i18n( count( $grouped_titles ) ) . '</span>'
     182            : '';
     183        $link_classes        = ! empty( $show_counts ) ? [ 'count' ] : [];
     184        $link_classes        = implode( ' ', \apply_filters( 'a2zaal_link_css_class', $link_classes, $selected_post_type ) );
     185        $link_title          = trim(
     186            \apply_filters( 'a2zaal_link_title', $link_prefix . $title_initial, $selected_post_type, $title_initial )
     187        );
     188        $link_text_display   = $title_initial;
     189
     190        if ( 0 === $title_initial ) {
     191            $group_link        = '/' . $selected_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num';
     192            $link_text_display = '#';
    140193        }
    141     }
    142 }
     194
     195        // TODO: Make sure link is accessible.
     196        $display_links[] = sprintf(
     197            '<li><a href="%s" class="%s" title="%s">%s%s</a></li>',
     198            $group_link,
     199            $link_classes,
     200            $link_title,
     201            $link_text_display,
     202            $group_count_display
     203        );
     204    }
     205
     206    return $display_links;
     207}
     208
     209/**
     210 * Returns adjusted className list for a2z-links widget wrapper.
     211 *
     212 * @author nvwd
     213 *
     214 * @since 2.1.0
     215 *
     216 * @param string $classname list of classnames to be used in the block widget’s container HTML.
     217 * @param string $block_name name of the block contained by the block widget.
     218 *
     219 * @return string
     220 */
     221function add_widget_classname( string $classname, string $block_name ) {
     222    if ( 'nvwda2zaal/a2z-links' !== $block_name ) {
     223        return $classname;
     224    }
     225
     226    // phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable -- unused even though it's part of the return.
     227    return $classname .= ' a2zaal_widget';
     228}
  • a2z-alphabetical-archive-links/tags/2.1.0/src/plugin.php

    r2015868 r3193532  
    11<?php
    2 /**
    3  * A2Z Alphabetical Archive Links Handler
    4  *
    5  * @package     NVWD\A2ZAAL
    6  * @since       2.0.0
    7  * @author      nvwd
    8  * @link        http://nvwebdev.com/
    9  * @license     GPL-2.0+
    10  */
    112
    123namespace NVWD\A2ZAAL;
     
    178define( 'A2ZAAL_VIEW_DIR', A2ZAAL_SOURCE_DIR . 'views/' );
    189
    19 include A2ZAAL_SOURCE_DIR . 'vender/wp-background-processing/wp-background-processing.php';
    20 include A2ZAAL_SOURCE_DIR . 'classes/A2ZAAL_Post_Type_Background_Process.php';
     10require A2ZAAL_SOURCE_DIR . 'vender/wp-background-processing/wp-background-processing.php';
     11require A2ZAAL_SOURCE_DIR . 'classes/class-a2zaal-post-type-background-process.php';
    2112
    2213if ( is_admin() ) {
    23     include A2ZAAL_SOURCE_DIR . 'settings.php';
     14    require A2ZAAL_SOURCE_DIR . 'settings.php';
    2415}
    25 include A2ZAAL_SOURCE_DIR . 'rewrites.php';
    26 include A2ZAAL_SOURCE_DIR . 'helpers.php';
    27 include A2ZAAL_SOURCE_DIR . 'widgets/a2zaal_widget.php';
     16
     17require A2ZAAL_SOURCE_DIR . 'rewrites.php';
     18require A2ZAAL_SOURCE_DIR . 'helpers.php';
     19require A2ZAAL_SOURCE_DIR . 'widgets/class-a2zaal-widget.php';
     20
     21\add_action( 'widgets_init', __NAMESPACE__ . '\maybe_register_archive_link_widget' );
     22
     23/**
     24 * Register the a2zaal widget only if there are active post types
     25 *
     26 * @author nvwd
     27 *
     28 * @since 2.0.0
     29 */
     30function maybe_register_archive_link_widget() {
     31    $a2z_active_post_types = get_a2zaal_active_post_types();
     32
     33    if ( empty( $a2z_active_post_types ) ) {
     34        return;
     35    }
     36
     37    \register_widget( __NAMESPACE__ . '\a2zaal_widget' );
     38}
    2839
    2940add_action( 'save_post', __NAMESPACE__ . '\maybe_add_a2zaal_post' );
    3041/**
    31  * add the a2zaal data to the specific structure when an active post type item is saved
     42 * Add the a2zaal data to the specific structure when an active post type item is saved
    3243 *
    33  * @author: nvwd
    34  * @since: 2.0.0
    35  * @param $post_id
    36  * @return void
     44 * @author nvwd
     45 *
     46 * @since 2.0.0
     47 *
     48 * @param int $post_id ID of the post being saved.
    3749 */
    38 function maybe_add_a2zaal_post( $post_id ) {
    39 
     50function maybe_add_a2zaal_post( int $post_id ) {
    4051    if ( \wp_is_post_revision( $post_id ) || \wp_is_post_autosave( $post_id ) ) {
    4152        return;
    4253    }
    4354
    44     $a2zaal_post = \get_post( $post_id );
    45     $a2zaal_active_post_types = namespace\get_a2zaal_active_post_types();
     55    $a2zaal_post              = \get_post( $post_id );
     56    $a2zaal_active_post_types = get_a2zaal_active_post_types();
    4657
    47     if ( ! in_array( $a2zaal_post->post_type, $a2zaal_active_post_types ) ) {
     58    if ( ! in_array( $a2zaal_post->post_type, $a2zaal_active_post_types, true ) ) {
    4859        return;
    4960    }
    5061
    51     namespace\add_post_a2zaal_info( $a2zaal_post );
    52 
     62    add_post_a2zaal_info( $a2zaal_post );
    5363}
    5464
    5565/**
    56  * make the a2zaal list page titles more readable
     66 * Make the a2zaal list page titles more readable
    5767 *
    58  * @author: nvwd
    59  * @since: 2.0.0
    60  * @param $archive_title
     68 * @author nvwd
     69 *
     70 * @since 2.0.0
     71 *
     72 * @param string $archive_title archive page title.
     73 *
    6174 * @return string
    6275 */
    63 function filter_archive_title( $archive_title ) {
    64     if ( ! namespace\is_a2zaal_query() ) {
     76function filter_archive_title( string $archive_title ) {
     77    if ( ! is_a2zaal_query() ) {
    6578        return $archive_title;
    6679    }
    6780
    68     $post_type_labels = get_post_type_object( get_query_var( 'post_type' ) );
    69     $group_title_initial = get_query_var( A2ZAAL_REWRITE_TAG );
     81    $post_type_labels    = \get_post_type_object( \get_query_var( 'post_type' ) );
     82    $group_title_initial = \get_query_var( A2ZAAL_REWRITE_TAG );
    7083
    71     if ( '0' == $group_title_initial ) {
     84    if ( '0' === $group_title_initial ) {
    7285        $group_title_initial = '#';
    7386    }
    7487
    75     return sprintf( __( '%1$s: %2$s' ), $post_type_labels->label, $group_title_initial );
     88    /* translators: 1: post type title 2: initial of the archive grouping */
     89    return sprintf( __( '%1$s: %2$s', 'nvwd-a2zaal' ), $post_type_labels->label, $group_title_initial );
    7690}
    7791
    7892/**
    79  * customize the a2zaal list page description
     93 * Customize the a2zaal list page description
    8094 *
    81  * @author: nvwd
    82  * @since: 2.0.0
    83  * @param $archive_description
     95 * @author nvwd
     96 *
     97 * @since 2.0.0
     98 *
     99 * @param string $archive_description archive page description.
     100 *
    84101 * @return string
    85102 */
    86 function filter_archive_description( $archive_description ) {
    87     if ( ! namespace\is_a2zaal_query() ) {
     103function filter_archive_description( string $archive_description ) {
     104    if ( ! is_a2zaal_query() ) {
    88105        return $archive_description;
    89106    }
    90107
    91     $post_type_labels = get_post_type_object( get_query_var( 'post_type' ) );
     108    $post_type_labels = \get_post_type_object( \get_query_var( 'post_type' ) );
    92109
    93     $group_title_initial = get_query_var( A2ZAAL_REWRITE_TAG );
     110    $group_title_initial = \get_query_var( A2ZAAL_REWRITE_TAG );
    94111
    95     if ( '0' == $group_title_initial ) {
    96         $group_title_initial = __( 'a number', A2ZAAL_TEXT_DOMAIN );
     112    if ( '0' === $group_title_initial ) {
     113        $group_title_initial = 'a number';
    97114    }
    98115
    99     return sprintf( __( '%1$s beginning with %2$s.' ), $post_type_labels->label, $group_title_initial );
     116    /* translators: 1: post type title 2: initial of the archive grouping */
     117    return sprintf( __( '%1$s beginning with %2$s.', 'nvwd-a2zaal' ), $post_type_labels->label, $group_title_initial );
    100118}
  • a2z-alphabetical-archive-links/tags/2.1.0/src/rewrites.php

    r2015868 r3193532  
    11<?php
    2 /**
    3  * A2Z Alphabetical Archive Rewrites Handler
    4  *
    5  * @package     NVWD\A2ZAAL
    6  * @since       2.0.0
    7  * @author      nvwd
    8  * @link        http://nvwebdev.com/
    9  * @license     GPL-2.0+
    10  */
    112
    123namespace NVWD\A2ZAAL;
     4
     5use WP;
     6use WP_Query;
    137
    148\add_filter( 'rewrite_rules_array', __NAMESPACE__ . '\maybe_modify_rewrite_rules_array' );
    159
    1610/**
    17  * add rewrite rules for each a2z active post type
     11 * Add rewrite rules for each a2z active post type
    1812 *
    19  * @author: nvwd
    20  * @since: 2.0.0
    21  * @param   array   $rewrite_rules  the rewrite structure passed in
    22  * @return  array                   the possibly updated rewrite structure
     13 * @author nvwd
     14 *
     15 * @since 2.0.0
     16 *
     17 * @param array $rewrite_rules the rewrite structure passed in.
     18 *
     19 * @return array
    2320 */
    24 function maybe_modify_rewrite_rules_array( $rewrite_rules ) {
    25     $a2zaal_active_post_types = namespace\get_a2zaal_active_post_types();
     21function maybe_modify_rewrite_rules_array( array $rewrite_rules ) {
     22    $a2zaal_active_post_types = get_a2zaal_active_post_types();
    2623
    2724    if ( empty( $a2zaal_active_post_types ) ) {
     
    2926    }
    3027
    31     $new_rules = array();
     28    $new_rules = [];
    3229
    33     foreach ( $a2zaal_active_post_types AS $active_post_type ) {
    34         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/?$']                              = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=all';
    35         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/page/?([0-9]{1,})/?$']            = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=all&paged=$matches[2]';
    36         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/([a-zA-Z])/?$']                   = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=$matches[1]';
    37         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num/?$']                          = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=0';
    38         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/([a-zA-Z])/page/?([0-9]{1,})/?$'] = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=$matches[1]&paged=$matches[2]';
    39         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num/page/?([0-9]{1,})/?$']        = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=0&paged=$matches[1]';
     30    // phpcs:disable SlevomatCodingStandard.Files.LineLength.LineTooLong -- line length due to alignment for each rewrite rule.
     31    foreach ( $a2zaal_active_post_types as $active_post_type ) {
     32        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/?$' ]                              = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=all';
     33        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/page/?([0-9]{1,})/?$' ]            = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=all&paged=$matches[2]';
     34        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/([a-zA-Z])/?$' ]                   = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=$matches[1]';
     35        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num/?$' ]                          = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=0';
     36        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/([a-zA-Z])/page/?([0-9]{1,})/?$' ] = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=$matches[1]&paged=$matches[2]';
     37        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num/page/?([0-9]{1,})/?$' ]        = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=0&paged=$matches[1]';
    4038    }
     39
     40    // phpcs:enable SlevomatCodingStandard.Files.LineLength.LineTooLong
    4141
    4242    return array_merge( $new_rules, $rewrite_rules );
     
    4646
    4747/**
    48  * add the query tag being used for holding the specific initial
     48 * Add the query tag being used for holding the specific initial
    4949 *
    50  * @author: nvwd
    51  * @since: 2.0.0
    52  * @return void
     50 * @author nvwd
     51 *
     52 * @since 2.0.0
    5353 */
    5454function add_a2zaal_rewrite_tag() {
     
    5959
    6060/**
    61  * add hooks to modify the query if the a2z rewrite tag exists
     61 * Add hooks to modify the query if the a2z rewrite tag exists
    6262 *
    63  * @author: nvwd
    64  * @since: 2.0.0
    65  * @param   WP_Query    $query  main query
    66  * @return void
     63 * @author nvwd
     64 *
     65 * @since 2.0.0
     66 *
     67 * @param \WP $query WordPress environment instance.
    6768 */
    68 function maybe_alter_query_for_a2zaal( $query ) {
     69function maybe_alter_query_for_a2zaal( WP $query ) {
    6970    if ( ! isset( $query->query_vars[ A2ZAAL_REWRITE_TAG ] ) ) {
    7071        return;
     
    7980
    8081/**
    81  * pull all posts until I can build in pagination, then make pagination optional
     82 * Pull all posts until I can build in pagination, then make pagination optional
    8283 *
    83  * @author: nvwd
    84  * @since: 2.0.0
    85  * @param   WP_Query    $query  main query
    86  * @return void
     84 * @author nvwd
     85 *
     86 * @since 2.0.0
     87 *
     88 * @param \WP_Query $query main query.
    8789 */
    88 function kill_pagination( $query ) {
     90function kill_pagination( WP_Query $query ) {
    8991    $query->set( 'posts_per_page', -1 );
    9092    $query->set( 'no_found_rows', true );
     
    9294
    9395/**
    94  * add IN clause providing a list of post IDs to be retrieved by the query
     96 * Add IN clause providing a list of post IDs to be retrieved by the query
    9597 *
    96  * @author: nvwd
    97  * @since: 2.0.0
    98  * @param   string  $posts_where    query where clause
    99  * @return  string                  maybe modified where clause
     98 * @author nvwd
     99 *
     100 * @since 2.0.0
     101 *
     102 * @param string $posts_where query where clause.
     103 *
     104 * @return string
    100105 */
    101 function filter_a2zaal_where_clause( $posts_where ) {
     106function filter_a2zaal_where_clause( string $posts_where ) {
    102107    global $wpdb;
    103108
    104     $post_type_a2zaal_struct = \get_option( \get_query_var( 'post_type' ) . A2ZAAL_POSTS_SUFFIX, array() );
     109    $post_type_a2zaal_struct = \get_option( \get_query_var( 'post_type' ) . A2ZAAL_POSTS_SUFFIX, [] );
    105110
    106111    if ( empty( $post_type_a2zaal_struct ) ) {
     
    112117    $specific_post_ids = implode( ',', array_keys( $post_type_a2zaal_struct[ $a2zaal_query_var ] ) );
    113118
    114     $posts_where .= " AND ( $wpdb->posts.ID IN (" . $specific_post_ids . ") )";
     119    $posts_where .= " AND ( $wpdb->posts.ID IN (" . $specific_post_ids . ') )';
    115120
    116121    return $posts_where;
     
    118123
    119124/**
    120  * set the sort order using the naturally sorted object titles
     125 * Set the sort order using the naturally sorted object titles
    121126 *
    122  * @author: nvwd
    123  * @since: 2.0.0
    124  * @param   string  $posts_orderby  orderby clause
    125  * @return  string                  maybe new orderby clause
     127 * @author nvwd
     128 *
     129 * @since 2.0.0
     130 *
     131 * @param string $posts_orderby orderby clause.
     132 *
     133 * @return string
    126134 */
    127 function filter_a2zaal_orderby_clause( $posts_orderby ) {
     135function filter_a2zaal_orderby_clause( string $posts_orderby ) {
    128136    global $wpdb;
    129137
    130     $post_type_a2zaal_struct = \get_option( \get_query_var( 'post_type' ) . A2ZAAL_POSTS_SUFFIX, array() );
     138    $post_type_a2zaal_struct = \get_option( \get_query_var( 'post_type' ) . A2ZAAL_POSTS_SUFFIX, [] );
    131139
    132140    if ( empty( $post_type_a2zaal_struct ) ) {
     
    148156
    149157/**
    150  * check to see if posts/objects for the specified a2z initial exists in the post type structure
     158 * Check to see if posts/objects for the specified a2z initial exists in the post type structure
    151159 *
    152  * @author: nvwd
    153  * @since: 2.0.0
    154  * @param $query
    155  * @return void
     160 * @author nvwd
     161 *
     162 * @since 2.0.0
     163 *
     164 * @param \WP_Query $query query to check to see if it's a 404.
    156165 */
    157 function check_a2zaal_404( &$query ) {
    158 
    159     if ( ! namespace\is_a2zaal_query() || \is_admin() ) {
     166function check_a2zaal_404( WP_Query &$query ) {
     167    if ( ! is_a2zaal_query() || \is_admin() ) {
    160168        return;
    161169    }
    162170
    163     $post_type_a2zaal_struct = \get_option( $query->query_vars['post_type'] . A2ZAAL_POSTS_SUFFIX, array() );
     171    $post_type_a2zaal_struct = \get_option( $query->query_vars['post_type'] . A2ZAAL_POSTS_SUFFIX, [] );
    164172
    165173    if ( empty( $post_type_a2zaal_struct ) ) {
     
    167175    }
    168176
    169     if ( array_key_exists( $query->query_vars[ A2ZAAL_REWRITE_TAG ], $post_type_a2zaal_struct ) || 'all' == $query->query_vars[ A2ZAAL_REWRITE_TAG ] ) {
     177    if (
     178        array_key_exists( $query->query_vars[ A2ZAAL_REWRITE_TAG ], $post_type_a2zaal_struct )
     179        || 'all' === $query->query_vars[ A2ZAAL_REWRITE_TAG ]
     180    ) {
    170181        return;
    171182    }
     
    173184    $query->set_404();
    174185}
    175 
  • a2z-alphabetical-archive-links/tags/2.1.0/src/settings.php

    r2015868 r3193532  
    11<?php
    2 /**
    3  * A2Z Alphabetical Archive Links Settings Page Handler
    4  *
    5  * @package     NVWD\A2ZAAL
    6  * @since       2.0.0
    7  * @author      nvwd
    8  * @link        http://nvwebdev.com/
    9  * @license     GPL-2.0+
    10  */
    112
    123namespace NVWD\A2ZAAL;
    134
     5use WP_Screen;
     6
    147\add_filter( 'plugin_action_links_' . A2ZAAL_BASENAME, __NAMESPACE__ . '\add_settings_page_link' );
    158
    16 function add_settings_page_link( $links ) {
    17     $settings_link = '<a href="' . \admin_url( 'options-general.php?page=a2zaal-options' ) . '">' . __( 'Settings', A2ZAAL_TEXT_DOMAIN ) . '</a>';
     9/**
     10 * Adds custom settings page to the admin settings menu
     11 *
     12 * @author nvwd
     13 *
     14 * @since 2.0.0
     15 *
     16 * @param array $links admin menu links.
     17 *
     18 * @return array
     19 */
     20function add_settings_page_link( array $links ) {
     21    $settings_link = sprintf(
     22        '<a href="%s">%s</a>',
     23        \admin_url( 'options-general.php?page=a2zaal-options' ),
     24        \__( 'Settings', 'nvwd-a2zaal' )
     25    );
    1826    array_unshift( $links, $settings_link );
     27
    1928    return $links;
    2029}
     
    2332
    2433/**
    25  * add admin settings page for our use
    26  *
    27  * @author: nvwd
    28  * @since: 2.0.0
    29  * @return void
     34 * Add admin settings page for our use
     35 *
     36 * @author nvwd
     37 *
     38 * @since 2.0.0
    3039 */
    3140function add_settings_menu_link() {
    32     \add_options_page( 'A2Z Alphabetical Archive Links Options', 'A2Z Alphabetical Archive Links', 'manage_options', 'a2zaal-options', __NAMESPACE__ . '\create_settings_page' );
    33     //enqueue javascript and styles needed for admin options page
    34 }
    35 
    36 /**
    37  * admin page creation script
    38  *
    39  * @author: nvwd
    40  * @since: 2.0.0
    41  * @return void
     41    \add_options_page(
     42        'A2Z Alphabetical Archive Links Options',
     43        'A2Z Alphabetical Archive Links',
     44        'manage_options',
     45        'a2zaal-options',
     46        __NAMESPACE__ . '\create_settings_page'
     47    );
     48    // enqueue javascript and styles needed for admin options page.
     49}
     50
     51/**
     52 * Admin page creation script
     53 *
     54 * @author nvwd
     55 *
     56 * @since 2.0.0
    4257 */
    4358function create_settings_page() {
    44 
    4559    if ( ! \current_user_can( 'manage_options' ) ) {
    4660        die( 'Unauthorized User' );
    4761    }
    4862
    49     $a2zaal_active_post_types = namespace\get_a2zaal_active_post_types();
    50 
    51     $a2zaal_processing_post_types = namespace\get_a2zaal_processing_post_types();
    52 
    53     $excluded_post_types = array(
    54         'attachment',
    55         'revision',
    56         'nav_menu_item'
    57     );
    58 
    59     $registered_post_types = \get_post_types( array( 'publicly_queryable' => true ), 'objects' );
    60 
    61     $a2zaal_processing_counts = \get_option( 'a2zaal_processing_counts', array() );
    62 
    63     include( A2ZAAL_VIEW_DIR . 'settings_form.php' );
     63    require A2ZAAL_VIEW_DIR . 'settings-form.php';
     64}
     65
     66\add_action( 'admin_init', __NAMESPACE__ . '\register_plugin_settings' );
     67\add_action( 'rest_admin_init', __NAMESPACE__ . '\register_plugin_settings' );
     68
     69/**
     70 * Register block widget
     71 *
     72 * @author nvwd
     73 *
     74 * @since 2.1.0
     75 */
     76function register_plugin_settings() {
     77    $args = [
     78        'default'      => [],
     79        'type'         => 'array',
     80        'show_in_rest' => [
     81            'schema' => [
     82                'type'  => 'array',
     83                'items' => [
     84                    'type' => 'string',
     85                ],
     86            ],
     87        ],
     88    ];
     89
     90    \register_setting( 'options', 'a2zaal_post_types', $args );
     91}
     92
     93add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\output_a2z_active_post_types' );
     94
     95/**
     96 * Outputs the a2z active post types for use in the block editor
     97 *
     98 * @author nvwd
     99 *
     100 * @since 2.1.0
     101 */
     102function output_a2z_active_post_types() {
     103    $a2z_active_post_types = get_a2zaal_active_post_types();
     104
     105    \wp_add_inline_script(
     106        'nvwda2zaal-a2z-links-editor-script',
     107        'const NVWDA2ZAAL_activePostTypes = ' . \wp_json_encode( $a2z_active_post_types ) . ';',
     108        'before'
     109    );
    64110}
    65111
     
    67113
    68114/**
    69  * handle the submission of our settings page
    70  *
    71  * @author: nvwd
    72  * @since: 2.0.0
    73  * @param $screen_obj
    74  * @return void
    75  */
    76 function maybe_process_a2zaal_settings_save( $screen_obj ) {
    77 
     115 * Handle the submission of our settings page
     116 *
     117 * @author nvwd
     118 *
     119 * @since 2.0.0
     120 *
     121 * @param \WP_Screen $screen_obj current screen.
     122 */
     123function maybe_process_a2zaal_settings_save( WP_Screen $screen_obj ) {
    78124    if ( 'settings_page_a2zaal-options' !== $screen_obj->id ) {
    79125        return;
    80126    }
    81127
    82     \wp_enqueue_script( 'a2zaal_settings_admin', A2ZAAL_ROOT_URL . 'js/a2zaal.js', array( 'jquery' ), false, true );
    83 
    84     if ( ! empty( $_POST['sbmt_a2zaal_settings'] ) ) {
    85         namespace\process_options_submission( namespace\get_a2zaal_active_post_types() );
    86     }
    87 }
    88 
    89 /**
    90  * control the a2zaal activation and deactivation of specified post types
    91  *
    92  * @author: nvwd
    93  * @since: 2.0.0
    94  * @param $a2zaal_original_cpts
    95  * @return void
    96  */
    97 function process_options_submission( $a2zaal_original_cpts ) {
    98 
     128    \wp_enqueue_script( 'a2zaal_settings_admin', A2ZAAL_ROOT_URL . 'js/a2zaal.js', [ 'jquery' ], A2ZAAL_VERSION, true );
     129
     130    // phpcs:ignore WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable -- the nonce is checked in process_options_submission() if this _POST var is not empty.
     131    if ( empty( $_POST['sbmt_a2zaal_settings'] ) ) {
     132        return;
     133    }
     134
     135    process_options_submission( get_a2zaal_active_post_types() );
     136}
     137
     138/**
     139 * Control the a2zaal activation and deactivation of specified post types
     140 *
     141 * @author nvwd
     142 *
     143 * @since 2.0.0
     144 *
     145 * @param array $a2zaal_original_cpts a2zaal active post types.
     146 */
     147function process_options_submission( array $a2zaal_original_cpts ) {
    99148    \check_admin_referer( 'a2zaal-options' );
    100149
    101     $enable_cpts = array();
    102     $disable_cpts = array();
    103 
    104     if ( isset( $_POST['a2zaal_enabled_post_type'] ) ) {
    105         $submitted_cpts = $_POST['a2zaal_enabled_post_type'];
    106         $enable_cpts = array_diff( $_POST['a2zaal_enabled_post_type'], $a2zaal_original_cpts );
    107         $disable_cpts = array_diff( $a2zaal_original_cpts, $_POST['a2zaal_enabled_post_type'] );
     150    // phpcs:disable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable -- checking if the _POST var exists before sanitizing it.
     151    $submitted_cpts = isset( $_POST['a2zaal_enabled_post_type'] )
     152        ? array_map( 'sanitize_text_field', \wp_unslash( $_POST['a2zaal_enabled_post_type'] ) )
     153        : false;
     154    // phpcs:enable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
     155
     156    $enable_cpts  = [];
     157    $disable_cpts = [];
     158
     159    if ( $submitted_cpts ) {
     160        $enable_cpts  = array_diff( $submitted_cpts, $a2zaal_original_cpts );
     161        $disable_cpts = array_diff( $a2zaal_original_cpts, $submitted_cpts );
    108162    } else {
    109         $submitted_cpts = array();
    110         $disable_cpts = $a2zaal_original_cpts;
    111     }
    112 
    113     namespace\maybe_remove_disabled_cpt_a2zaal_posts( $disable_cpts );
    114 
    115     namespace\maybe_setup_background_post_type_process( $enable_cpts );
     163        $submitted_cpts = [];
     164        $disable_cpts   = $a2zaal_original_cpts;
     165    }
     166
     167    maybe_remove_disabled_cpt_a2zaal_posts( $disable_cpts );
     168
     169    maybe_setup_background_post_type_process( $enable_cpts );
    116170
    117171    if ( ! empty( $disable_cpts ) || ! empty( $enable_cpts ) ) {
     
    120174
    121175    \update_option( 'a2zaal_post_types', $submitted_cpts, true );
    122 
    123 }
    124 
    125 /**
    126  * disable specified post types a2zaal and create the trigger to delete the associated terms
    127  *
    128  * @author: nvwd
    129  * @since: 2.0.0
    130  * @param $disable_cpts
    131  * @return void
    132  */
    133 function maybe_remove_disabled_cpt_a2zaal_posts( $disable_cpts ) {
     176}
     177
     178/**
     179 * Disable specified post types a2zaal and create the trigger to delete the associated terms
     180 *
     181 * @author nvwd
     182 *
     183 * @since 2.0.0
     184 *
     185 * @param array $disable_cpts cpts being disabled.
     186 */
     187function maybe_remove_disabled_cpt_a2zaal_posts( array $disable_cpts ) {
    134188    if ( empty( $disable_cpts ) ) {
    135189        return;
    136190    }
    137191
    138     foreach ( $disable_cpts AS $disabled_post_type ) {
    139         namespace\remove_disabled_cpts( $disabled_post_type );
    140         namespace\maybe_remove_current_processing( $disabled_post_type );
     192    foreach ( $disable_cpts as $disabled_post_type ) {
     193        remove_disabled_cpts( $disabled_post_type );
     194        maybe_remove_current_processing( $disabled_post_type );
    141195    }
    142196
     
    145199
    146200/**
    147  * remove a2zaal data for disabled post types
    148  *
    149  * @author: nvwd
    150  * @since: 2.0.0
    151  * @param $disabled_post_type
    152  * @return void
    153  */
    154 function remove_disabled_cpts( $disabled_post_type ) {
     201 * Remove a2zaal data for disabled post types
     202 *
     203 * @author nvwd
     204 *
     205 * @since 2.0.0
     206 *
     207 * @param string $disabled_post_type post type to delete its option data.
     208 */
     209function remove_disabled_cpts( string $disabled_post_type ) {
    155210    $a2zaal_option = $disabled_post_type . A2ZAAL_POSTS_SUFFIX;
    156211
    157212    \delete_option( $a2zaal_option );
    158 
    159 }
    160 
    161 function maybe_remove_current_processing( $disabled_post_type ) {
    162     $a2zaal_background_processing = \get_option( 'a2zaal_background_processing', array() );
    163 
    164     if ( ! isset( $a2zaal_background_processing['post-type'][$disabled_post_type] ) ) {
    165         return;
    166     }
    167 
    168     unset( $a2zaal_background_processing['post-type'][$disabled_post_type] );
     213}
     214
     215/**
     216 * Disable background processing for disabled post type.
     217 *
     218 * @author nvwd
     219 *
     220 * @since 2.0.0
     221 *
     222 * @param string $disabled_post_type disabled post type to check for background processing.
     223 */
     224function maybe_remove_current_processing( string $disabled_post_type ) {
     225    $a2zaal_background_processing = \get_option( 'a2zaal_background_processing', [] );
     226
     227    if ( ! isset( $a2zaal_background_processing['post-type'][ $disabled_post_type ] ) ) {
     228        return;
     229    }
     230
     231    unset( $a2zaal_background_processing['post-type'][ $disabled_post_type ] );
    169232    \update_option( 'a2zaal_background_processing', $a2zaal_background_processing, true );
    170233
    171     if ( empty( $a2zaal_background_processing['post-type'] ) ) {
    172         namespace\clear_scheduled_processing( 'a2zaal_process_activation_cron' );
    173     }
    174 
    175     return;
    176 }
    177 
    178 /**
    179  * create the admin notice that the disable request is a success
    180  *
    181  * @author: nvwd
    182  * @since: 2.0.0
    183  * @return void
     234    if ( ! empty( $a2zaal_background_processing['post-type'] ) ) {
     235        return;
     236    }
     237
     238    clear_scheduled_processing( 'a2zaal_process_activation_cron' );
     239}
     240
     241/**
     242 * Create the admin notice that the disable request is a success
     243 *
     244 * @author nvwd
     245 *
     246 * @since 2.0.0
    184247 */
    185248function disabled_a2zaal_cpts_admin_notice() {
    186     $class = 'notice notice-success is-dismissible';
    187     $message = __( 'The specified Post Type(s) are now disabled for A2Z Alphabetical Archive Links.', A2ZAAL_TEXT_DOMAIN );
     249    $class   = 'notice notice-success is-dismissible';
     250    $message = __( 'The specified Post Type(s) are now disabled for A2Z Alphabetical Archive Links.', 'nvwd-a2zaal' );
    188251
    189252    a2zaal_output_admin_notice( $class, $message );
     
    191254
    192255/**
    193  * register a2zaal activated post types and create structure to hold the initials and sort titles for
     256 * Register a2zaal activated post types and create structure to hold the initials and sort titles for
    194257 * existing post type items
    195258 *
    196  * @author: nvwd
    197  * @since: 2.0.0
    198  * @param $enable_cpts
    199  * @return void
    200  */
    201 function maybe_setup_background_post_type_process( $enable_post_type ) {
     259 * @author nvwd
     260 *
     261 * @since 2.0.0
     262 *
     263 * @param array $enable_post_type cpts to enable.
     264 */
     265function maybe_setup_background_post_type_process( array $enable_post_type ) {
    202266    if ( empty( $enable_post_type ) ) {
    203267        return;
     
    210274
    211275/**
    212  * create admin notice that the activate request is a success
    213  *
    214  * @author: nvwd
    215  * @since: 2.0.0
    216  * @return void
     276 * Create admin notice that the activate request is a success
     277 *
     278 * @author nvwd
     279 *
     280 * @since 2.0.0
    217281 */
    218282function enabled_a2zaal_cpts_admin_notice() {
    219     $class = 'notice notice-success is-dismissible';
    220     $message = __( 'The specified Post Type(s) are being processed in the background for A2Z Alphabetical Archive Links. Leaving this page does not stop the process.', A2ZAAL_TEXT_DOMAIN );
     283    $class   = 'notice notice-success is-dismissible';
     284    $message = __(
     285        'The specified Post Type(s) are being processed in the background for A2Z Alphabetical Archive Links.
     286        Leaving this page does not stop the process.',
     287        'nvwd-a2zaal'
     288    );
    221289
    222290    a2zaal_output_admin_notice( $class, $message );
  • a2z-alphabetical-archive-links/trunk/a2z-alphabetical-archive-links.php

    r2019541 r3193532  
    11<?php
    2     /*
    3         Plugin Name: A2Z Alphabetical Archive Links
    4         Plugin URI: https://github.com/NowellVanHoesen/a2z-alphabetical-archive-links/wiki
    5         Description: Get a list of characters, A to Z, from the initial character of a post or CPT title. The Initials will link to an archive page of posts/CPTs that begin with that character.
    6         Version: 2.0.2
    7         Author: Nowell VanHoesen
    8         Author URI: http://nvwebdev.com/
    9         Author Email: nowell@nvwebdev.com
    10         License: GPL-2.0+
    11         Licanse URI: https://www.gnu.org/licenses/gpl.html
    12         Text Domain: nvwd-a2zaal
    13     */
    14 
    15     if ( ! defined( 'WPINC' ) ) {
    16         die;
    17     }
    18 
    19     define( 'A2ZAAL_PLUGIN', __FILE__ );
    20     define( 'A2ZAAL_BASENAME', plugin_basename( A2ZAAL_PLUGIN ) );
    21     define( 'A2ZAAL_VERSION', '2.0.2' );
    22     define( 'A2ZAAL_PLUGIN_ROOT_DIR', trailingslashit( __DIR__ ) );
    23 
    24     $a2zaal_url = plugin_dir_url( A2ZAAL_PLUGIN );
    25     if ( is_ssl() ) {
    26         $a2zaal_url = str_replace( 'http://', 'https://', $a2zaal_url );
    27     }
    28 
    29     define( 'A2ZAAL_ROOT_URL', $a2zaal_url );
    30     define( 'A2ZAAL_TEXT_DOMAIN', 'nvwd-a2zaal' );
    31 
    32     define( 'A2ZAAL_PHP_MIN_VERIONS', '5.6' );
    33     define( 'A2ZAAL_WP_MIN_VERSIONS', '4.6.0' );
    34 
    35     register_activation_hook( A2ZAAL_PLUGIN, 'a2zaal_activation_check' );
    36     register_deactivation_hook( A2ZAAL_PLUGIN, 'a2zaal_deactivate' );
    37     register_uninstall_hook( A2ZAAL_PLUGIN, 'a2zaal_uninstall' );
    38 
    39     add_action( 'admin_init', 'a2zaal_verify_versions' );
    40 
    41     /* do version checks before including the rest of the plugin code */
    42     if ( is_wp_error( a2zaal_version_checks() ) ) {
     2/*
     3    Plugin Name: A2Z Alphabetical Archive Links
     4    Plugin URI: https://github.com/NowellVanHoesen/a2z-alphabetical-archive-links/wiki
     5    Description: Get a list of characters, A to Z, from the initial character of a post or CPT title.
     6                The Initials will link to an archive page of posts/CPTs that begin with that character.
     7    Version: 2.1.0
     8    Requires at least: 5.8
     9    Requires PHP: 7.4
     10    Author: Nowell VanHoesen
     11    Author URI: http://nvwebdev.com/
     12    Author Email: nowell@nvwebdev.com
     13    License: GPL-2.0+
     14    License URI: https://www.gnu.org/licenses/gpl.html
     15    Text Domain: nvwd-a2zaal
     16*/
     17
     18namespace NVWD\A2ZAAL;
     19
     20if ( ! defined( 'WPINC' ) ) {
     21    die;
     22}
     23
     24define( 'A2ZAAL_PLUGIN', __FILE__ );
     25define( 'A2ZAAL_BASENAME', plugin_basename( A2ZAAL_PLUGIN ) );
     26define( 'A2ZAAL_VERSION', '2.1.0' );
     27define( 'A2ZAAL_PLUGIN_ROOT_DIR', trailingslashit( __DIR__ ) );
     28
     29$a2zaal_url = plugin_dir_url( A2ZAAL_PLUGIN );
     30
     31if ( is_ssl() ) {
     32    $a2zaal_url = str_replace( 'http://', 'https://', $a2zaal_url );
     33}
     34
     35define( 'A2ZAAL_ROOT_URL', $a2zaal_url );
     36
     37define( 'A2ZAAL_PHP_MIN_VERSION', '7.4' );
     38define( 'A2ZAAL_WP_MIN_VERSION', '5.8' );
     39
     40register_activation_hook( A2ZAAL_PLUGIN, 'a2zaal_activation_check' );
     41register_deactivation_hook( A2ZAAL_PLUGIN, 'a2zaal_deactivate' );
     42register_uninstall_hook( A2ZAAL_PLUGIN, 'a2zaal_uninstall' );
     43
     44add_filter( 'a2zaal_do_version_checks', __NAMESPACE__ . '\a2zaal_check_php_version' );
     45add_filter( 'a2zaal_do_version_checks', __NAMESPACE__ . '\a2zaal_check_wp_version' );
     46add_action( 'admin_init', __NAMESPACE__ . '\a2zaal_verify_versions' );
     47
     48/* do version checks before including the rest of the plugin code */
     49if ( is_wp_error( a2zaal_version_checks() ) ) {
     50    return;
     51}
     52
     53/* version checks complete let's get the party started */
     54require A2ZAAL_PLUGIN_ROOT_DIR . 'src/plugin.php';
     55
     56/**
     57 * Plugin activation script
     58 * initiate version checks, disable plugin if any fail
     59 *
     60 * @author nvwd
     61 *
     62 * @since 2.0.0
     63 */
     64function a2zaal_activation_check() {
     65    $a2zaal_activation_check = a2zaal_version_checks();
     66
     67    if ( ! is_wp_error( $a2zaal_activation_check ) ) {
    4368        return;
    4469    }
    4570
    46     /* version checks complete let's get the party started */
    47     include( A2ZAAL_PLUGIN_ROOT_DIR . 'src/plugin.php' );
    48 
     71    deactivate_plugins( A2ZAAL_BASENAME );
     72}
     73
     74/**
     75 * Plugin deactivate script
     76 *
     77 * @author nvwd
     78 *
     79 * @since 2.0.0
     80 */
     81function a2zaal_deactivate() {
    4982    /**
    50      * plugin activation script
    51      * initiate version checks, disable plugin if any fail
    52      *
    53      * @author: nvwd
    54      *
    55      * @since: 2.0.0
    56      *
    57      * @return void
     83     * Remove background processing
     84     * remove any options
     85     * remove any activated post type data
    5886     */
    59     function a2zaal_activation_check() {
    60         $a2zaal_activation_check = a2zaal_version_checks();
    61 
    62         if ( is_wp_error( $a2zaal_activation_check ) ) {
    63             deactivate_plugins( A2ZAAL_BASENAME );
    64             return;
     87    do_action( 'a2zaal_deactivation' );
     88    a2zaal_clear_rewrite_rules();
     89}
     90
     91/**
     92 * Plugin uninstall script
     93 *
     94 * @author nvwd
     95 *
     96 * @since 2.0.0
     97 */
     98function a2zaal_uninstall() {
     99
     100    $a2zaal_active_post_types = get_a2zaal_active_post_types();
     101
     102    foreach ( $a2zaal_active_post_types as $active_cpt ) {
     103        remove_disabled_cpts( $active_cpt );
     104    }
     105
     106    delete_option( 'a2zaal_post_types' );
     107}
     108
     109/**
     110 * Delete rewrite rules option wrapper
     111 *
     112 * @author nvwd
     113 *
     114 * @since 2.0.0
     115 */
     116function a2zaal_clear_rewrite_rules() {
     117    delete_option( 'rewrite_rules' );
     118}
     119
     120/**
     121 * Main version check function
     122 * disable plugin if version check(s) fail
     123 *
     124 * @author nvwd
     125 *
     126 * @since 2.0.0
     127 */
     128function a2zaal_verify_versions() {
     129    $a2zaal_activation_check = a2zaal_version_checks();
     130
     131    if ( ! is_wp_error( $a2zaal_activation_check ) || ! is_plugin_active( A2ZAAL_BASENAME ) ) {
     132        return;
     133    }
     134
     135    deactivate_plugins( A2ZAAL_BASENAME );
     136
     137    // phpcs:disable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable, WordPress.Security.NonceVerification.Recommended -- plugin activation failed, need to remove the param so nothing else is processed.
     138    if ( isset( $_GET['activate'] ) ) {
     139        unset( $_GET['activate'] );
     140    }
     141    // phpcs:enable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
     142}
     143
     144/**
     145 * Version check controller
     146 *
     147 * @author nvwd
     148 *
     149 * @since 2.0.0
     150 *
     151 * @return bool|\WP_Error
     152 */
     153function a2zaal_version_checks() {
     154    // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName -- Slevomat can't make up its mind with or without the '\', so I'm opting for with.
     155    $version_check_errors = new \WP_Error();
     156
     157    $version_check_errors = apply_filters( 'a2zaal_do_version_checks', $version_check_errors );
     158
     159    $error_codes = $version_check_errors->get_error_codes();
     160
     161    if ( ! empty( $error_codes ) ) {
     162        return $version_check_errors;
     163    }
     164
     165    return true;
     166}
     167
     168/**
     169 * Check WP version
     170 *
     171 * @author nvwd
     172 *
     173 * @since 2.0.0
     174 *
     175 * @param \WP_Error $version_check_errors version check errors.
     176 *
     177 * @return \WP_Error
     178 *
     179 * phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName -- Slevomat can't make up its mind with or without the '\', so I'm opting for with.
     180 */
     181function a2zaal_check_wp_version( \WP_Error $version_check_errors ) {
     182    global $wp_version;
     183
     184    if ( version_compare( $wp_version, A2ZAAL_WP_MIN_VERSION, '>=' ) ) {
     185        return $version_check_errors;
     186    }
     187
     188    add_action( 'admin_notices', 'a2zaal_wp_version_failure_message' );
     189
     190    $version_check_errors->add(
     191        'wp_version',
     192        esc_html__(
     193            'WordPress version check for A2Z Alphabetical Archive Links failed.
     194            A2Z Alphabetical Archive Links should not be active.',
     195            'nvwd-a2zaal'
     196        )
     197    );
     198
     199    return $version_check_errors;
     200}
     201// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
     202
     203/**
     204 * Check PHP version
     205 *
     206 * @author nvwd
     207 *
     208 * @since 2.0.0
     209 *
     210 * @param \WP_Error $version_check_errors version check errors.
     211 *
     212 * @return \WP_Error
     213 *
     214 * phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName -- Slevomat can't make up its mind with or without the '\', so I'm opting for with.
     215 */
     216function a2zaal_check_php_version( \WP_Error $version_check_errors ) {
     217    if ( version_compare( PHP_VERSION, A2ZAAL_PHP_MIN_VERSION, '>=' ) ) {
     218        return $version_check_errors;
     219    }
     220
     221    add_action( 'admin_notices', 'a2zaal_php_version_failure_message' );
     222
     223    $version_check_errors->add(
     224        'php_version',
     225        esc_html__(
     226            'PHP version check for A2Z Alphabetical Archive Links failed.
     227            A2Z Alphabetical Archive Links should not be active.',
     228            'nvwd-a2zaal'
     229        )
     230    );
     231
     232    return $version_check_errors;
     233}
     234// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
     235
     236/**
     237 * Create admin notice of WP version check failure
     238 *
     239 * @author nvwd
     240 *
     241 * @since 2.0.0
     242 */
     243function a2zaal_wp_version_failure_message() {
     244    $css_class = 'notice notice-error';
     245    $message   = sprintf(
     246        /* translators: 1: Minimum WordPress version */
     247        __(
     248            'A2Z Alphabetical Archive Links requires WordPress %1$s to function properly. Please upgrade WordPress.
     249            A2Z Alphabetical Archive Links has been auto-deactivated.',
     250            'nvwd-a2zaal'
     251        ),
     252        A2ZAAL_WP_MIN_VERSION
     253    );
     254
     255    a2zaal_output_admin_notice( $css_class, $message );
     256}
     257
     258/**
     259 * Create admin notice of PHP version check failure
     260 *
     261 * @author nvwd
     262 *
     263 * @since 2.0.0
     264 */
     265function a2zaal_php_version_failure_message() {
     266    $css_class = 'notice notice-error';
     267    $message   = sprintf(
     268        /* translators: 1: Minimum php version */
     269        __(
     270            'A2Z Alphabetical Archive Links requires PHP %1$s to function properly. Please upgrade PHP.
     271            A2Z Alphabetical Archive Links has been auto-deactivated.',
     272            'nvwd-a2zaal'
     273        ),
     274        A2ZAAL_PHP_MIN_VERSION
     275    );
     276
     277    a2zaal_output_admin_notice( $css_class, $message );
     278}
     279
     280/**
     281 * Display admin notice
     282 *
     283 * @author nvwd
     284 *
     285 * @since 2.0.0
     286 *
     287 * @param string $css_class css class for the message output.
     288 * @param string $message message to display.
     289 */
     290function a2zaal_output_admin_notice( string $css_class, string $message ) {
     291    printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $css_class ), esc_html( $message ) );
     292}
     293
     294if ( function_exists( 'register_block_type' ) ) {
     295    add_action(
     296        'init',
     297        static function () {
     298            register_block_type( __DIR__ . '/build/blocks/a2z-links' );
    65299        }
    66 
    67         return;
    68     }
    69 
    70     /**
    71      * plugin deactivate script
    72      *
    73      * @author: nvwd
    74      *
    75      * @since: 2.0.0
    76      *
    77      * @return void
    78      */
    79     function a2zaal_deactivate() {
    80         /**
    81          * remove background processing
    82          * remove any options
    83          * remove any activated post type data
    84          */
    85         do_action( 'a2zaal_deactivation' );
    86         a2zaal_clear_rewrite_rules();
    87     }
    88 
    89     /**
    90      * plugin uninstall script
    91      *
    92      * @author: nvwd
    93      *
    94      * @since: 2.0.0
    95      *
    96      * @return void
    97      */
    98     function a2zaal_uninstall() {
    99 
    100         $a2zaal_active_post_types = NVWD\A2ZAAL\get_a2zaal_active_post_types();
    101 
    102         foreach ( $a2zaal_active_post_types AS $active_cpt ) {
    103             NVWD\A2ZAAL\remove_disabled_taxonomy_terms( $active_cpt );
    104         }
    105 
    106         delete_option( 'a2zaal_post_types' );
    107 
    108     }
    109 
    110     /**
    111      * delete rewrite rules option wrapper
    112      *
    113      * @author: nvwd
    114      *
    115      * @since: 2.0.0
    116      *
    117      * @return void
    118      */
    119     function a2zaal_clear_rewrite_rules() {
    120         delete_option( 'rewrite_rules' );
    121     }
    122 
    123     /**
    124      * main version check function
    125      * disable plugin if version check(s) fail
    126      *
    127      * @author: nvwd
    128      *
    129      * @since: 2.0.0
    130      *
    131      * @return void
    132      */
    133     function a2zaal_verify_versions() {
    134         $a2zaal_activation_check = a2zaal_version_checks();
    135 
    136         if ( ! is_wp_error( $a2zaal_activation_check ) ) {
    137             return;
    138         }
    139 
    140         if ( ! is_plugin_active( A2ZAAL_BASENAME ) ) {
    141             return;
    142         }
    143 
    144         deactivate_plugins( A2ZAAL_BASENAME );
    145 
    146         if ( isset( $_GET['activate'] ) ) {
    147             unset( $_GET['activate'] );
    148         }
    149     }
    150 
    151     /**
    152      * version check controller
    153      *
    154      * @author: nvwd
    155      *
    156      * @since: 2.0.0
    157      *
    158      * @return bool|WP_Error
    159      */
    160     function a2zaal_version_checks() {
    161         $version_check_errors = new WP_Error();
    162 
    163         $version_check_errors = apply_filters( 'a2zaal_do_version_checks', $version_check_errors );
    164 
    165         $error_codes = $version_check_errors->get_error_codes();
    166         if ( ! empty( $error_codes ) ) {
    167             return $version_check_errors;
    168         }
    169 
    170         return true;
    171     }
    172 
    173     add_filter( 'a2zaal_do_version_checks', 'a2zaal_check_wp_version' );
    174     /**
    175      * Check WP version
    176      *
    177      * @author: nvwd
    178      *
    179      * @since: 2.0.0
    180      *
    181      * @param $version_check_errors
    182      *
    183      * @return WP_Error
    184      */
    185     function a2zaal_check_wp_version( $version_check_errors ) {
    186         global $wp_version;
    187 
    188         if ( version_compare( $wp_version, A2ZAAL_WP_MIN_VERSIONS, '>=' ) ) {
    189             return $version_check_errors;
    190         }
    191 
    192         add_action( 'admin_notices', 'a2zaal_wp_version_failure_message' );
    193 
    194         $version_check_errors->add(
    195             'wp_version',
    196             esc_html__('WordPress version check for A2Z Alphabetical Archive Links failed. A2Z Alphabetical Archive Links should not be active.', A2ZAAL_TEXT_DOMAIN )
    197         );
    198 
    199         return $version_check_errors;
    200     }
    201 
    202     add_filter( 'a2zaal_do_version_checks', 'a2zaal_check_php_version' );
    203     /**
    204      * Check PHP version
    205      *
    206      * @author: nvwd
    207      *
    208      * @since: 2.0.0
    209      *
    210      * @param $version_check_errors
    211      *
    212      * @return WP_Error
    213      */
    214     function a2zaal_check_php_version( $version_check_errors ) {
    215         if ( version_compare( PHP_VERSION, A2ZAAL_PHP_MIN_VERIONS, '>=' ) ) {
    216             return $version_check_errors;
    217         }
    218 
    219         add_action( 'admin_notices', 'a2zaal_php_version_failure_message' );
    220 
    221         $version_check_errors->add(
    222             'php_version',
    223             esc_html__('PHP version check for A2Z Alphabetical Archive Links failed. A2Z Alphabetical Archive Links should not be active.', A2ZAAL_TEXT_DOMAIN )
    224         );
    225 
    226         return $version_check_errors;
    227     }
    228 
    229     /**
    230      * Create admin notice of WP version check failure
    231      *
    232      * @author: nvwd
    233      *
    234      * @since: 2.0.0
    235      *
    236      * @return void
    237      */
    238     function a2zaal_wp_version_failure_message() {
    239         $class = 'notice notice-error';
    240         $message = __( 'A2Z Alphabetical Archive Links requires WordPress ' . A2ZAAL_WP_MIN_VERSIONS . ' to function properly. Please upgrade WordPress. A2Z Alphabetical Archive Links has been auto-deactivated.', A2ZAAL_TEXT_DOMAIN );
    241 
    242         a2zaal_output_admin_notice( $class, $message );
    243     }
    244 
    245     /**
    246      * Create admin notice of PHP version check failure
    247      *
    248      * @author: nvwd
    249      *
    250      * @since: 2.0.0
    251      *
    252      * @return void
    253      */
    254     function a2zaal_php_version_failure_message() {
    255         $class = 'notice notice-error';
    256         $message = __( 'A2Z Alphabetical Archive Links requires PHP ' . A2ZAAL_PHP_MIN_VERIONS . ' to function properly. Please upgrade PHP. A2Z Alphabetical Archive Links has been auto-deactivated.', A2ZAAL_TEXT_DOMAIN );
    257 
    258         a2zaal_output_admin_notice( $class, $message );
    259     }
    260 
    261     /**
    262      * Display admin notice
    263      *
    264      * @author: nvwd
    265      *
    266      * @since: 2.0.0
    267      *
    268      * @param $class
    269      * @param $message
    270      *
    271      * @return void
    272      */
    273     function a2zaal_output_admin_notice( $class, $message ) {
    274         printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
    275     }
     300    );
     301}
  • a2z-alphabetical-archive-links/trunk/css/display.css

    r2015868 r3193532  
    11/* CSS Document */
     2ul.wp-block-nvwda2zaal-a2z-links,
    23.a2zaal_widget ul {
    34    display: grid;
     
    78    margin: 0;
    89}
     10ul.wp-block-nvwda2zaal-a2z-links.counts,
    911.a2zaal_widget ul.counts {
    1012    grid-template-columns: repeat( auto-fill, minmax( 3rem, 1fr ) );
    1113}
     14.wp-block-nvwda2zaal-a2z-links li,
    1215.a2zaal_widget li {
    1316    position: relative;
    1417}
    15 
     18.wp-block-nvwda2zaal-a2z-links a,
    1619.a2zaal_widget a {
    1720    border-bottom: 1px solid transparent;
     
    2023    text-align: center;
    2124}
    22 
     25.wp-block-nvwda2zaal-a2z-links a:hover,
    2326.a2zaal_widget a:hover {
    2427    border-bottom: 1px solid blue;
     
    2629    text-decoration: underline;
    2730}
     31.wp-block-nvwda2zaal-a2z-links a.count > span,
    2832.a2zaal_widget a.count > span {
    2933    font-size: 75%;
     
    3236    white-space: nowrap;
    3337}
    34 /* premium styles for sliding counts
     38/*
     39ul.wp-block-nvwda2zaal-a2z-links.counts,
    3540.a2zaal_widget ul.counts {
    3641    grid-template-columns: repeat( auto-fit, minmax( 2.5em, 1fr ) );
     42    gap: 5px;
    3743}
     44.wp-block-nvwda2zaal-a2z-links li.count:after,
    3845.a2zaal_widget li.count:after {
    3946    content: '';
     
    5057    box-shadow: inset 1px 0 #99400e;
    5158}
    52 .a2zaal_widget li a, .a2zaal_widget li a span {
     59.wp-block-nvwda2zaal-a2z-links li a,
     60.wp-block-nvwda2zaal-a2z-links li a span,
     61.a2zaal_widget li a,
     62.a2zaal_widget li a span {
    5363    display: block;
    5464    -webkit-box-sizing: border-box;
     
    5767    white-space: nowrap;
    5868}
     69.wp-block-nvwda2zaal-a2z-links li a,
    5970.a2zaal_widget li a {
    6071    padding: 0.25em 0.5em;
     
    7687    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.7), 0 1px 2px rgba(0, 0, 0, 0.05);
    7788}
     89.wp-block-nvwda2zaal-a2z-links a:hover span,
    7890.a2zaal_widget a:hover span {
    7991    padding: 0.2em 7px 0.2em 6px;
     
    8294    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.15), 1px 1px 2px rgba(0, 0, 0, 0.2);
    8395}
     96.wp-block-nvwda2zaal-a2z-links span,
    8497.a2zaal_widget span {
    8598    position: absolute;
  • a2z-alphabetical-archive-links/trunk/readme.txt

    r2019541 r3193532  
    22Contributors: nvwd
    33Donate link: http://nvwebdev.com
    4 Tags: post title, custom post type title, cpt title, title, alphabetical, alphabatized
    5 Requires at least: 4.6.0
    6 Tested up to: 5.0.3
    7 Requires PHP: 5.6
    8 Stable tag: 2.0.2
     4Tags: post title, custom post type title, cpt title, title, alphabetical, alphabetized
     5Requires at least: 5.8.0
     6Tested up to: 6.7
     7Requires PHP: 7.4
     8Stable tag: 2.1.0
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    36361. Activate the plugin through the 'Plugins' menu in WordPress
    37371. Go to the settings page to activate which post type(s) to have
    38 1. Add the 'A2Z Alphabetical Archive Links' widget to a widget area
     381. Add the 'A2Z Alphabetical Archive Links' widget to a widget area or add the A2Z Links block to a block area
    3939
    4040== Frequently Asked Questions ==
     
    4444== Screenshots ==
    4545
    46 1. Widget config
    47 1. Links without count
    48 1. Links with count (top), hover example ( bottom )
     461. Legacy widget config
     471. A2Z Links block editor with counts disabled
     481. A2Z Links block editor with counts enabled
     491. A2Z Links block output with counts disabled
     501. A2Z Links block output with counts enabled
    4951
    5052== Changelog ==
     53
     54= 2.1.0 =
     55
     56* Added links block to replace legacy widget
     57* Updated minimum php version to 7.4
     58* Updated minimum WordPress version to 5.8
     59* Resolved RegEx issue that incorrectly grouped some titles in the group that begin with numbers.
    5160
    5261= 2.0.2 =
     
    5665= 2.0.1 =
    5766
    58 * fixed bug with earlier versions of php and version checking
     67fixed bug with earlier versions of php and version checking
    5968
    6069= 2.0.0 =
    6170
    62 Complete rewrite. In an effort to make the code scale better.
     71* Complete rewrite. In an effort to make the code scale better.
    6372* Removed slow query
    6473* Added minimum WordPress version check
     
    7382= 1.0.1 =
    7483
    75 * Changed widget option for post type to include only publically queriable post types ( Pages not an option anymore )
     84* Changed widget option for post type to include only publicly queryable post types ( Pages not an option anymore )
    7685* Fixed links generated when using the post type of Post
    7786
  • a2z-alphabetical-archive-links/trunk/src/helpers.php

    r2015868 r3193532  
    11<?php
    2 /**
    3  * A2Z Alphabetical Archive Links Helper Functions
    4  *
    5  * @package     NVWD\A2ZAAL
    6  * @since       2.0.0
    7  * @author      nvwd
    8  * @link        http://nvwebdev.com/
    9  * @license     GPL-2.0+
    10  */
    112
    123namespace NVWD\A2ZAAL;
    134
    14 /**
    15  * return an array of a2zaal active post types or an empty array when none are active
    16  *
    17  * @author: nvwd
    18  * @since: 2.0.0
     5use WP_Post;
     6
     7add_filter( 'widget_block_dynamic_classname', __NAMESPACE__ . '\add_widget_classname', 10, 2 );
     8
     9/**
     10 * Return an array of a2zaal active post types or an empty array when none are active
     11 *
     12 * @author nvwd
     13 *
     14 * @since 2.0.0
     15 *
    1916 * @return array
    2017 */
    2118function get_a2zaal_active_post_types() {
    22     $a2zaal_active_post_types = get_option( 'a2zaal_post_types', array() );
     19    $a2zaal_active_post_types = \get_option( 'a2zaal_post_types', [] );
    2320
    2421    if ( ! is_array( $a2zaal_active_post_types ) ) {
    25         return array();
     22        return [];
    2623    }
    2724
    2825    return $a2zaal_active_post_types;
    29 
    30 }
    31 
    32 /**
    33  * return an array of a2zaal post types that are currently processing or an empty array
    34  *
    35  * @author: nvwd
    36  * @since: 2.0.0
     26}
     27
     28/**
     29 * Return an array of a2zaal post types that are currently processing or an empty array
     30 *
     31 * @author nvwd
     32 *
     33 * @since 2.0.0
     34 *
    3735 * @return array
    3836 */
    3937function get_a2zaal_processing_post_types() {
    40     $a2zaal_processing_objects = get_option( 'a2zaal_processing_objects', array() );
     38    $a2zaal_processing_objects = \get_option( 'a2zaal_processing_objects', [] );
    4139
    4240    if ( empty( $a2zaal_processing_objects['post_types'] ) ) {
    43         return array();
     41        return [];
    4442    }
    4543
    4644    return $a2zaal_processing_objects['post_types'];
    47 
    48 }
    49 
    50 /**
    51  * this is where the magic happens, return the first character of the provided title and the string used for sorting
     45}
     46
     47/**
     48 * This is where the magic happens, return the first character of the provided title and the string used for sorting
    5249 * if the title begins with one or more 'a', 'an', 'and', and/or 'the', ignore them and goto the next word
    5350 * if the title then begins with a number return '#', otherwise return the first alpha character, capitalized.
    5451 * return the adjusted title for proper sorting when the links are retireved
    5552 *
    56  * @author: nvwd
    57  * @since: 2.0.0
    58  * @param string    $post_title
     53 * @author nvwd
     54 *
     55 * @since 2.0.0
     56 *
     57 * @param string $post_title Post title being processed for its grouping initial.
     58 *
    5959 * @return bool|array
    6060 */
    61 function get_post_a2zaal_info( $post_title ) {
     61function get_post_a2zaal_info( string $post_title ) {
    6262    if ( empty( $post_title ) ) {
    6363        return false;
    6464    }
    65     preg_match( '/\A[(a|an|and|the) ]*\b(([a-zA-Z]{1}|[\d]{1,}).*)\z/i', $post_title, $a2zaal_char );
    66     $post_a2zaal_info = array( 'sort_title' => $a2zaal_char[1], 'initial' => strtoupper( $a2zaal_char[2] ) );
     65
     66    // Regex explained: https://regex101.com/r/B8vbdX/1.
     67    preg_match( '/\A(?|a |an |and |the )*(([a-zA-Z]{1}|[\d]{1,}).*)\z/i', $post_title, $a2zaal_char );
     68    $post_a2zaal_info = [
     69        'sort_title' => $a2zaal_char[1],
     70        'initial'    => strtoupper( $a2zaal_char[2] ),
     71    ];
    6772
    6873    if ( is_numeric( $post_a2zaal_info['initial'] ) ) {
     
    7479
    7580/**
    76  * check if the query is an a2z query needing some extra stuff
    77  *
    78  * @author: nvwd
    79  * @since: 2.0.0
    80  * @return bool
     81 * Check if the query is an a2z query needing some extra stuff
     82 *
     83 * @author nvwd
     84 *
     85 * @since 2.0.0
     86 *
     87 * @return bool|string
    8188 */
    8289function is_a2zaal_query() {
    83 
    8490    if ( ! \is_main_query() || ! \is_post_type_archive() ) {
    8591        return false;
    8692    }
    8793
    88     if ( false === \get_query_var( A2ZAAL_REWRITE_TAG, false ) ) {
    89         return false;
    90     }
    91 
    92     return true;
    93 
    94 }
    95 
    96 /**
    97  * add sortable title and post ID to the structure used to order the posts alphabetically
    98  *
    99  * @author: nvwd
    100  * @since: 2.0.0
    101  * @param   WP_Post   $post     post being saved
    102  * @return void
    103  */
    104 function add_post_a2zaal_info( $post ) {
    105     $a2zaal_cpt_option = \get_option( $post->post_type . A2ZAAL_POSTS_SUFFIX, array() );
    106     $post_a2zaal_info = namespace\get_post_a2zaal_info( $post->post_title );
    107 
    108     if ( ! is_array( $post_a2zaal_info ) || ( array_key_exists( $post_a2zaal_info['initial'], $a2zaal_cpt_option ) && array_key_exists( $post->ID, $a2zaal_cpt_option[ $post_a2zaal_info['initial'] ] ) ) ) {
     94    /* phpcs:ignore SlevomatCodingStandard.ControlStructures.UselessTernaryOperator.UselessTernaryOperator -- returning the results of get_query_var is not reliable due to one of the values being 0, making it false. */
     95    return false === \get_query_var( A2ZAAL_REWRITE_TAG, false ) ? false : true;
     96}
     97
     98/**
     99 * Add sortable title and post ID to the structure used to order the posts alphabetically
     100 *
     101 * @author nvwd
     102 *
     103 * @since 2.0.0
     104 *
     105 * @param \WP_Post $post post being saved.
     106 */
     107function add_post_a2zaal_info( WP_Post $post ) {
     108    $a2zaal_cpt_option = \get_option( $post->post_type . A2ZAAL_POSTS_SUFFIX, [] );
     109    $post_a2zaal_info  = get_post_a2zaal_info( $post->post_title );
     110
     111    if (
     112        ! is_array( $post_a2zaal_info )
     113        || (
     114            array_key_exists( $post_a2zaal_info['initial'], $a2zaal_cpt_option )
     115            && array_key_exists( $post->ID, $a2zaal_cpt_option[ $post_a2zaal_info['initial'] ] )
     116        )
     117    ) {
    109118        return;
    110119    }
    111120
    112     // in the case of an update, the post may already exist in the a2z post info structure
    113     // ensure that the post doesn't already exist in the structure
    114     namespace\remove_prior_post_a2zaal_data( $a2zaal_cpt_option, $post->ID, $post_a2zaal_info['initial'] );
     121    // in the case of an update, the post may already exist in the a2z post info structure.
     122    // ensure that the post doesn't already exist in the structure.
     123    remove_prior_post_a2zaal_data( $a2zaal_cpt_option, $post->ID, $post_a2zaal_info['initial'] );
    115124
    116125    $a2zaal_cpt_option[ $post_a2zaal_info['initial'] ][ $post->ID ] = $post_a2zaal_info['sort_title'];
    117126
    118127    \update_option( $post->post_type . A2ZAAL_POSTS_SUFFIX, $a2zaal_cpt_option, true );
    119 
    120 }
    121 
    122 /**
    123  * in the case of a post update, check to see if the post already exists within the structure
     128}
     129
     130/**
     131 * In the case of a post update, check to see if the post already exists within the structure
    124132 * remove it if necessary
    125133 *
    126  * @author: nvwd
    127  * @since: 2.0.0
    128  * @param   array   $a2zaal_cpt_option  contains the post alphabetical detail for this post type
    129  * @param   int     $post_id            current post ID to possibly remove a previous entry
    130  * @param   string  $post_initial       initial of the current post title
    131  * @return void
    132  */
    133 function remove_prior_post_a2zaal_data( &$a2zaal_cpt_option, $post_id, $post_initial ) {
    134     foreach( $a2zaal_cpt_option AS $key => $initial_posts ) {
    135         if ( $key == $post_initial ) {
     134 * @author nvwd
     135 *
     136 * @since 2.0.0
     137 *
     138 * @param array  $a2zaal_cpt_option contains the post alphabetical detail for this post type.
     139 * @param int    $post_id current post ID to possibly remove a previous entry.
     140 * @param string $post_initial initial of the current post title.
     141 */
     142function remove_prior_post_a2zaal_data( array &$a2zaal_cpt_option, int $post_id, string $post_initial ) {
     143    foreach ( $a2zaal_cpt_option as $key => $initial_posts ) {
     144        if ( $key === $post_initial || ! array_key_exists( $post_id, $initial_posts ) ) {
    136145            continue;
    137146        }
    138         if ( array_key_exists( $post_id, $initial_posts ) ) {
    139             unset( $a2zaal_cpt_option[$key][$post_id] );
     147
     148        unset( $a2zaal_cpt_option[ $key ][ $post_id ] );
     149    }
     150}
     151
     152/**
     153 * Returns the array of a2z links for the widget/block to display.
     154 *
     155 * @author nvwd
     156 *
     157 * @since 2.1.0
     158 *
     159 * @param string $selected_post_type selected post type for the links.
     160 * @param bool   $show_counts whether to display counts for the initial groups.
     161 *
     162 * @return array
     163 */
     164function get_a2zaal_display_links( string $selected_post_type, bool $show_counts ) {
     165    $post_type_titles_struct = \get_option( $selected_post_type . A2ZAAL_POSTS_SUFFIX, [] );
     166
     167    if ( empty( $post_type_titles_struct ) ) {
     168        return [ '<p>' . \__( 'No links to display.', 'nvwd-a2zaal' ) . '</p>' ];
     169    }
     170
     171    $post_type_labels = \get_post_type_object( $selected_post_type )->labels;
     172
     173    ksort( $post_type_titles_struct, SORT_NATURAL );
     174
     175    /* translators: %s: plural post type name */
     176    $link_prefix = sprintf( \__( '%s beginning with ', 'nvwd-a2zaal' ), $post_type_labels->name );
     177
     178    foreach ( $post_type_titles_struct as $title_initial => $grouped_titles ) {
     179        $group_link          = '/' . $selected_post_type . '/' . A2ZAAL_REWRITE_TAG . '/' . $title_initial;
     180        $group_count_display = ! empty( $show_counts )
     181            ? '<span>' . \number_format_i18n( count( $grouped_titles ) ) . '</span>'
     182            : '';
     183        $link_classes        = ! empty( $show_counts ) ? [ 'count' ] : [];
     184        $link_classes        = implode( ' ', \apply_filters( 'a2zaal_link_css_class', $link_classes, $selected_post_type ) );
     185        $link_title          = trim(
     186            \apply_filters( 'a2zaal_link_title', $link_prefix . $title_initial, $selected_post_type, $title_initial )
     187        );
     188        $link_text_display   = $title_initial;
     189
     190        if ( 0 === $title_initial ) {
     191            $group_link        = '/' . $selected_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num';
     192            $link_text_display = '#';
    140193        }
    141     }
    142 }
     194
     195        // TODO: Make sure link is accessible.
     196        $display_links[] = sprintf(
     197            '<li><a href="%s" class="%s" title="%s">%s%s</a></li>',
     198            $group_link,
     199            $link_classes,
     200            $link_title,
     201            $link_text_display,
     202            $group_count_display
     203        );
     204    }
     205
     206    return $display_links;
     207}
     208
     209/**
     210 * Returns adjusted className list for a2z-links widget wrapper.
     211 *
     212 * @author nvwd
     213 *
     214 * @since 2.1.0
     215 *
     216 * @param string $classname list of classnames to be used in the block widget’s container HTML.
     217 * @param string $block_name name of the block contained by the block widget.
     218 *
     219 * @return string
     220 */
     221function add_widget_classname( string $classname, string $block_name ) {
     222    if ( 'nvwda2zaal/a2z-links' !== $block_name ) {
     223        return $classname;
     224    }
     225
     226    // phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable -- unused even though it's part of the return.
     227    return $classname .= ' a2zaal_widget';
     228}
  • a2z-alphabetical-archive-links/trunk/src/plugin.php

    r2015868 r3193532  
    11<?php
    2 /**
    3  * A2Z Alphabetical Archive Links Handler
    4  *
    5  * @package     NVWD\A2ZAAL
    6  * @since       2.0.0
    7  * @author      nvwd
    8  * @link        http://nvwebdev.com/
    9  * @license     GPL-2.0+
    10  */
    112
    123namespace NVWD\A2ZAAL;
     
    178define( 'A2ZAAL_VIEW_DIR', A2ZAAL_SOURCE_DIR . 'views/' );
    189
    19 include A2ZAAL_SOURCE_DIR . 'vender/wp-background-processing/wp-background-processing.php';
    20 include A2ZAAL_SOURCE_DIR . 'classes/A2ZAAL_Post_Type_Background_Process.php';
     10require A2ZAAL_SOURCE_DIR . 'vender/wp-background-processing/wp-background-processing.php';
     11require A2ZAAL_SOURCE_DIR . 'classes/class-a2zaal-post-type-background-process.php';
    2112
    2213if ( is_admin() ) {
    23     include A2ZAAL_SOURCE_DIR . 'settings.php';
     14    require A2ZAAL_SOURCE_DIR . 'settings.php';
    2415}
    25 include A2ZAAL_SOURCE_DIR . 'rewrites.php';
    26 include A2ZAAL_SOURCE_DIR . 'helpers.php';
    27 include A2ZAAL_SOURCE_DIR . 'widgets/a2zaal_widget.php';
     16
     17require A2ZAAL_SOURCE_DIR . 'rewrites.php';
     18require A2ZAAL_SOURCE_DIR . 'helpers.php';
     19require A2ZAAL_SOURCE_DIR . 'widgets/class-a2zaal-widget.php';
     20
     21\add_action( 'widgets_init', __NAMESPACE__ . '\maybe_register_archive_link_widget' );
     22
     23/**
     24 * Register the a2zaal widget only if there are active post types
     25 *
     26 * @author nvwd
     27 *
     28 * @since 2.0.0
     29 */
     30function maybe_register_archive_link_widget() {
     31    $a2z_active_post_types = get_a2zaal_active_post_types();
     32
     33    if ( empty( $a2z_active_post_types ) ) {
     34        return;
     35    }
     36
     37    \register_widget( __NAMESPACE__ . '\a2zaal_widget' );
     38}
    2839
    2940add_action( 'save_post', __NAMESPACE__ . '\maybe_add_a2zaal_post' );
    3041/**
    31  * add the a2zaal data to the specific structure when an active post type item is saved
     42 * Add the a2zaal data to the specific structure when an active post type item is saved
    3243 *
    33  * @author: nvwd
    34  * @since: 2.0.0
    35  * @param $post_id
    36  * @return void
     44 * @author nvwd
     45 *
     46 * @since 2.0.0
     47 *
     48 * @param int $post_id ID of the post being saved.
    3749 */
    38 function maybe_add_a2zaal_post( $post_id ) {
    39 
     50function maybe_add_a2zaal_post( int $post_id ) {
    4051    if ( \wp_is_post_revision( $post_id ) || \wp_is_post_autosave( $post_id ) ) {
    4152        return;
    4253    }
    4354
    44     $a2zaal_post = \get_post( $post_id );
    45     $a2zaal_active_post_types = namespace\get_a2zaal_active_post_types();
     55    $a2zaal_post              = \get_post( $post_id );
     56    $a2zaal_active_post_types = get_a2zaal_active_post_types();
    4657
    47     if ( ! in_array( $a2zaal_post->post_type, $a2zaal_active_post_types ) ) {
     58    if ( ! in_array( $a2zaal_post->post_type, $a2zaal_active_post_types, true ) ) {
    4859        return;
    4960    }
    5061
    51     namespace\add_post_a2zaal_info( $a2zaal_post );
    52 
     62    add_post_a2zaal_info( $a2zaal_post );
    5363}
    5464
    5565/**
    56  * make the a2zaal list page titles more readable
     66 * Make the a2zaal list page titles more readable
    5767 *
    58  * @author: nvwd
    59  * @since: 2.0.0
    60  * @param $archive_title
     68 * @author nvwd
     69 *
     70 * @since 2.0.0
     71 *
     72 * @param string $archive_title archive page title.
     73 *
    6174 * @return string
    6275 */
    63 function filter_archive_title( $archive_title ) {
    64     if ( ! namespace\is_a2zaal_query() ) {
     76function filter_archive_title( string $archive_title ) {
     77    if ( ! is_a2zaal_query() ) {
    6578        return $archive_title;
    6679    }
    6780
    68     $post_type_labels = get_post_type_object( get_query_var( 'post_type' ) );
    69     $group_title_initial = get_query_var( A2ZAAL_REWRITE_TAG );
     81    $post_type_labels    = \get_post_type_object( \get_query_var( 'post_type' ) );
     82    $group_title_initial = \get_query_var( A2ZAAL_REWRITE_TAG );
    7083
    71     if ( '0' == $group_title_initial ) {
     84    if ( '0' === $group_title_initial ) {
    7285        $group_title_initial = '#';
    7386    }
    7487
    75     return sprintf( __( '%1$s: %2$s' ), $post_type_labels->label, $group_title_initial );
     88    /* translators: 1: post type title 2: initial of the archive grouping */
     89    return sprintf( __( '%1$s: %2$s', 'nvwd-a2zaal' ), $post_type_labels->label, $group_title_initial );
    7690}
    7791
    7892/**
    79  * customize the a2zaal list page description
     93 * Customize the a2zaal list page description
    8094 *
    81  * @author: nvwd
    82  * @since: 2.0.0
    83  * @param $archive_description
     95 * @author nvwd
     96 *
     97 * @since 2.0.0
     98 *
     99 * @param string $archive_description archive page description.
     100 *
    84101 * @return string
    85102 */
    86 function filter_archive_description( $archive_description ) {
    87     if ( ! namespace\is_a2zaal_query() ) {
     103function filter_archive_description( string $archive_description ) {
     104    if ( ! is_a2zaal_query() ) {
    88105        return $archive_description;
    89106    }
    90107
    91     $post_type_labels = get_post_type_object( get_query_var( 'post_type' ) );
     108    $post_type_labels = \get_post_type_object( \get_query_var( 'post_type' ) );
    92109
    93     $group_title_initial = get_query_var( A2ZAAL_REWRITE_TAG );
     110    $group_title_initial = \get_query_var( A2ZAAL_REWRITE_TAG );
    94111
    95     if ( '0' == $group_title_initial ) {
    96         $group_title_initial = __( 'a number', A2ZAAL_TEXT_DOMAIN );
     112    if ( '0' === $group_title_initial ) {
     113        $group_title_initial = 'a number';
    97114    }
    98115
    99     return sprintf( __( '%1$s beginning with %2$s.' ), $post_type_labels->label, $group_title_initial );
     116    /* translators: 1: post type title 2: initial of the archive grouping */
     117    return sprintf( __( '%1$s beginning with %2$s.', 'nvwd-a2zaal' ), $post_type_labels->label, $group_title_initial );
    100118}
  • a2z-alphabetical-archive-links/trunk/src/rewrites.php

    r2015868 r3193532  
    11<?php
    2 /**
    3  * A2Z Alphabetical Archive Rewrites Handler
    4  *
    5  * @package     NVWD\A2ZAAL
    6  * @since       2.0.0
    7  * @author      nvwd
    8  * @link        http://nvwebdev.com/
    9  * @license     GPL-2.0+
    10  */
    112
    123namespace NVWD\A2ZAAL;
     4
     5use WP;
     6use WP_Query;
    137
    148\add_filter( 'rewrite_rules_array', __NAMESPACE__ . '\maybe_modify_rewrite_rules_array' );
    159
    1610/**
    17  * add rewrite rules for each a2z active post type
     11 * Add rewrite rules for each a2z active post type
    1812 *
    19  * @author: nvwd
    20  * @since: 2.0.0
    21  * @param   array   $rewrite_rules  the rewrite structure passed in
    22  * @return  array                   the possibly updated rewrite structure
     13 * @author nvwd
     14 *
     15 * @since 2.0.0
     16 *
     17 * @param array $rewrite_rules the rewrite structure passed in.
     18 *
     19 * @return array
    2320 */
    24 function maybe_modify_rewrite_rules_array( $rewrite_rules ) {
    25     $a2zaal_active_post_types = namespace\get_a2zaal_active_post_types();
     21function maybe_modify_rewrite_rules_array( array $rewrite_rules ) {
     22    $a2zaal_active_post_types = get_a2zaal_active_post_types();
    2623
    2724    if ( empty( $a2zaal_active_post_types ) ) {
     
    2926    }
    3027
    31     $new_rules = array();
     28    $new_rules = [];
    3229
    33     foreach ( $a2zaal_active_post_types AS $active_post_type ) {
    34         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/?$']                              = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=all';
    35         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/page/?([0-9]{1,})/?$']            = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=all&paged=$matches[2]';
    36         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/([a-zA-Z])/?$']                   = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=$matches[1]';
    37         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num/?$']                          = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=0';
    38         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/([a-zA-Z])/page/?([0-9]{1,})/?$'] = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=$matches[1]&paged=$matches[2]';
    39         $new_rules[$active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num/page/?([0-9]{1,})/?$']        = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=0&paged=$matches[1]';
     30    // phpcs:disable SlevomatCodingStandard.Files.LineLength.LineTooLong -- line length due to alignment for each rewrite rule.
     31    foreach ( $a2zaal_active_post_types as $active_post_type ) {
     32        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/?$' ]                              = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=all';
     33        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/page/?([0-9]{1,})/?$' ]            = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=all&paged=$matches[2]';
     34        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/([a-zA-Z])/?$' ]                   = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=$matches[1]';
     35        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num/?$' ]                          = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=0';
     36        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/([a-zA-Z])/page/?([0-9]{1,})/?$' ] = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=$matches[1]&paged=$matches[2]';
     37        $new_rules[ $active_post_type . '/' . A2ZAAL_REWRITE_TAG . '/num/page/?([0-9]{1,})/?$' ]        = 'index.php?post_type=' . $active_post_type . '&' . A2ZAAL_REWRITE_TAG . '=0&paged=$matches[1]';
    4038    }
     39
     40    // phpcs:enable SlevomatCodingStandard.Files.LineLength.LineTooLong
    4141
    4242    return array_merge( $new_rules, $rewrite_rules );
     
    4646
    4747/**
    48  * add the query tag being used for holding the specific initial
     48 * Add the query tag being used for holding the specific initial
    4949 *
    50  * @author: nvwd
    51  * @since: 2.0.0
    52  * @return void
     50 * @author nvwd
     51 *
     52 * @since 2.0.0
    5353 */
    5454function add_a2zaal_rewrite_tag() {
     
    5959
    6060/**
    61  * add hooks to modify the query if the a2z rewrite tag exists
     61 * Add hooks to modify the query if the a2z rewrite tag exists
    6262 *
    63  * @author: nvwd
    64  * @since: 2.0.0
    65  * @param   WP_Query    $query  main query
    66  * @return void
     63 * @author nvwd
     64 *
     65 * @since 2.0.0
     66 *
     67 * @param \WP $query WordPress environment instance.
    6768 */
    68 function maybe_alter_query_for_a2zaal( $query ) {
     69function maybe_alter_query_for_a2zaal( WP $query ) {
    6970    if ( ! isset( $query->query_vars[ A2ZAAL_REWRITE_TAG ] ) ) {
    7071        return;
     
    7980
    8081/**
    81  * pull all posts until I can build in pagination, then make pagination optional
     82 * Pull all posts until I can build in pagination, then make pagination optional
    8283 *
    83  * @author: nvwd
    84  * @since: 2.0.0
    85  * @param   WP_Query    $query  main query
    86  * @return void
     84 * @author nvwd
     85 *
     86 * @since 2.0.0
     87 *
     88 * @param \WP_Query $query main query.
    8789 */
    88 function kill_pagination( $query ) {
     90function kill_pagination( WP_Query $query ) {
    8991    $query->set( 'posts_per_page', -1 );
    9092    $query->set( 'no_found_rows', true );
     
    9294
    9395/**
    94  * add IN clause providing a list of post IDs to be retrieved by the query
     96 * Add IN clause providing a list of post IDs to be retrieved by the query
    9597 *
    96  * @author: nvwd
    97  * @since: 2.0.0
    98  * @param   string  $posts_where    query where clause
    99  * @return  string                  maybe modified where clause
     98 * @author nvwd
     99 *
     100 * @since 2.0.0
     101 *
     102 * @param string $posts_where query where clause.
     103 *
     104 * @return string
    100105 */
    101 function filter_a2zaal_where_clause( $posts_where ) {
     106function filter_a2zaal_where_clause( string $posts_where ) {
    102107    global $wpdb;
    103108
    104     $post_type_a2zaal_struct = \get_option( \get_query_var( 'post_type' ) . A2ZAAL_POSTS_SUFFIX, array() );
     109    $post_type_a2zaal_struct = \get_option( \get_query_var( 'post_type' ) . A2ZAAL_POSTS_SUFFIX, [] );
    105110
    106111    if ( empty( $post_type_a2zaal_struct ) ) {
     
    112117    $specific_post_ids = implode( ',', array_keys( $post_type_a2zaal_struct[ $a2zaal_query_var ] ) );
    113118
    114     $posts_where .= " AND ( $wpdb->posts.ID IN (" . $specific_post_ids . ") )";
     119    $posts_where .= " AND ( $wpdb->posts.ID IN (" . $specific_post_ids . ') )';
    115120
    116121    return $posts_where;
     
    118123
    119124/**
    120  * set the sort order using the naturally sorted object titles
     125 * Set the sort order using the naturally sorted object titles
    121126 *
    122  * @author: nvwd
    123  * @since: 2.0.0
    124  * @param   string  $posts_orderby  orderby clause
    125  * @return  string                  maybe new orderby clause
     127 * @author nvwd
     128 *
     129 * @since 2.0.0
     130 *
     131 * @param string $posts_orderby orderby clause.
     132 *
     133 * @return string
    126134 */
    127 function filter_a2zaal_orderby_clause( $posts_orderby ) {
     135function filter_a2zaal_orderby_clause( string $posts_orderby ) {
    128136    global $wpdb;
    129137
    130     $post_type_a2zaal_struct = \get_option( \get_query_var( 'post_type' ) . A2ZAAL_POSTS_SUFFIX, array() );
     138    $post_type_a2zaal_struct = \get_option( \get_query_var( 'post_type' ) . A2ZAAL_POSTS_SUFFIX, [] );
    131139
    132140    if ( empty( $post_type_a2zaal_struct ) ) {
     
    148156
    149157/**
    150  * check to see if posts/objects for the specified a2z initial exists in the post type structure
     158 * Check to see if posts/objects for the specified a2z initial exists in the post type structure
    151159 *
    152  * @author: nvwd
    153  * @since: 2.0.0
    154  * @param $query
    155  * @return void
     160 * @author nvwd
     161 *
     162 * @since 2.0.0
     163 *
     164 * @param \WP_Query $query query to check to see if it's a 404.
    156165 */
    157 function check_a2zaal_404( &$query ) {
    158 
    159     if ( ! namespace\is_a2zaal_query() || \is_admin() ) {
     166function check_a2zaal_404( WP_Query &$query ) {
     167    if ( ! is_a2zaal_query() || \is_admin() ) {
    160168        return;
    161169    }
    162170
    163     $post_type_a2zaal_struct = \get_option( $query->query_vars['post_type'] . A2ZAAL_POSTS_SUFFIX, array() );
     171    $post_type_a2zaal_struct = \get_option( $query->query_vars['post_type'] . A2ZAAL_POSTS_SUFFIX, [] );
    164172
    165173    if ( empty( $post_type_a2zaal_struct ) ) {
     
    167175    }
    168176
    169     if ( array_key_exists( $query->query_vars[ A2ZAAL_REWRITE_TAG ], $post_type_a2zaal_struct ) || 'all' == $query->query_vars[ A2ZAAL_REWRITE_TAG ] ) {
     177    if (
     178        array_key_exists( $query->query_vars[ A2ZAAL_REWRITE_TAG ], $post_type_a2zaal_struct )
     179        || 'all' === $query->query_vars[ A2ZAAL_REWRITE_TAG ]
     180    ) {
    170181        return;
    171182    }
     
    173184    $query->set_404();
    174185}
    175 
  • a2z-alphabetical-archive-links/trunk/src/settings.php

    r2015868 r3193532  
    11<?php
    2 /**
    3  * A2Z Alphabetical Archive Links Settings Page Handler
    4  *
    5  * @package     NVWD\A2ZAAL
    6  * @since       2.0.0
    7  * @author      nvwd
    8  * @link        http://nvwebdev.com/
    9  * @license     GPL-2.0+
    10  */
    112
    123namespace NVWD\A2ZAAL;
    134
     5use WP_Screen;
     6
    147\add_filter( 'plugin_action_links_' . A2ZAAL_BASENAME, __NAMESPACE__ . '\add_settings_page_link' );
    158
    16 function add_settings_page_link( $links ) {
    17     $settings_link = '<a href="' . \admin_url( 'options-general.php?page=a2zaal-options' ) . '">' . __( 'Settings', A2ZAAL_TEXT_DOMAIN ) . '</a>';
     9/**
     10 * Adds custom settings page to the admin settings menu
     11 *
     12 * @author nvwd
     13 *
     14 * @since 2.0.0
     15 *
     16 * @param array $links admin menu links.
     17 *
     18 * @return array
     19 */
     20function add_settings_page_link( array $links ) {
     21    $settings_link = sprintf(
     22        '<a href="%s">%s</a>',
     23        \admin_url( 'options-general.php?page=a2zaal-options' ),
     24        \__( 'Settings', 'nvwd-a2zaal' )
     25    );
    1826    array_unshift( $links, $settings_link );
     27
    1928    return $links;
    2029}
     
    2332
    2433/**
    25  * add admin settings page for our use
    26  *
    27  * @author: nvwd
    28  * @since: 2.0.0
    29  * @return void
     34 * Add admin settings page for our use
     35 *
     36 * @author nvwd
     37 *
     38 * @since 2.0.0
    3039 */
    3140function add_settings_menu_link() {
    32     \add_options_page( 'A2Z Alphabetical Archive Links Options', 'A2Z Alphabetical Archive Links', 'manage_options', 'a2zaal-options', __NAMESPACE__ . '\create_settings_page' );
    33     //enqueue javascript and styles needed for admin options page
    34 }
    35 
    36 /**
    37  * admin page creation script
    38  *
    39  * @author: nvwd
    40  * @since: 2.0.0
    41  * @return void
     41    \add_options_page(
     42        'A2Z Alphabetical Archive Links Options',
     43        'A2Z Alphabetical Archive Links',
     44        'manage_options',
     45        'a2zaal-options',
     46        __NAMESPACE__ . '\create_settings_page'
     47    );
     48    // enqueue javascript and styles needed for admin options page.
     49}
     50
     51/**
     52 * Admin page creation script
     53 *
     54 * @author nvwd
     55 *
     56 * @since 2.0.0
    4257 */
    4358function create_settings_page() {
    44 
    4559    if ( ! \current_user_can( 'manage_options' ) ) {
    4660        die( 'Unauthorized User' );
    4761    }
    4862
    49     $a2zaal_active_post_types = namespace\get_a2zaal_active_post_types();
    50 
    51     $a2zaal_processing_post_types = namespace\get_a2zaal_processing_post_types();
    52 
    53     $excluded_post_types = array(
    54         'attachment',
    55         'revision',
    56         'nav_menu_item'
    57     );
    58 
    59     $registered_post_types = \get_post_types( array( 'publicly_queryable' => true ), 'objects' );
    60 
    61     $a2zaal_processing_counts = \get_option( 'a2zaal_processing_counts', array() );
    62 
    63     include( A2ZAAL_VIEW_DIR . 'settings_form.php' );
     63    require A2ZAAL_VIEW_DIR . 'settings-form.php';
     64}
     65
     66\add_action( 'admin_init', __NAMESPACE__ . '\register_plugin_settings' );
     67\add_action( 'rest_admin_init', __NAMESPACE__ . '\register_plugin_settings' );
     68
     69/**
     70 * Register block widget
     71 *
     72 * @author nvwd
     73 *
     74 * @since 2.1.0
     75 */
     76function register_plugin_settings() {
     77    $args = [
     78        'default'      => [],
     79        'type'         => 'array',
     80        'show_in_rest' => [
     81            'schema' => [
     82                'type'  => 'array',
     83                'items' => [
     84                    'type' => 'string',
     85                ],
     86            ],
     87        ],
     88    ];
     89
     90    \register_setting( 'options', 'a2zaal_post_types', $args );
     91}
     92
     93add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\output_a2z_active_post_types' );
     94
     95/**
     96 * Outputs the a2z active post types for use in the block editor
     97 *
     98 * @author nvwd
     99 *
     100 * @since 2.1.0
     101 */
     102function output_a2z_active_post_types() {
     103    $a2z_active_post_types = get_a2zaal_active_post_types();
     104
     105    \wp_add_inline_script(
     106        'nvwda2zaal-a2z-links-editor-script',
     107        'const NVWDA2ZAAL_activePostTypes = ' . \wp_json_encode( $a2z_active_post_types ) . ';',
     108        'before'
     109    );
    64110}
    65111
     
    67113
    68114/**
    69  * handle the submission of our settings page
    70  *
    71  * @author: nvwd
    72  * @since: 2.0.0
    73  * @param $screen_obj
    74  * @return void
    75  */
    76 function maybe_process_a2zaal_settings_save( $screen_obj ) {
    77 
     115 * Handle the submission of our settings page
     116 *
     117 * @author nvwd
     118 *
     119 * @since 2.0.0
     120 *
     121 * @param \WP_Screen $screen_obj current screen.
     122 */
     123function maybe_process_a2zaal_settings_save( WP_Screen $screen_obj ) {
    78124    if ( 'settings_page_a2zaal-options' !== $screen_obj->id ) {
    79125        return;
    80126    }
    81127
    82     \wp_enqueue_script( 'a2zaal_settings_admin', A2ZAAL_ROOT_URL . 'js/a2zaal.js', array( 'jquery' ), false, true );
    83 
    84     if ( ! empty( $_POST['sbmt_a2zaal_settings'] ) ) {
    85         namespace\process_options_submission( namespace\get_a2zaal_active_post_types() );
    86     }
    87 }
    88 
    89 /**
    90  * control the a2zaal activation and deactivation of specified post types
    91  *
    92  * @author: nvwd
    93  * @since: 2.0.0
    94  * @param $a2zaal_original_cpts
    95  * @return void
    96  */
    97 function process_options_submission( $a2zaal_original_cpts ) {
    98 
     128    \wp_enqueue_script( 'a2zaal_settings_admin', A2ZAAL_ROOT_URL . 'js/a2zaal.js', [ 'jquery' ], A2ZAAL_VERSION, true );
     129
     130    // phpcs:ignore WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable -- the nonce is checked in process_options_submission() if this _POST var is not empty.
     131    if ( empty( $_POST['sbmt_a2zaal_settings'] ) ) {
     132        return;
     133    }
     134
     135    process_options_submission( get_a2zaal_active_post_types() );
     136}
     137
     138/**
     139 * Control the a2zaal activation and deactivation of specified post types
     140 *
     141 * @author nvwd
     142 *
     143 * @since 2.0.0
     144 *
     145 * @param array $a2zaal_original_cpts a2zaal active post types.
     146 */
     147function process_options_submission( array $a2zaal_original_cpts ) {
    99148    \check_admin_referer( 'a2zaal-options' );
    100149
    101     $enable_cpts = array();
    102     $disable_cpts = array();
    103 
    104     if ( isset( $_POST['a2zaal_enabled_post_type'] ) ) {
    105         $submitted_cpts = $_POST['a2zaal_enabled_post_type'];
    106         $enable_cpts = array_diff( $_POST['a2zaal_enabled_post_type'], $a2zaal_original_cpts );
    107         $disable_cpts = array_diff( $a2zaal_original_cpts, $_POST['a2zaal_enabled_post_type'] );
     150    // phpcs:disable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable -- checking if the _POST var exists before sanitizing it.
     151    $submitted_cpts = isset( $_POST['a2zaal_enabled_post_type'] )
     152        ? array_map( 'sanitize_text_field', \wp_unslash( $_POST['a2zaal_enabled_post_type'] ) )
     153        : false;
     154    // phpcs:enable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
     155
     156    $enable_cpts  = [];
     157    $disable_cpts = [];
     158
     159    if ( $submitted_cpts ) {
     160        $enable_cpts  = array_diff( $submitted_cpts, $a2zaal_original_cpts );
     161        $disable_cpts = array_diff( $a2zaal_original_cpts, $submitted_cpts );
    108162    } else {
    109         $submitted_cpts = array();
    110         $disable_cpts = $a2zaal_original_cpts;
    111     }
    112 
    113     namespace\maybe_remove_disabled_cpt_a2zaal_posts( $disable_cpts );
    114 
    115     namespace\maybe_setup_background_post_type_process( $enable_cpts );
     163        $submitted_cpts = [];
     164        $disable_cpts   = $a2zaal_original_cpts;
     165    }
     166
     167    maybe_remove_disabled_cpt_a2zaal_posts( $disable_cpts );
     168
     169    maybe_setup_background_post_type_process( $enable_cpts );
    116170
    117171    if ( ! empty( $disable_cpts ) || ! empty( $enable_cpts ) ) {
     
    120174
    121175    \update_option( 'a2zaal_post_types', $submitted_cpts, true );
    122 
    123 }
    124 
    125 /**
    126  * disable specified post types a2zaal and create the trigger to delete the associated terms
    127  *
    128  * @author: nvwd
    129  * @since: 2.0.0
    130  * @param $disable_cpts
    131  * @return void
    132  */
    133 function maybe_remove_disabled_cpt_a2zaal_posts( $disable_cpts ) {
     176}
     177
     178/**
     179 * Disable specified post types a2zaal and create the trigger to delete the associated terms
     180 *
     181 * @author nvwd
     182 *
     183 * @since 2.0.0
     184 *
     185 * @param array $disable_cpts cpts being disabled.
     186 */
     187function maybe_remove_disabled_cpt_a2zaal_posts( array $disable_cpts ) {
    134188    if ( empty( $disable_cpts ) ) {
    135189        return;
    136190    }
    137191
    138     foreach ( $disable_cpts AS $disabled_post_type ) {
    139         namespace\remove_disabled_cpts( $disabled_post_type );
    140         namespace\maybe_remove_current_processing( $disabled_post_type );
     192    foreach ( $disable_cpts as $disabled_post_type ) {
     193        remove_disabled_cpts( $disabled_post_type );
     194        maybe_remove_current_processing( $disabled_post_type );
    141195    }
    142196
     
    145199
    146200/**
    147  * remove a2zaal data for disabled post types
    148  *
    149  * @author: nvwd
    150  * @since: 2.0.0
    151  * @param $disabled_post_type
    152  * @return void
    153  */
    154 function remove_disabled_cpts( $disabled_post_type ) {
     201 * Remove a2zaal data for disabled post types
     202 *
     203 * @author nvwd
     204 *
     205 * @since 2.0.0
     206 *
     207 * @param string $disabled_post_type post type to delete its option data.
     208 */
     209function remove_disabled_cpts( string $disabled_post_type ) {
    155210    $a2zaal_option = $disabled_post_type . A2ZAAL_POSTS_SUFFIX;
    156211
    157212    \delete_option( $a2zaal_option );
    158 
    159 }
    160 
    161 function maybe_remove_current_processing( $disabled_post_type ) {
    162     $a2zaal_background_processing = \get_option( 'a2zaal_background_processing', array() );
    163 
    164     if ( ! isset( $a2zaal_background_processing['post-type'][$disabled_post_type] ) ) {
    165         return;
    166     }
    167 
    168     unset( $a2zaal_background_processing['post-type'][$disabled_post_type] );
     213}
     214
     215/**
     216 * Disable background processing for disabled post type.
     217 *
     218 * @author nvwd
     219 *
     220 * @since 2.0.0
     221 *
     222 * @param string $disabled_post_type disabled post type to check for background processing.
     223 */
     224function maybe_remove_current_processing( string $disabled_post_type ) {
     225    $a2zaal_background_processing = \get_option( 'a2zaal_background_processing', [] );
     226
     227    if ( ! isset( $a2zaal_background_processing['post-type'][ $disabled_post_type ] ) ) {
     228        return;
     229    }
     230
     231    unset( $a2zaal_background_processing['post-type'][ $disabled_post_type ] );
    169232    \update_option( 'a2zaal_background_processing', $a2zaal_background_processing, true );
    170233
    171     if ( empty( $a2zaal_background_processing['post-type'] ) ) {
    172         namespace\clear_scheduled_processing( 'a2zaal_process_activation_cron' );
    173     }
    174 
    175     return;
    176 }
    177 
    178 /**
    179  * create the admin notice that the disable request is a success
    180  *
    181  * @author: nvwd
    182  * @since: 2.0.0
    183  * @return void
     234    if ( ! empty( $a2zaal_background_processing['post-type'] ) ) {
     235        return;
     236    }
     237
     238    clear_scheduled_processing( 'a2zaal_process_activation_cron' );
     239}
     240
     241/**
     242 * Create the admin notice that the disable request is a success
     243 *
     244 * @author nvwd
     245 *
     246 * @since 2.0.0
    184247 */
    185248function disabled_a2zaal_cpts_admin_notice() {
    186     $class = 'notice notice-success is-dismissible';
    187     $message = __( 'The specified Post Type(s) are now disabled for A2Z Alphabetical Archive Links.', A2ZAAL_TEXT_DOMAIN );
     249    $class   = 'notice notice-success is-dismissible';
     250    $message = __( 'The specified Post Type(s) are now disabled for A2Z Alphabetical Archive Links.', 'nvwd-a2zaal' );
    188251
    189252    a2zaal_output_admin_notice( $class, $message );
     
    191254
    192255/**
    193  * register a2zaal activated post types and create structure to hold the initials and sort titles for
     256 * Register a2zaal activated post types and create structure to hold the initials and sort titles for
    194257 * existing post type items
    195258 *
    196  * @author: nvwd
    197  * @since: 2.0.0
    198  * @param $enable_cpts
    199  * @return void
    200  */
    201 function maybe_setup_background_post_type_process( $enable_post_type ) {
     259 * @author nvwd
     260 *
     261 * @since 2.0.0
     262 *
     263 * @param array $enable_post_type cpts to enable.
     264 */
     265function maybe_setup_background_post_type_process( array $enable_post_type ) {
    202266    if ( empty( $enable_post_type ) ) {
    203267        return;
     
    210274
    211275/**
    212  * create admin notice that the activate request is a success
    213  *
    214  * @author: nvwd
    215  * @since: 2.0.0
    216  * @return void
     276 * Create admin notice that the activate request is a success
     277 *
     278 * @author nvwd
     279 *
     280 * @since 2.0.0
    217281 */
    218282function enabled_a2zaal_cpts_admin_notice() {
    219     $class = 'notice notice-success is-dismissible';
    220     $message = __( 'The specified Post Type(s) are being processed in the background for A2Z Alphabetical Archive Links. Leaving this page does not stop the process.', A2ZAAL_TEXT_DOMAIN );
     283    $class   = 'notice notice-success is-dismissible';
     284    $message = __(
     285        'The specified Post Type(s) are being processed in the background for A2Z Alphabetical Archive Links.
     286        Leaving this page does not stop the process.',
     287        'nvwd-a2zaal'
     288    );
    221289
    222290    a2zaal_output_admin_notice( $class, $message );
Note: See TracChangeset for help on using the changeset viewer.