class-gsc-table.php 8.99 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
<?php
/**
 * @package WPSEO\Admin|Google_Search_Console
 */

if ( ! class_exists( 'WP_List_Table' ) ) {
	require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

/**
 * Class WPSEO_GSC_Table
 */
class WPSEO_GSC_Table extends WP_List_Table {

	const FREE_MODAL_HEIGHT = 140;

	/**
	 * @var string
	 */
	private $search_string;

	/**
	 * @var array
	 */
	protected $_column_headers;

	/**
	 * The category that is displayed
	 *
	 * @var mixed|string
	 */
	private $current_view;

	/**
	 * @var integer
	 */
	private $per_page = 50;

	/**
	 * @var integer
	 */
	private $current_page = 1;

	/**
	 * Search Console table class constructor (subclasses list table).
	 *
	 * @param string $platform Platform (desktop, mobile, feature phone).
	 * @param string $category Type of the issues.
	 * @param array  $items    Set of the issues to display.
	 */
	public function __construct( $platform, $category, array $items ) {
		parent::__construct();

		// Adding the thickbox.
		add_thickbox();

		// Set search string.
		$search_string = filter_input( INPUT_GET, 's' );

		if ( $search_string !== '' ) {
			$this->search_string = $search_string;
		}

		$this->current_view = $category;

		// Set the crawl issue source.
		$this->show_fields( $platform );

		$this->items = $items;
	}

	/**
	 * Getting the screen id from this table
	 *
	 * @return string
	 */
	public function get_screen_id() {
		return $this->screen->id;
	}

	/**
	 * Setup the table variables, fetch the items from the database, search, sort and format the items.
	 */
	public function prepare_items() {
		// Get variables needed for pagination.
		$this->per_page     = $this->get_items_per_page( 'errors_per_page', $this->per_page );
		$this->current_page = intval( ( $paged = filter_input( INPUT_GET, 'paged' ) ) ? $paged : 1 );

		$this->setup_columns();
		$this->views();
		$this->parse_items();
	}

	/**
	 * Set the table columns
	 *
	 * @return array
	 */
	public function get_columns() {
		$columns = array(
			'cb'             => '<input type="checkbox" />',
			'url'            => __( 'URL', 'wordpress-seo' ),
			'last_crawled'   => __( 'Last crawled', 'wordpress-seo' ),
			'first_detected' => __( 'First detected', 'wordpress-seo' ),
			'response_code'  => __( 'Response code', 'wordpress-seo' ),
		);

		return $columns;
	}

	/**
	 * Return the columns that are sortable
	 *
	 * @return array
	 */
	protected function get_sortable_columns() {
		$sortable_columns = array(
			'url'            => array( 'url', false ),
			'last_crawled'   => array( 'last_crawled', false ),
			'first_detected' => array( 'first_detected', false ),
			'response_code'  => array( 'response_code', false ),
		);

		return $sortable_columns;
	}

	/**
	 * Return available bulk actions
	 *
	 * @return array
	 */
	protected function get_bulk_actions() {
		return array(
			'mark_as_fixed' => __( 'Mark as fixed', 'wordpress-seo' ),
		);
	}

	/**
	 * Default method to display a column
	 *
	 * @param array  $item        Data array.
	 * @param string $column_name Column name key.
	 *
	 * @return mixed
	 */
	protected function column_default( $item, $column_name ) {
		return $item[ $column_name ];
	}

	/**
	 * Checkbox column
	 *
	 * @param array $item Item data array.
	 *
	 * @return string
	 */
	protected function column_cb( $item ) {
		return sprintf(
			'<input type="checkbox" name="wpseo_crawl_issues[]" id="cb-%1$s" value="%2$s" /><label for="cb-%1$s" class="screen-reader-text">%3$s</label>',
			md5( $item['url'] ),
			$item['url'],
			__( 'Select redirect', 'wordpress-seo' )
		);
	}

	/**
	 * Formatting the output of the column last crawled into a dateformat
	 *
	 * @param array $item Item data array.
	 *
	 * @return string
	 */
	protected function column_last_crawled( $item ) {
		return date_i18n( get_option( 'date_format' ), strtotime( $item['last_crawled'] ) );
	}

	/**
	 * Formatting the output of the column first detected into a dateformat
	 *
	 * @param array $item Item data array.
	 *
	 * @return string
	 */
	protected function column_first_detected( $item ) {
		return date_i18n( get_option( 'date_format' ), strtotime( $item['first_detected'] ) );
	}

	/**
	 * URL column
	 *
	 * @param array $item Item data array.
	 *
	 * @return string
	 */
	protected function column_url( $item ) {
		$actions = array();

		if ( $this->can_create_redirect() ) {
			/** Gets the modal box */
			$modal = $this->get_modal_box( $item['url'] );
			$modal->load_view( md5( $item['url'] ) );

			$actions['create_redirect'] = '<a href="#TB_inline?width=600&height=' . $modal->get_height() . '&inlineId=redirect-' . md5( $item['url'] ) . '" class="thickbox wpseo-open-gsc-redirect-modal aria-button-if-js">' . __( 'Create redirect', 'wordpress-seo' ) . '</a>';
		}

		$actions['view']        = '<a href="' . home_url( $item['url'] ) . '" target="_blank">' . __( 'View', 'wordpress-seo' ) . '</a>';
		$actions['markasfixed'] = '<a href="javascript:wpseo_mark_as_fixed(\'' . urlencode( $item['url'] ) . '\');">' . __( 'Mark as fixed', 'wordpress-seo' ) . '</a>';

		return sprintf(
			'<span class="value">%1$s</span> %2$s',
			$item['url'],
			$this->row_actions( $actions )
		);
	}

	/**
	 * Running the setup of the columns
	 */
	private function setup_columns() {
		$this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );
	}

	/**
	 * Check if the current category allow creating redirects
	 *
	 * @return bool
	 */
	private function can_create_redirect() {
		return in_array( $this->current_view, array( 'soft_404', 'not_found', 'access_denied' ) );
	}

	/**
	 * Setting the table navigation
	 *
	 * @param int $total_items    Total number of items.
	 * @param int $posts_per_page Number of items per page.
	 */
	private function set_pagination( $total_items, $posts_per_page ) {
		$this->set_pagination_args( array(
			'total_items' => $total_items,
			'total_pages' => ceil( ( $total_items / $posts_per_page ) ),
			'per_page'    => $posts_per_page,
		) );
	}

	/**
	 * Setting the items
	 */
	private function parse_items() {
		if ( is_array( $this->items ) && count( $this->items ) > 0 ) {
			if ( ! empty( $this->search_string ) ) {
				$this->do_search();
			}

			$this->set_pagination( count( $this->items ), $this->per_page );

			$this->sort_items();
			$this->paginate_items();
		}
	}

	/**
	 * Search through the items
	 */
	private function do_search() {
		$results = array();

		foreach ( $this->items as $item ) {
			foreach ( $item as $value ) {
				if ( stristr( $value, $this->search_string ) !== false ) {
					$results[] = $item;
					continue;
				}
			}
		}

		$this->items = $results;
	}

	/**
	 * Running the pagination
	 */
	private function paginate_items() {
		// Setting the starting point. If starting point is below 1, overwrite it with value 0, otherwise it will be sliced of at the back.
		$slice_start = ( $this->current_page - 1 );
		if ( $slice_start < 0 ) {
			$slice_start = 0;
		}

		// Apply 'pagination'.
		$this->items = array_slice( $this->items, ( $slice_start * $this->per_page ), $this->per_page );
	}

	/**
	 * Sort the items by callback
	 */
	private function sort_items() {
		// Sort the results.
		usort( $this->items, array( $this, 'do_reorder' ) );
	}

	/**
	 * Doing the sorting of the issues
	 *
	 * @param array $a First data set for comparison.
	 * @param array $b Second data set for comparison.
	 *
	 * @return int
	 */
	private function do_reorder( $a, $b ) {
		// If no sort, default to title.
		$orderby = ( $orderby = filter_input( INPUT_GET, 'orderby' ) ) ? $orderby : 'url';

		// If no order, default to asc.
		$order = ( $order = filter_input( INPUT_GET, 'order' ) ) ? $order : 'asc';

		// When there is a raw field of it, sort by this field.
		if ( array_key_exists( $orderby . '_raw', $a ) && array_key_exists( $orderby . '_raw', $b ) ) {
			$orderby = $orderby . '_raw';
		}

		// Determine sort order.
		$result = strcmp( $a[ $orderby ], $b[ $orderby ] );

		// Send final sort direction to usort.
		return ( $order === 'asc' ) ? $result : ( - $result );
	}

	/**
	 * Checks if premium is loaded, if not the nopremium modal will be shown. Otherwise it will load the premium one.
	 *
	 * @param string $url URL string.
	 *
	 * @return WPSEO_GSC_Modal Instance of the GSC modal.
	 */
	private function get_modal_box( $url ) {
		if ( defined( 'WPSEO_PREMIUM_FILE' ) && class_exists( 'WPSEO_Premium_GSC_Modal' ) ) {
			static $premium_modal;

			if ( ! $premium_modal ) {
				$premium_modal = new WPSEO_Premium_GSC_Modal();
			}

			return $premium_modal->show( $url );
		}

		return new WPSEO_GSC_Modal(
			dirname( __FILE__ ) . '/views/gsc-redirect-nopremium.php',
			self::FREE_MODAL_HEIGHT,
			array( 'url' => $url )
		);
	}

	/**
	 * Showing the hidden fields used by the AJAX requests
	 *
	 * @param string $platform Platform (desktop, mobile, feature phone).
	 */
	private function show_fields( $platform ) {
		echo "<input type='hidden' name='wpseo_gsc_nonce' value='" . wp_create_nonce( 'wpseo_gsc_nonce' ) . "' />";
		echo "<input id='field_platform' type='hidden' name='platform' value='{$platform}' />";
		echo "<input id='field_category' type='hidden' name='category' value='{$this->current_view}' />";
	}
}