/** * Outputs HTML select menu of all registered styles. * * @since 0.1.0 * @uses postscript_style_handles() Array of registered style handles */ function postscript_add_style_callback() { // Array of alphabetized, front-end registered script handles. $style_handles = postscript_style_handles(); // Output HTML select menu of (sorted) handles. ?> <select id="postscript-add-style" name="postscript[add_style]"> <option value=''><?php _e('Select style to add:', 'postscript'); ?> </option> <?php foreach ($style_handles as $style_handle) { $handle = esc_attr($style_handle); echo "<option value=\"{$handle}\">{$handle}</option>"; } ?> </select> <?php // Get user-selected style handles (stored as tax terms). $args = array('hide_empty' => false, 'fields' => 'all'); $styles_added = get_terms('poststyles', $args); // Display table of selected handles (with $wp_styles data and term's post count). ?> <table class="wp-list-table widefat striped"> <caption><strong><?php _e('Styles added', 'postscript'); ?> </strong></caption> <thead> <tr> <th scope="col" class="th-full" style="padding: 0.5em;"><?php _e('Handle', 'postscript'); ?> </th> <th scope="col" class="th-full" style="padding: 0.5em;"><?php _e('Ver', 'postscript'); ?> </th> <th scope="col" class="th-full" style="padding: 0.5em;"><?php _e('Deps', 'postscript'); ?> </th> <th scope="col" class="th-full" style="padding: 0.5em;"><?php _e('Media', 'postscript'); ?> </th> <th scope="col" class="th-full" style="padding: 0.5em;"><?php _e('Posts', 'postscript'); ?> </th> </tr> </thead> <tbody> <?php // Array of registered styles' data (the transient holds front-end $wp_styles). $postscript_styles_reg = get_transient('postscript_styles_reg'); if (!empty($styles_added)) { foreach ($styles_added as $style_obj) { if (in_array($style_obj->name, $style_handles)) { $style_name = esc_html($style_obj->name); $style_arr = $postscript_styles_reg[$style_name]; // Comma-separated list of style dependencies. $deps = implode(',', $style_arr->deps); // Does script load in footer? $footer = isset($script_arr->extra['group']) ? $script_arr->extra['group'] : ''; // Make relative URLs full (for core registered scripts in '/wp-admin' or '/wp-includes'). $src = $style_arr->src ? postscript_core_full_urls($style_arr->src) : ''; // Add link to handle name, if registered source file. $handle = $src ? "<a href=\"{$src}\">" . $style_name . '</a>' : $style_name; // Tax term post count, linked to list of posts (if count>0). $count = $style_obj->count; if ($count) { $posts_count_url = admin_url() . "edit.php?poststyles={$style_name}\""; $posts_count = '<a href="' . esc_url($posts_count_url) . "\">{$count}</a>"; } else { $posts_count = $count; } // For wp_kses() sanitization of HTML. $allowed_html = array('a' => array('href' => array())); $allowed_protocols = array('http', 'https'); ?> <tr> <th scope="row" class="th-full" style="padding: 0.5em;"> <label><?php echo wp_kses($handle, $allowed_html, $allowed_protocols); ?> </label> </th> <td><?php echo esc_html($style_arr->ver); ?> </td> <td><?php echo esc_html($deps); ?> </td> <td><?php echo esc_html($footer); ?> </td> <td><?php echo wp_kses($posts_count, $allowed_html, $allowed_protocols); ?> </td> </tr> <?php } // if } // foreach } else { ?> <tr><td><p><?php _e('You have not added any styles yet.', 'postscript'); ?> </p></td></tr> <?php } ?> </tbody> </table> <?php }
/** * Allows only registered handles as custom tax terms. * * Late priority number to ensure font-end script handles are loaded. * * @uses postscript_script_handles() Returns array of registered scripts. * @uses postscript_style_handles() Returns array of registered styles. * @return string $term Submitted taxonomy term (or WP_Error admin-notice). */ function postscript_check_tax_term($term, $taxonomy) { if ($taxonomy == 'postscripts') { $script_handles = postscript_script_handles(); if (in_array($term, $script_handles)) { return $term; } else { return new WP_Error('invalid_term', __('The script handle you entered is <strong>not</strong> <a href="https://developer.wordpress.org/reference/functions/wp_register_script/">registered</a>.')); } } if ($taxonomy == 'poststyles') { $style_handles = postscript_style_handles(); if (in_array($term, $style_handles)) { return $term; } else { return new WP_Error('invalid_term', __('The style handle you entered is <strong>not</strong> <a href="https://developer.wordpress.org/reference/functions/wp_register_style/">registered</a>.')); } } return $term; }