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