balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-themes-list-table.php (10189B)


      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(
     34 			array(
     35 				'ajax'   => true,
     36 				'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
     37 			)
     38 		);
     39 	}
     40 
     41 	/**
     42 	 * @return bool
     43 	 */
     44 	public function ajax_user_can() {
     45 		// Do not check edit_theme_options here. Ajax calls for available themes require switch_themes.
     46 		return current_user_can( 'switch_themes' );
     47 	}
     48 
     49 	/**
     50 	 */
     51 	public function prepare_items() {
     52 		$themes = wp_get_themes( array( 'allowed' => true ) );
     53 
     54 		if ( ! empty( $_REQUEST['s'] ) ) {
     55 			$this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
     56 		}
     57 
     58 		if ( ! empty( $_REQUEST['features'] ) ) {
     59 			$this->features = $_REQUEST['features'];
     60 		}
     61 
     62 		if ( $this->search_terms || $this->features ) {
     63 			foreach ( $themes as $key => $theme ) {
     64 				if ( ! $this->search_theme( $theme ) ) {
     65 					unset( $themes[ $key ] );
     66 				}
     67 			}
     68 		}
     69 
     70 		unset( $themes[ get_option( 'stylesheet' ) ] );
     71 		WP_Theme::sort_by_name( $themes );
     72 
     73 		$per_page = 36;
     74 		$page     = $this->get_pagenum();
     75 
     76 		$start = ( $page - 1 ) * $per_page;
     77 
     78 		$this->items = array_slice( $themes, $start, $per_page, true );
     79 
     80 		$this->set_pagination_args(
     81 			array(
     82 				'total_items'     => count( $themes ),
     83 				'per_page'        => $per_page,
     84 				'infinite_scroll' => true,
     85 			)
     86 		);
     87 	}
     88 
     89 	/**
     90 	 */
     91 	public function no_items() {
     92 		if ( $this->search_terms || $this->features ) {
     93 			_e( 'No items found.' );
     94 			return;
     95 		}
     96 
     97 		$blog_id = get_current_blog_id();
     98 		if ( is_multisite() ) {
     99 			if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
    100 				printf(
    101 					/* translators: 1: URL to Themes tab on Edit Site screen, 2: URL to Add Themes screen. */
    102 					__( '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.' ),
    103 					network_admin_url( 'site-themes.php?id=' . $blog_id ),
    104 					network_admin_url( 'theme-install.php' )
    105 				);
    106 
    107 				return;
    108 			} elseif ( current_user_can( 'manage_network_themes' ) ) {
    109 				printf(
    110 					/* translators: %s: URL to Themes tab on Edit Site screen. */
    111 					__( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%s">enable</a> more themes.' ),
    112 					network_admin_url( 'site-themes.php?id=' . $blog_id )
    113 				);
    114 
    115 				return;
    116 			}
    117 			// Else, fallthrough. install_themes doesn't help if you can't enable it.
    118 		} else {
    119 			if ( current_user_can( 'install_themes' ) ) {
    120 				printf(
    121 					/* translators: %s: URL to Add Themes screen. */
    122 					__( '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.' ),
    123 					admin_url( 'theme-install.php' )
    124 				);
    125 
    126 				return;
    127 			}
    128 		}
    129 		// Fallthrough.
    130 		printf(
    131 			/* translators: %s: Network title. */
    132 			__( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ),
    133 			get_site_option( 'site_name' )
    134 		);
    135 	}
    136 
    137 	/**
    138 	 * @param string $which
    139 	 */
    140 	public function tablenav( $which = 'top' ) {
    141 		if ( $this->get_pagination_arg( 'total_pages' ) <= 1 ) {
    142 			return;
    143 		}
    144 		?>
    145 		<div class="tablenav themes <?php echo $which; ?>">
    146 			<?php $this->pagination( $which ); ?>
    147 			<span class="spinner"></span>
    148 			<br class="clear" />
    149 		</div>
    150 		<?php
    151 	}
    152 
    153 	/**
    154 	 * Displays the themes table.
    155 	 *
    156 	 * Overrides the parent display() method to provide a different container.
    157 	 *
    158 	 * @since 3.1.0
    159 	 */
    160 	public function display() {
    161 		wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
    162 		?>
    163 		<?php $this->tablenav( 'top' ); ?>
    164 
    165 		<div id="availablethemes">
    166 			<?php $this->display_rows_or_placeholder(); ?>
    167 		</div>
    168 
    169 		<?php $this->tablenav( 'bottom' ); ?>
    170 		<?php
    171 	}
    172 
    173 	/**
    174 	 * @return array
    175 	 */
    176 	public function get_columns() {
    177 		return array();
    178 	}
    179 
    180 	/**
    181 	 */
    182 	public function display_rows_or_placeholder() {
    183 		if ( $this->has_items() ) {
    184 			$this->display_rows();
    185 		} else {
    186 			echo '<div class="no-items">';
    187 			$this->no_items();
    188 			echo '</div>';
    189 		}
    190 	}
    191 
    192 	/**
    193 	 */
    194 	public function display_rows() {
    195 		$themes = $this->items;
    196 
    197 		foreach ( $themes as $theme ) :
    198 			?>
    199 			<div class="available-theme">
    200 			<?php
    201 
    202 			$template   = $theme->get_template();
    203 			$stylesheet = $theme->get_stylesheet();
    204 			$title      = $theme->display( 'Name' );
    205 			$version    = $theme->display( 'Version' );
    206 			$author     = $theme->display( 'Author' );
    207 
    208 			$activate_link = wp_nonce_url( 'themes.php?action=activate&amp;template=' . urlencode( $template ) . '&amp;stylesheet=' . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
    209 
    210 			$actions             = array();
    211 			$actions['activate'] = sprintf(
    212 				'<a href="%s" class="activatelink" title="%s">%s</a>',
    213 				$activate_link,
    214 				/* translators: %s: Theme name. */
    215 				esc_attr( sprintf( _x( 'Activate &#8220;%s&#8221;', 'theme' ), $title ) ),
    216 				__( 'Activate' )
    217 			);
    218 
    219 			if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
    220 				$actions['preview'] .= sprintf(
    221 					'<a href="%s" class="load-customize hide-if-no-customize">%s</a>',
    222 					wp_customize_url( $stylesheet ),
    223 					__( 'Live Preview' )
    224 				);
    225 			}
    226 
    227 			if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) {
    228 				$actions['delete'] = sprintf(
    229 					'<a class="submitdelete deletion" href="%s" onclick="return confirm( \'%s\' );">%s</a>',
    230 					wp_nonce_url( 'themes.php?action=delete&amp;stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ),
    231 					/* translators: %s: Theme name. */
    232 					esc_js( sprintf( __( "You are about to delete this theme '%s'\n  'Cancel' to stop, 'OK' to delete." ), $title ) ),
    233 					__( 'Delete' )
    234 				);
    235 			}
    236 
    237 			/** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
    238 			$actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' );
    239 
    240 			/** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
    241 			$actions       = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, 'all' );
    242 			$delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
    243 			unset( $actions['delete'] );
    244 
    245 			$screenshot = $theme->get_screenshot();
    246 			?>
    247 
    248 			<span class="screenshot hide-if-customize">
    249 				<?php if ( $screenshot ) : ?>
    250 					<img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
    251 				<?php endif; ?>
    252 			</span>
    253 			<a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
    254 				<?php if ( $screenshot ) : ?>
    255 					<img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
    256 				<?php endif; ?>
    257 			</a>
    258 
    259 			<h3><?php echo $title; ?></h3>
    260 			<div class="theme-author">
    261 				<?php
    262 					/* translators: %s: Theme author. */
    263 					printf( __( 'By %s' ), $author );
    264 				?>
    265 			</div>
    266 			<div class="action-links">
    267 				<ul>
    268 					<?php foreach ( $actions as $action ) : ?>
    269 						<li><?php echo $action; ?></li>
    270 					<?php endforeach; ?>
    271 					<li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li>
    272 				</ul>
    273 				<?php echo $delete_action; ?>
    274 
    275 				<?php theme_update_available( $theme ); ?>
    276 			</div>
    277 
    278 			<div class="themedetaildiv hide-if-js">
    279 				<p><strong><?php _e( 'Version:' ); ?></strong> <?php echo $version; ?></p>
    280 				<p><?php echo $theme->display( 'Description' ); ?></p>
    281 				<?php
    282 				if ( $theme->parent() ) {
    283 					printf(
    284 						/* translators: 1: Link to documentation on child themes, 2: Name of parent theme. */
    285 						' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
    286 						__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
    287 						$theme->parent()->display( 'Name' )
    288 					);
    289 				}
    290 				?>
    291 			</div>
    292 
    293 			</div>
    294 			<?php
    295 		endforeach;
    296 	}
    297 
    298 	/**
    299 	 * @param WP_Theme $theme
    300 	 * @return bool
    301 	 */
    302 	public function search_theme( $theme ) {
    303 		// Search the features.
    304 		foreach ( $this->features as $word ) {
    305 			if ( ! in_array( $word, $theme->get( 'Tags' ), true ) ) {
    306 				return false;
    307 			}
    308 		}
    309 
    310 		// Match all phrases.
    311 		foreach ( $this->search_terms as $word ) {
    312 			if ( in_array( $word, $theme->get( 'Tags' ), true ) ) {
    313 				continue;
    314 			}
    315 
    316 			foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
    317 				// Don't mark up; Do translate.
    318 				if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
    319 					continue 2;
    320 				}
    321 			}
    322 
    323 			if ( false !== stripos( $theme->get_stylesheet(), $word ) ) {
    324 				continue;
    325 			}
    326 
    327 			if ( false !== stripos( $theme->get_template(), $word ) ) {
    328 				continue;
    329 			}
    330 
    331 			return false;
    332 		}
    333 
    334 		return true;
    335 	}
    336 
    337 	/**
    338 	 * Send required variables to JavaScript land
    339 	 *
    340 	 * @since 3.4.0
    341 	 *
    342 	 * @param array $extra_args
    343 	 */
    344 	public function _js_vars( $extra_args = array() ) {
    345 		$search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
    346 
    347 		$args = array(
    348 			'search'      => $search_string,
    349 			'features'    => $this->features,
    350 			'paged'       => $this->get_pagenum(),
    351 			'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
    352 		);
    353 
    354 		if ( is_array( $extra_args ) ) {
    355 			$args = array_merge( $args, $extra_args );
    356 		}
    357 
    358 		printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
    359 		parent::_js_vars();
    360 	}
    361 }