home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / includes / class-wp-themes-list-table.php < prev    next >
Encoding:
PHP Script  |  2017-08-03  |  9.0 KB  |  303 lines

  1. <?php
  2. /**
  3.  * List Table API: WP_Themes_List_Table class
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Administration
  7.  * @since 3.1.0
  8.  */
  9.  
  10. /**
  11.  * Core class used to implement displaying installed themes in a list table.
  12.  *
  13.  * @since 3.1.0
  14.  * @access private
  15.  *
  16.  * @see WP_List_Table
  17.  */
  18. class WP_Themes_List_Table extends WP_List_Table {
  19.  
  20.     protected $search_terms = array();
  21.     public $features = array();
  22.  
  23.     /**
  24.      * Constructor.
  25.      *
  26.      * @since 3.1.0
  27.      *
  28.      * @see WP_List_Table::__construct() for more information on default arguments.
  29.      *
  30.      * @param array $args An associative array of arguments.
  31.      */
  32.     public function __construct( $args = array() ) {
  33.         parent::__construct( array(
  34.             'ajax' => true,
  35.             'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  36.         ) );
  37.     }
  38.  
  39.     /**
  40.      *
  41.      * @return bool
  42.      */
  43.     public function ajax_user_can() {
  44.         // Do not check edit_theme_options here. Ajax calls for available themes require switch_themes.
  45.         return current_user_can( 'switch_themes' );
  46.     }
  47.  
  48.     /**
  49.      */
  50.     public function prepare_items() {
  51.         $themes = wp_get_themes( array( 'allowed' => true ) );
  52.  
  53.         if ( ! empty( $_REQUEST['s'] ) )
  54.             $this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
  55.  
  56.         if ( ! empty( $_REQUEST['features'] ) )
  57.             $this->features = $_REQUEST['features'];
  58.  
  59.         if ( $this->search_terms || $this->features ) {
  60.             foreach ( $themes as $key => $theme ) {
  61.                 if ( ! $this->search_theme( $theme ) )
  62.                     unset( $themes[ $key ] );
  63.             }
  64.         }
  65.  
  66.         unset( $themes[ get_option( 'stylesheet' ) ] );
  67.         WP_Theme::sort_by_name( $themes );
  68.  
  69.         $per_page = 36;
  70.         $page = $this->get_pagenum();
  71.  
  72.         $start = ( $page - 1 ) * $per_page;
  73.  
  74.         $this->items = array_slice( $themes, $start, $per_page, true );
  75.  
  76.         $this->set_pagination_args( array(
  77.             'total_items' => count( $themes ),
  78.             'per_page' => $per_page,
  79.             'infinite_scroll' => true,
  80.         ) );
  81.     }
  82.  
  83.     /**
  84.      */
  85.     public function no_items() {
  86.         if ( $this->search_terms || $this->features ) {
  87.             _e( 'No items found.' );
  88.             return;
  89.         }
  90.  
  91.         $blog_id = get_current_blog_id();
  92.         if ( is_multisite() ) {
  93.             if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
  94.                 printf( __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> or <a href="%2$s">install</a> more themes.' ), network_admin_url( 'site-themes.php?id=' . $blog_id ), network_admin_url( 'theme-install.php' ) );
  95.  
  96.                 return;
  97.             } elseif ( current_user_can( 'manage_network_themes' ) ) {
  98.                 printf( __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> more themes.' ), network_admin_url( 'site-themes.php?id=' . $blog_id ) );
  99.  
  100.                 return;
  101.             }
  102.             // Else, fallthrough. install_themes doesn't help if you can't enable it.
  103.         } else {
  104.             if ( current_user_can( 'install_themes' ) ) {
  105.                 printf( __( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress Theme Directory at any time: just click on the <a href="%s">Install Themes</a> tab above.' ), admin_url( 'theme-install.php' ) );
  106.  
  107.                 return;
  108.             }
  109.         }
  110.         // Fallthrough.
  111.         printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
  112.     }
  113.  
  114.     /**
  115.      * @param string $which
  116.      */
  117.     public function tablenav( $which = 'top' ) {
  118.         if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
  119.             return;
  120.         ?>
  121.         <div class="tablenav themes <?php echo $which; ?>">
  122.             <?php $this->pagination( $which ); ?>
  123.             <span class="spinner"></span>
  124.             <br class="clear" />
  125.         </div>
  126.         <?php
  127.     }
  128.  
  129.     /**
  130.      */
  131.     public function display() {
  132.         wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
  133. ?>
  134.         <?php $this->tablenav( 'top' ); ?>
  135.  
  136.         <div id="availablethemes">
  137.             <?php $this->display_rows_or_placeholder(); ?>
  138.         </div>
  139.  
  140.         <?php $this->tablenav( 'bottom' ); ?>
  141. <?php
  142.     }
  143.  
  144.     /**
  145.      *
  146.      * @return array
  147.      */
  148.     public function get_columns() {
  149.         return array();
  150.     }
  151.  
  152.     /**
  153.      */
  154.     public function display_rows_or_placeholder() {
  155.         if ( $this->has_items() ) {
  156.             $this->display_rows();
  157.         } else {
  158.             echo '<div class="no-items">';
  159.             $this->no_items();
  160.             echo '</div>';
  161.         }
  162.     }
  163.  
  164.     /**
  165.      */
  166.     public function display_rows() {
  167.         $themes = $this->items;
  168.  
  169.         foreach ( $themes as $theme ):
  170.             ?><div class="available-theme"><?php
  171.  
  172.             $template   = $theme->get_template();
  173.             $stylesheet = $theme->get_stylesheet();
  174.             $title      = $theme->display('Name');
  175.             $version    = $theme->display('Version');
  176.             $author     = $theme->display('Author');
  177.  
  178.             $activate_link = wp_nonce_url( "themes.php?action=activate&template=" . urlencode( $template ) . "&stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
  179.  
  180.             $actions = array();
  181.             $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
  182.                 . esc_attr( sprintf( __( 'Activate “%s”' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
  183.  
  184.             if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
  185.                 $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
  186.                     . __( 'Live Preview' ) . '</a>';
  187.             }
  188.  
  189.             if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
  190.                 $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
  191.                     . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n  'Cancel' to stop, 'OK' to delete." ), $title ) )
  192.                     . "' );" . '">' . __( 'Delete' ) . '</a>';
  193.  
  194.             /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
  195.             $actions       = apply_filters( 'theme_action_links', $actions, $theme, 'all' );
  196.  
  197.             /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
  198.             $actions       = apply_filters( "theme_action_links_$stylesheet", $actions, $theme, 'all' );
  199.             $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
  200.             unset( $actions['delete'] );
  201.  
  202.             ?>
  203.  
  204.             <span class="screenshot hide-if-customize">
  205.                 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  206.                     <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  207.                 <?php endif; ?>
  208.             </span>
  209.             <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
  210.                 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
  211.                     <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
  212.                 <?php endif; ?>
  213.             </a>
  214.  
  215.             <h3><?php echo $title; ?></h3>
  216.             <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
  217.             <div class="action-links">
  218.                 <ul>
  219.                     <?php foreach ( $actions as $action ): ?>
  220.                         <li><?php echo $action; ?></li>
  221.                     <?php endforeach; ?>
  222.                     <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
  223.                 </ul>
  224.                 <?php echo $delete_action; ?>
  225.  
  226.                 <?php theme_update_available( $theme ); ?>
  227.             </div>
  228.  
  229.             <div class="themedetaildiv hide-if-js">
  230.                 <p><strong><?php _e('Version:'); ?></strong> <?php echo $version; ?></p>
  231.                 <p><?php echo $theme->display('Description'); ?></p>
  232.                 <?php if ( $theme->parent() ) {
  233.                     printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
  234.                         __( 'https://codex.wordpress.org/Child_Themes' ),
  235.                         $theme->parent()->display( 'Name' ) );
  236.                 } ?>
  237.             </div>
  238.  
  239.             </div>
  240.         <?php
  241.         endforeach;
  242.     }
  243.  
  244.     /**
  245.      * @param WP_Theme $theme
  246.      * @return bool
  247.      */
  248.     public function search_theme( $theme ) {
  249.         // Search the features
  250.         foreach ( $this->features as $word ) {
  251.             if ( ! in_array( $word, $theme->get('Tags') ) )
  252.                 return false;
  253.         }
  254.  
  255.         // Match all phrases
  256.         foreach ( $this->search_terms as $word ) {
  257.             if ( in_array( $word, $theme->get('Tags') ) )
  258.                 continue;
  259.  
  260.             foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
  261.                 // Don't mark up; Do translate.
  262.                 if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
  263.                     continue 2;
  264.                 }
  265.             }
  266.  
  267.             if ( false !== stripos( $theme->get_stylesheet(), $word ) )
  268.                 continue;
  269.  
  270.             if ( false !== stripos( $theme->get_template(), $word ) )
  271.                 continue;
  272.  
  273.             return false;
  274.         }
  275.  
  276.         return true;
  277.     }
  278.  
  279.     /**
  280.      * Send required variables to JavaScript land
  281.      *
  282.      * @since 3.4.0
  283.      *
  284.      * @param array $extra_args
  285.      */
  286.     public function _js_vars( $extra_args = array() ) {
  287.         $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
  288.  
  289.         $args = array(
  290.             'search' => $search_string,
  291.             'features' => $this->features,
  292.             'paged' => $this->get_pagenum(),
  293.             'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
  294.         );
  295.  
  296.         if ( is_array( $extra_args ) )
  297.             $args = array_merge( $args, $extra_args );
  298.  
  299.         printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
  300.         parent::_js_vars();
  301.     }
  302. }
  303.