/** * Outputs HTML select menu of all registered scripts. * * @since 0.1.0 * @uses postscript_script_handles() Array of registered style handles */ function postscript_add_script_callback() { // Array of alphabetized front-end registered script handles. $script_handles = postscript_script_handles(); // Output HTML select menu of (sorted) registered script handles. ?> <select id="postscript-add-script" name="postscript[add_script]"> <option value=''><?php _e('Select script to add:', 'postscript'); ?> </option> <?php foreach ($script_handles as $script_handle) { $handle = esc_attr($script_handle); echo "<option value=\"{$handle}\">{$handle}</option>"; } ?> </select> <?php // Get user-selected script handles (stored as tax terms). $args = array('hide_empty' => false, 'fields' => 'all'); $scripts_added = get_terms('postscripts', $args); // Display table of selected handles (with $wp_scripts data and term's post count). ?> <table class="wp-list-table widefat striped"> <caption><strong><?php _e('Scripts 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('Footer', 'postscript'); ?> </th> <th scope="col" class="th-full" style="padding: 0.5em;"><?php _e('Posts', 'postscript'); ?> </th> </tr> </thead> <tbody> <?php // Array of registered scripts' data (transient stores front-end $wp_scripts). $postscript_scripts_reg = get_transient('postscript_scripts_reg'); if (!empty($scripts_added)) { foreach ($scripts_added as $script_obj) { if (in_array($script_obj->name, $script_handles)) { $script_name = esc_html($script_obj->name); $script_arr = $postscript_scripts_reg[$script_name]; // Comma-separated list of style dependencies. $deps = implode(', ', $script_arr->deps); // Make relative URLs full (for core registered scripts in '/wp-admin' or '/wp-includes'). $src = $script_arr->src ? postscript_core_full_urls($script_arr->src) : ''; // Add link to handle name, if registered source file. $handle = $src ? "<a href=\"{$src}\">" . $script_name . '</a>' : $script_name; // @todo Add status-code column. Check URL when adding script, add status to term meta. // Check URL status response code, if script has a 'src' set. // $status_code = ( $src ) ? "<a href='$src'>" . postscript_url_exists( $src ) . '</a>' : '--'; // Tax term post count, linked to list of posts (if count>0). $count = $script_obj->count; if ($count) { $posts_count_url = admin_url() . "edit.php?postscripts={$script_name}\""; $posts_count = '<a href="' . esc_url_raw($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($script_arr->ver); ?> </td> <td><?php echo esc_html($deps); ?> </td> <td><?php echo esc_html($script_arr->args); // Style media: 'screen', 'print', or 'all'. ?> </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 scripts yet.', 'postscript'); ?> </p></td></tr> <?php } ?> </tbody> </table> <p class="wp-ui-text-icon"> <?php _e('<strong>Handle</strong> name links to <code>src</code> file. <strong>Posts</strong> count links to a list of posts that enqueue the file.', 'postscript'); ?> <br /> </p> <?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; }