wp/wp-admin/includes/class-wp-themes-list-table.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     8  * @access private
     8  * @access private
     9  */
     9  */
    10 class WP_Themes_List_Table extends WP_List_Table {
    10 class WP_Themes_List_Table extends WP_List_Table {
    11 
    11 
    12 	protected $search_terms = array();
    12 	protected $search_terms = array();
    13 	var $features = array();
    13 	public $features = array();
    14 
    14 
    15 	function __construct( $args = array() ) {
    15 	/**
       
    16 	 * Constructor.
       
    17 	 *
       
    18 	 * @since 3.1.0
       
    19 	 * @access public
       
    20 	 *
       
    21 	 * @see WP_List_Table::__construct() for more information on default arguments.
       
    22 	 *
       
    23 	 * @param array $args An associative array of arguments.
       
    24 	 */
       
    25 	public function __construct( $args = array() ) {
    16 		parent::__construct( array(
    26 		parent::__construct( array(
    17 			'ajax' => true,
    27 			'ajax' => true,
    18 			'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
    28 			'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
    19 		) );
    29 		) );
    20 	}
    30 	}
    21 
    31 
    22 	function ajax_user_can() {
    32 	public function ajax_user_can() {
    23 		// Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
    33 		// Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
    24 		return current_user_can( 'switch_themes' );
    34 		return current_user_can( 'switch_themes' );
    25 	}
    35 	}
    26 
    36 
    27 	function prepare_items() {
    37 	public function prepare_items() {
    28 		$themes = wp_get_themes( array( 'allowed' => true ) );
    38 		$themes = wp_get_themes( array( 'allowed' => true ) );
    29 
    39 
    30 		if ( ! empty( $_REQUEST['s'] ) )
    40 		if ( ! empty( $_REQUEST['s'] ) )
    31 			$this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
    41 			$this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
    32 
    42 
    55 			'per_page' => $per_page,
    65 			'per_page' => $per_page,
    56 			'infinite_scroll' => true,
    66 			'infinite_scroll' => true,
    57 		) );
    67 		) );
    58 	}
    68 	}
    59 
    69 
    60 	function no_items() {
    70 	public function no_items() {
    61 		if ( $this->search_terms || $this->features ) {
    71 		if ( $this->search_terms || $this->features ) {
    62 			_e( 'No items found.' );
    72 			_e( 'No items found.' );
    63 			return;
    73 			return;
    64 		}
    74 		}
    65 
    75 
    71 			} elseif ( current_user_can( 'manage_network_themes' ) ) {
    81 			} 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'] ) );
    82 				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 
    83 
    74 				return;
    84 				return;
    75 			}
    85 			}
    76 			// else, fallthrough. install_themes doesn't help if you can't enable it.
    86 			// Else, fallthrough. install_themes doesn't help if you can't enable it.
    77 		} else {
    87 		} else {
    78 			if ( current_user_can( 'install_themes' ) ) {
    88 			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' ) );
    89 				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 
    90 
    81 				return;
    91 				return;
    83 		}
    93 		}
    84 		// Fallthrough.
    94 		// 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' ) );
    95 		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 	}
    96 	}
    87 
    97 
    88 	function tablenav( $which = 'top' ) {
    98 	/**
       
    99 	 * @param string $which
       
   100 	 * @return null
       
   101 	 */
       
   102 	public function tablenav( $which = 'top' ) {
    89 		if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
   103 		if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
    90 			return;
   104 			return;
    91 		?>
   105 		?>
    92 		<div class="tablenav themes <?php echo $which; ?>">
   106 		<div class="tablenav themes <?php echo $which; ?>">
    93 			<?php $this->pagination( $which ); ?>
   107 			<?php $this->pagination( $which ); ?>
    95 			<br class="clear" />
   109 			<br class="clear" />
    96 		</div>
   110 		</div>
    97 		<?php
   111 		<?php
    98 	}
   112 	}
    99 
   113 
   100 	function display() {
   114 	public function display() {
   101 		wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
   115 		wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
   102 ?>
   116 ?>
   103 		<?php $this->tablenav( 'top' ); ?>
   117 		<?php $this->tablenav( 'top' ); ?>
   104 
   118 
   105 		<div id="availablethemes">
   119 		<div id="availablethemes">
   108 
   122 
   109 		<?php $this->tablenav( 'bottom' ); ?>
   123 		<?php $this->tablenav( 'bottom' ); ?>
   110 <?php
   124 <?php
   111 	}
   125 	}
   112 
   126 
   113 	function get_columns() {
   127 	public function get_columns() {
   114 		return array();
   128 		return array();
   115 	}
   129 	}
   116 
   130 
   117 	function display_rows_or_placeholder() {
   131 	public function display_rows_or_placeholder() {
   118 		if ( $this->has_items() ) {
   132 		if ( $this->has_items() ) {
   119 			$this->display_rows();
   133 			$this->display_rows();
   120 		} else {
   134 		} else {
   121 			echo '<div class="no-items">';
   135 			echo '<div class="no-items">';
   122 			$this->no_items();
   136 			$this->no_items();
   123 			echo '</div>';
   137 			echo '</div>';
   124 		}
   138 		}
   125 	}
   139 	}
   126 
   140 
   127 	function display_rows() {
   141 	public function display_rows() {
   128 		$themes = $this->items;
   142 		$themes = $this->items;
   129 
   143 
   130 		foreach ( $themes as $theme ):
   144 		foreach ( $themes as $theme ):
   131 			?><div class="available-theme"><?php
   145 			?><div class="available-theme"><?php
   132 
   146 
   147 				. esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
   161 				. esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
   148 
   162 
   149 			$actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
   163 			$actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
   150 				. esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
   164 				. esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
   151 
   165 
   152 			if ( current_user_can( 'edit_theme_options' ) )
   166 			if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
   153 				$actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
   167 				$actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
   154 					. __( 'Live Preview' ) . '</a>';
   168 					. __( 'Live Preview' ) . '</a>';
       
   169 			}
   155 
   170 
   156 			if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
   171 			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 )
   172 				$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 ) )
   173 					. '" 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>';
   174 					. "' );" . '">' . __( 'Delete' ) . '</a>';
   160 
   175 
       
   176 			/** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
   161 			$actions       = apply_filters( 'theme_action_links', $actions, $theme );
   177 			$actions       = apply_filters( 'theme_action_links', $actions, $theme );
       
   178 
       
   179 			/** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
   162 			$actions       = apply_filters( "theme_action_links_$stylesheet", $actions, $theme );
   180 			$actions       = apply_filters( "theme_action_links_$stylesheet", $actions, $theme );
   163 			$delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
   181 			$delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
   164 			unset( $actions['delete'] );
   182 			unset( $actions['delete'] );
   165 
   183 
   166 			?>
   184 			?>
   189 
   207 
   190 				<?php theme_update_available( $theme ); ?>
   208 				<?php theme_update_available( $theme ); ?>
   191 			</div>
   209 			</div>
   192 
   210 
   193 			<div class="themedetaildiv hide-if-js">
   211 			<div class="themedetaildiv hide-if-js">
   194 				<p><strong><?php _e('Version: '); ?></strong><?php echo $version; ?></p>
   212 				<p><strong><?php _e('Version:'); ?></strong> <?php echo $version; ?></p>
   195 				<p><?php echo $theme->display('Description'); ?></p>
   213 				<p><?php echo $theme->display('Description'); ?></p>
   196 				<?php if ( $theme->parent() ) {
   214 				<?php if ( $theme->parent() ) {
   197 					printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
   215 					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' ),
   216 						__( 'https://codex.wordpress.org/Child_Themes' ),
   199 						$theme->parent()->display( 'Name' ) );
   217 						$theme->parent()->display( 'Name' ) );
   200 				} ?>
   218 				} ?>
   201 			</div>
   219 			</div>
   202 
   220 
   203 			</div>
   221 			</div>
   204 		<?php
   222 		<?php
   205 		endforeach;
   223 		endforeach;
   206 	}
   224 	}
   207 
   225 
   208 	function search_theme( $theme ) {
   226 	/**
       
   227 	 * @param WP_Theme $theme
       
   228 	 * @return bool
       
   229 	 */
       
   230 	public function search_theme( $theme ) {
   209 		// Search the features
   231 		// Search the features
   210 		foreach ( $this->features as $word ) {
   232 		foreach ( $this->features as $word ) {
   211 			if ( ! in_array( $word, $theme->get('Tags') ) )
   233 			if ( ! in_array( $word, $theme->get('Tags') ) )
   212 				return false;
   234 				return false;
   213 		}
   235 		}
   217 			if ( in_array( $word, $theme->get('Tags') ) )
   239 			if ( in_array( $word, $theme->get('Tags') ) )
   218 				continue;
   240 				continue;
   219 
   241 
   220 			foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
   242 			foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
   221 				// Don't mark up; Do translate.
   243 				// Don't mark up; Do translate.
   222 				if ( false !== stripos( $theme->display( $header, false, true ), $word ) )
   244 				if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
   223 					continue 2;
   245 					continue 2;
       
   246 				}
   224 			}
   247 			}
   225 
   248 
   226 			if ( false !== stripos( $theme->get_stylesheet(), $word ) )
   249 			if ( false !== stripos( $theme->get_stylesheet(), $word ) )
   227 				continue;
   250 				continue;
   228 
   251 
   236 	}
   259 	}
   237 
   260 
   238 	/**
   261 	/**
   239 	 * Send required variables to JavaScript land
   262 	 * Send required variables to JavaScript land
   240 	 *
   263 	 *
   241 	 * @since 3.4
   264 	 * @since 3.4.0
   242 	 * @access private
   265 	 * @access public
   243 	 *
   266 	 *
   244 	 * @uses $this->features Array of all feature search terms.
   267 	 * @param array $extra_args
   245 	 * @uses get_pagenum()
   268 	 */
   246 	 * @uses _pagination_args['total_pages']
   269 	public function _js_vars( $extra_args = array() ) {
   247 	 */
       
   248 	 function _js_vars( $extra_args = array() ) {
       
   249 		$search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
   270 		$search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
   250 
   271 
   251 		$args = array(
   272 		$args = array(
   252 			'search' => $search_string,
   273 			'search' => $search_string,
   253 			'features' => $this->features,
   274 			'features' => $this->features,
   256 		);
   277 		);
   257 
   278 
   258 		if ( is_array( $extra_args ) )
   279 		if ( is_array( $extra_args ) )
   259 			$args = array_merge( $args, $extra_args );
   280 			$args = array_merge( $args, $extra_args );
   260 
   281 
   261 		printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", json_encode( $args ) );
   282 		printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
   262 		parent::_js_vars();
   283 		parent::_js_vars();
   263 	}
   284 	}
   264 }
   285 }