PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/stops-core-theme-and-plugin-updates/includes/MPSUM_Themes_List_Table.php

https://gitlab.com/memuller.web/wp_site
PHP | 417 lines | 244 code | 70 blank | 103 comment | 45 complexity | 836ea383dc4026e58737eb2dbb47d7bd MD5 | raw file
  1. <?php
  2. /**
  3. * Easy Updates Manager Themes List Table class.
  4. *
  5. * @package Easy Updates Manager
  6. * @subpackage MPSUM_List_Table
  7. * @since 5.0.0
  8. * @access private
  9. */
  10. class MPSUM_Themes_List_Table extends MPSUM_List_Table {
  11. public $site_id;
  12. public $is_site_themes;
  13. private $tab = '';
  14. /**
  15. * Constructor.
  16. *
  17. * @since 3.1.0
  18. * @access public
  19. *
  20. * @see WP_List_Table::__construct() for more information on default arguments.
  21. *
  22. * @param array $args An associative array of arguments.
  23. */
  24. public function __construct( $args = array() ) {
  25. global $status, $page;
  26. parent::__construct( array(
  27. 'plural' => 'themes',
  28. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  29. ) );
  30. $this->tab = isset( $args[ 'tab' ] ) ? $args[ 'tab' ] : '';
  31. $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all';
  32. if ( !in_array( $status, array( 'all', 'update_disabled', 'update_enabled', 'automatic' ) ) )
  33. $status = 'all';
  34. $page = $this->get_pagenum();
  35. $this->is_site_themes = ( 'site-themes-network' == $this->screen->id ) ? true : false;
  36. if ( $this->is_site_themes )
  37. $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
  38. }
  39. protected function get_table_classes() {
  40. // todo: remove and add CSS for .themes
  41. return array( 'widefat', 'plugins' );
  42. }
  43. public function ajax_user_can() {
  44. if ( $this->is_site_themes )
  45. return current_user_can( 'manage_sites' );
  46. else
  47. return current_user_can( 'manage_network_themes' );
  48. }
  49. public function prepare_items() {
  50. global $totals, $status;
  51. $order = 'DESC';
  52. $page = isset( $_GET[ 'paged' ] ) ? absint( $_GET[ 'paged' ] ) : 1;
  53. $orderby = 'Name';
  54. $themes = array(
  55. /**
  56. * Filter the full array of WP_Theme objects to list in the Multisite
  57. * themes list table.
  58. *
  59. * @since 3.1.0
  60. *
  61. * @param array $all An array of WP_Theme objects to display in the list table.
  62. */
  63. 'all' => apply_filters( 'all_themes', wp_get_themes() ),
  64. 'update_enabled' => array(),
  65. 'update_disabled' => array(),
  66. 'automatic' => array()
  67. );
  68. $maybe_update = current_user_can( 'update_themes' ) && ! $this->is_site_themes && $current = get_site_transient( 'update_themes' );
  69. $theme_options = MPSUM_Updates_Manager::get_options( 'themes' );
  70. $theme_automatic_options = MPSUM_Updates_Manager::get_options( 'themes_automatic' );
  71. foreach ( (array) $themes['all'] as $theme => $theme_data ) {
  72. if ( false !== $key = array_search( $theme, $theme_options ) ) {
  73. $themes[ 'update_disabled' ][ $theme ] = $theme_data;
  74. } else {
  75. $themes[ 'update_enabled' ][ $theme ] = $theme_data;
  76. if ( in_array( $theme, $theme_automatic_options ) ) {
  77. $themes[ 'automatic' ][ $theme ] = $theme_data;
  78. }
  79. }
  80. }
  81. $totals = array();
  82. foreach ( $themes as $type => $list )
  83. $totals[ $type ] = count( $list );
  84. //Disable the automatic updates view
  85. $core_options = MPSUM_Updates_Manager::get_options( 'core' );
  86. if ( isset( $core_options[ 'automatic_theme_updates' ] ) && 'individual' !== $core_options[ 'automatic_theme_updates' ] ) {
  87. unset( $totals[ 'automatic' ] );
  88. $themes[ 'automatic' ] = array();
  89. }
  90. if ( empty( $themes[ $status ] ) )
  91. $status = 'all';
  92. $this->items = $themes[ $status ];
  93. WP_Theme::sort_by_name( $this->items );
  94. $this->has_items = ! empty( $themes['all'] );
  95. $total_this_page = $totals[ $status ];
  96. if ( $orderby ) {
  97. $orderby = ucfirst( $orderby );
  98. $order = strtoupper( $order );
  99. if ( $orderby == 'Name' ) {
  100. if ( 'ASC' == $order )
  101. $this->items = array_reverse( $this->items );
  102. } else {
  103. uasort( $this->items, array( $this, '_order_callback' ) );
  104. }
  105. }
  106. $total_this_page = count( $themes[ 'all' ] );
  107. // Get themes per page
  108. $user_id = get_current_user_id();
  109. $themes_per_page = get_user_meta( $user_id, 'mpsum_items_per_page', true );
  110. if ( ! is_numeric( $themes_per_page ) ) {
  111. $themes_per_page = 100;
  112. }
  113. $start = ( $page - 1 ) * $themes_per_page;
  114. if ( $total_this_page > $themes_per_page )
  115. $this->items = array_slice( $this->items, $start, $themes_per_page, true );
  116. $this->set_pagination_args( array(
  117. 'total_items' => $total_this_page,
  118. 'per_page' => $themes_per_page,
  119. ) );
  120. }
  121. /**
  122. * @staticvar string $term
  123. * @param WP_Theme $theme
  124. * @return bool
  125. */
  126. public function _search_callback( $theme ) {
  127. static $term;
  128. if ( is_null( $term ) )
  129. $term = wp_unslash( $_REQUEST['s'] );
  130. foreach ( array( 'Name', 'Description', 'Author', 'Author', 'AuthorURI' ) as $field ) {
  131. // Don't mark up; Do translate.
  132. if ( false !== stripos( $theme->display( $field, false, true ), $term ) )
  133. return true;
  134. }
  135. if ( false !== stripos( $theme->get_stylesheet(), $term ) )
  136. return true;
  137. if ( false !== stripos( $theme->get_template(), $term ) )
  138. return true;
  139. return false;
  140. }
  141. // Not used by any core columns.
  142. /**
  143. * @global string $orderby
  144. * @global string $order
  145. * @param array $theme_a
  146. * @param array $theme_b
  147. * @return int
  148. */
  149. public function _order_callback( $theme_a, $theme_b ) {
  150. global $orderby, $order;
  151. $a = $theme_a[ $orderby ];
  152. $b = $theme_b[ $orderby ];
  153. if ( $a == $b )
  154. return 0;
  155. if ( 'DESC' == $order )
  156. return ( $a < $b ) ? 1 : -1;
  157. else
  158. return ( $a < $b ) ? -1 : 1;
  159. }
  160. public function no_items() {
  161. if ( ! $this->has_items )
  162. _e( 'No themes found.', 'stops-core-theme-and-plugin-updates' );
  163. else
  164. _e( 'You do not appear to have any themes available at this time.', 'stops-core-theme-and-plugin-updates' );
  165. }
  166. public function get_columns() {
  167. global $status;
  168. return array(
  169. 'cb' => '<input type="checkbox" />',
  170. 'name' => __( 'Theme', 'stops-core-theme-and-plugin-updates' ),
  171. 'description' => __( 'Description', 'stops-core-theme-and-plugin-updates' ),
  172. );
  173. }
  174. protected function get_sortable_columns() {
  175. return array();
  176. }
  177. protected function get_views() {
  178. global $totals, $status;
  179. $status_links = array();
  180. foreach ( $totals as $type => $count ) {
  181. if ( !$count )
  182. continue;
  183. switch ( $type ) {
  184. case 'all':
  185. $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'themes' );
  186. break;
  187. case 'update_disabled':
  188. $text = _n( 'Updates Disabled <span class="count">(%s)</span>', 'Updates Disabled <span class="count">(%s)</span>', $count, 'stops-core-theme-and-plugin-updates' );
  189. break;
  190. case 'update_enabled':
  191. $text = _n( 'Updates Enabled <span class="count">(%s)</span>', 'Updates Enabled <span class="count">(%s)</span>', $count, 'stops-core-theme-and-plugin-updates' );
  192. break;
  193. case 'automatic':
  194. $text = _n( 'Automatic Updates <span class="count">(%s)</span>', 'Automatic Updates <span class="count">(%s)</span>', $count, 'stops-core-theme-and-plugin-updates' );
  195. break;
  196. }
  197. if ( 'search' != $type ) {
  198. $theme_url = MPSUM_Admin::get_url();
  199. $query_args = array(
  200. 'tab' => $this->tab,
  201. 'theme_status' => $type
  202. );
  203. $status_links[$type] = sprintf( "<a href='%s' %s>%s</a>",
  204. add_query_arg( $query_args, $theme_url ),
  205. ( $type == $status ) ? ' class="current"' : '',
  206. sprintf( $text, number_format_i18n( $count ) )
  207. );
  208. }
  209. }
  210. return $status_links;
  211. }
  212. protected function get_bulk_actions() {
  213. global $status;
  214. $actions = array();
  215. $actions[ 'allow-update-selected' ] = esc_html__( 'Allow Updates', 'stops-core-theme-and-plugin-updates' );
  216. $actions[ 'disallow-update-selected' ] = esc_html__( 'Disallow Updates', 'stops-core-theme-and-plugin-updates' );
  217. $core_options = MPSUM_Updates_Manager::get_options( 'core' );
  218. if ( isset( $core_options[ 'automatic_theme_updates' ] ) && 'individual' == $core_options[ 'automatic_theme_updates' ] ) {
  219. $actions[ 'allow-automatic-selected' ] = esc_html__( 'Allow Automatic Updates', 'stops-core-theme-and-plugin-updates' );
  220. $actions[ 'disallow-automatic-selected' ] = esc_html__( 'Disallow Automatic Updates', 'stops-core-theme-and-plugin-updates' );
  221. }
  222. return $actions;
  223. }
  224. public function display_rows() {
  225. foreach ( $this->items as $theme )
  226. $this->single_row( $theme );
  227. }
  228. /**
  229. * @global string $status
  230. * @global int $page
  231. * @global string $s
  232. * @global array $totals
  233. * @param WP_Theme $theme
  234. */
  235. public function single_row( $theme ) {
  236. $status = 'all';
  237. $stylesheet = $theme->get_stylesheet();
  238. remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row', 10, 2 );
  239. $theme_key = urlencode( $stylesheet );
  240. /**
  241. * Filter the action links that show up under each theme row.
  242. *
  243. * @since 5.0.0
  244. *
  245. * @param array Array of action links
  246. * @param WP_Theme $theme WP_Theme object
  247. * @param string $status Status of the theme.
  248. */
  249. $actions = apply_filters( 'mpsum_theme_action_links', array(), $theme, 'all' );
  250. $checkbox_id = "checkbox_" . md5( $theme->get('Name') );
  251. $checkbox = "<input type='checkbox' name='checked[]' value='" . esc_attr( $stylesheet ) . "' id='" . $checkbox_id . "' /><label class='screen-reader-text' for='" . $checkbox_id . "' >" . __('Select', 'stops-core-theme-and-plugin-updates' ) . " " . $theme->display('Name') . "</label>";
  252. $id = sanitize_html_class( $theme->get_stylesheet() );
  253. $class = 'active';
  254. $theme_options = MPSUM_Updates_Manager::get_options( 'themes' );
  255. if ( false !== $key = array_search( $stylesheet, $theme_options ) ) {
  256. $class = 'inactive';
  257. }
  258. echo "<tr id='$id' class='$class'>";
  259. list( $columns, $hidden ) = $this->get_column_info();
  260. foreach ( $columns as $column_name => $column_display_name ) {
  261. $style = '';
  262. if ( in_array( $column_name, $hidden ) )
  263. $style = ' style="display:none;"';
  264. switch ( $column_name ) {
  265. case 'cb':
  266. echo "<th scope='row' class='check-column'>$checkbox</th>";
  267. break;
  268. case 'name':
  269. echo "<td class='theme-title'$style><strong>" . $theme->display('Name') . "</strong>";
  270. echo $this->row_actions( $actions, true );
  271. echo "</td>";
  272. break;
  273. case 'description':
  274. echo "<td class='column-description desc'$style>";
  275. if ( $theme->errors() ) {
  276. $pre = $status == 'broken' ? __( 'Broken Theme:', 'stops-core-theme-and-plugin-updates' ) . ' ' : '';
  277. echo '<p><strong class="attention">' . $pre . $theme->errors()->get_error_message() . '</strong></p>';
  278. }
  279. echo "<div class='theme-description'><p>" . $theme->display( 'Description' ) . "</p></div>
  280. <div class='second theme-version-author-uri'>";
  281. $theme_meta = array();
  282. if ( $theme->get('Version') )
  283. $theme_meta[] = sprintf( __( 'Version %s', 'stops-core-theme-and-plugin-updates' ), $theme->display('Version') );
  284. $theme_meta[] = sprintf( __( 'By %s', 'stops-core-theme-and-plugin-updates' ), $theme->display('Author') );
  285. if ( $theme->get('ThemeURI') )
  286. $theme_meta[] = '<a href="' . $theme->display('ThemeURI') . '" title="' . esc_attr__( 'Visit theme homepage', 'stops-core-theme-and-plugin-updates' ) . '">' . __( 'Visit Theme Site', 'stops-core-theme-and-plugin-updates' ) . '</a>';
  287. /**
  288. * Filter the array of row meta for each theme in the Multisite themes
  289. * list table.
  290. *
  291. * @since 3.1.0
  292. *
  293. * @param array $theme_meta An array of the theme's metadata,
  294. * including the version, author, and
  295. * theme URI.
  296. * @param string $stylesheet Directory name of the theme.
  297. * @param WP_Theme $theme WP_Theme object.
  298. * @param string $status Status of the theme.
  299. */
  300. $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $stylesheet, $theme, $status );
  301. echo implode( ' | ', $theme_meta );
  302. echo "</div></td>";
  303. break;
  304. default:
  305. echo "<td class='$column_name column-$column_name'$style>";
  306. /**
  307. * Fires inside each custom column of the Multisite themes list table.
  308. *
  309. * @since 3.1.0
  310. *
  311. * @param string $column_name Name of the column.
  312. * @param string $stylesheet Directory name of the theme.
  313. * @param WP_Theme $theme Current WP_Theme object.
  314. */
  315. do_action( 'manage_themes_custom_column', $column_name, $stylesheet, $theme );
  316. echo "</td>";
  317. }
  318. }
  319. echo "</tr>";
  320. if ( $this->is_site_themes )
  321. remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row' );
  322. /**
  323. * Fires after each row in the Multisite themes list table.
  324. *
  325. * @since 3.1.0
  326. *
  327. * @param string $stylesheet Directory name of the theme.
  328. * @param WP_Theme $theme Current WP_Theme object.
  329. * @param string $status Status of the theme.
  330. */
  331. do_action( 'after_theme_row', $stylesheet, $theme, $status );
  332. /**
  333. * Fires after each specific row in the Multisite themes list table.
  334. *
  335. * The dynamic portion of the hook name, `$stylesheet`, refers to the
  336. * directory name of the theme, most often synonymous with the template
  337. * name of the theme.
  338. *
  339. * @since 3.5.0
  340. *
  341. * @param string $stylesheet Directory name of the theme.
  342. * @param WP_Theme $theme Current WP_Theme object.
  343. * @param string $status Status of the theme.
  344. */
  345. do_action( "after_theme_row_$stylesheet", $stylesheet, $theme, $status );
  346. }
  347. }