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