| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
/** |
| 6 |
* List Table component class. |
| 7 |
* |
| 8 |
* @since 1.1.0 |
| 9 |
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
| 10 |
*/ |
| 11 |
class Sellkit_Admin_List_Table { |
| 12 |
|
| 13 |
/** |
| 14 |
* Class instance. |
| 15 |
* |
| 16 |
* @since 1.1.0 |
| 17 |
* @var Sellkit_Admin_List_Table |
| 18 |
*/ |
| 19 |
private static $instance = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get a class instance. |
| 23 |
* |
| 24 |
* @since 1.1.0 |
| 25 |
* |
| 26 |
* @return Sellkit_Admin_List_Table Class instance. |
| 27 |
*/ |
| 28 |
public static function get_instance() { |
| 29 |
if ( null === self::$instance ) { |
| 30 |
self::$instance = new self(); |
| 31 |
} |
| 32 |
|
| 33 |
return self::$instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Class constructor. |
| 38 |
* |
| 39 |
* @since 1.1.0 |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
add_action( 'wp_ajax_sellkit_list_table_posts', [ $this, 'get_posts' ] ); |
| 43 |
add_action( 'wp_ajax_sellkit_list_table_post', [ $this, 'handle_post' ] ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get posts. |
| 48 |
* |
| 49 |
* @since 1.1.0 |
| 50 |
*/ |
| 51 |
public function get_posts() { |
| 52 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 53 |
|
| 54 |
// Sanitize. |
| 55 |
$post_type = sellkit_htmlspecialchars( INPUT_POST, 'post_type' ); |
| 56 |
$paged = filter_input( INPUT_POST, 'paged', FILTER_SANITIZE_NUMBER_INT ); |
| 57 |
$posts_per_page = filter_input( INPUT_POST, 'posts_per_page', FILTER_SANITIZE_NUMBER_INT ); |
| 58 |
$search_args = filter_input( INPUT_POST, 'args', FILTER_DEFAULT, FILTER_FORCE_ARRAY ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Filter List Table query arguments. |
| 62 |
* |
| 63 |
* @since 1.1.0 |
| 64 |
* |
| 65 |
* @param array $args The query arguments. |
| 66 |
*/ |
| 67 |
$args = apply_filters( "sellkit_list_table_{$post_type}_args", [ |
| 68 |
'post_type' => $post_type, |
| 69 |
'paged' => $paged, |
| 70 |
'posts_per_page' => ! empty( $posts_per_page ) ? $posts_per_page : 20, |
| 71 |
'orderby' => 'ID', |
| 72 |
'order' => 'DESC', |
| 73 |
] ); |
| 74 |
|
| 75 |
if ( is_array( $search_args ) && ! empty( $search_args ) ) { |
| 76 |
foreach ( $search_args as $search_arg ) { |
| 77 |
$args[ $search_arg['param'] ] = $search_arg['value']; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Query. |
| 82 |
$query = new \WP_Query( $args ); |
| 83 |
|
| 84 |
/** |
| 85 |
* Filter List Table query posts. |
| 86 |
* |
| 87 |
* @since 1.1.0 |
| 88 |
* |
| 89 |
* @param array $args The taxonomy arguments. |
| 90 |
*/ |
| 91 |
$posts = apply_filters( "sellkit_list_table_{$post_type}_posts", $query->posts ); |
| 92 |
|
| 93 |
/** |
| 94 |
* Filter List Table columns. |
| 95 |
* |
| 96 |
* @since 1.1.0 |
| 97 |
* |
| 98 |
* @param array $args The columns headings and values. |
| 99 |
*/ |
| 100 |
$columns = apply_filters( "sellkit_list_table_{$post_type}_columns", [ |
| 101 |
'labels' => [], |
| 102 |
'values' => [], |
| 103 |
], $posts ); |
| 104 |
|
| 105 |
// Send response. |
| 106 |
wp_send_json_success( [ |
| 107 |
'posts' => $posts, |
| 108 |
'max_num_pages' => $query->max_num_pages, |
| 109 |
'columns' => $columns, |
| 110 |
] ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Handle post actions. |
| 115 |
* |
| 116 |
* @since 1.1.0 |
| 117 |
*/ |
| 118 |
public function handle_post() { |
| 119 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 120 |
|
| 121 |
// Sanitize. |
| 122 |
$post = filter_input( INPUT_POST, 'post', FILTER_DEFAULT, FILTER_FORCE_ARRAY ); |
| 123 |
$sub_action = sellkit_htmlspecialchars( INPUT_POST, 'sub_action' ); |
| 124 |
|
| 125 |
call_user_func( [ $this, "handle_post_{$sub_action}" ], $post ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Handle post toggle status. |
| 130 |
* |
| 131 |
* @since 1.1.0 |
| 132 |
* @param array $post Post Array. |
| 133 |
*/ |
| 134 |
private function handle_post_toggle_status( $post ) { |
| 135 |
// Format proper post status. |
| 136 |
$post_status = ( 'publish' === $post['post_status'] ) ? 'draft' : 'publish'; |
| 137 |
|
| 138 |
// Update post status. |
| 139 |
$result = wp_update_post( [ |
| 140 |
'ID' => $post['ID'], |
| 141 |
'post_status' => $post_status, |
| 142 |
], true ); |
| 143 |
|
| 144 |
do_action( "sellkit_after_toggle_status_{$post['post_type']}", $post['ID'], $post_status ); |
| 145 |
|
| 146 |
// Handle error. |
| 147 |
if ( is_wp_error( $result ) ) { |
| 148 |
wp_send_json_error( $result ); |
| 149 |
} |
| 150 |
|
| 151 |
// Send success response. |
| 152 |
wp_send_json_success( $result ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Duplicate post. |
| 157 |
* |
| 158 |
* @since 1.1.0 |
| 159 |
* @param array $post Post Array. |
| 160 |
*/ |
| 161 |
private function handle_post_duplicate_post( $post ) { |
| 162 |
$main_post_id = $post['ID']; |
| 163 |
|
| 164 |
unset( $post['ID'] ); |
| 165 |
|
| 166 |
$post['post_title'] = sprintf( '%1s - %2s', esc_html( $post['post_title'] ), esc_html__( 'Clone', 'sellkit' ) ); |
| 167 |
$post['post_status'] = 'draft'; |
| 168 |
|
| 169 |
$post_id = wp_insert_post( $post ); |
| 170 |
|
| 171 |
$main_post_meta = get_post_custom( $main_post_id ); |
| 172 |
|
| 173 |
foreach ( $main_post_meta as $key => $value ) { |
| 174 |
update_post_meta( $post_id, $key, maybe_unserialize( $value[0] ) ); |
| 175 |
} |
| 176 |
|
| 177 |
// Handle error. |
| 178 |
if ( is_wp_error( $post_id ) ) { |
| 179 |
wp_send_json_error( $post_id ); |
| 180 |
} |
| 181 |
|
| 182 |
// Send success response. |
| 183 |
wp_send_json_success( $post_id ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Handle post remove. |
| 188 |
* |
| 189 |
* @since 1.1.0 |
| 190 |
* @param array $post Post array. |
| 191 |
*/ |
| 192 |
private function handle_post_remove( $post ) { |
| 193 |
// Delete post. |
| 194 |
$result = wp_delete_post( $post['ID'] ); |
| 195 |
|
| 196 |
// Handle error. |
| 197 |
if ( ! is_object( $result ) ) { |
| 198 |
wp_send_json_error( $result ); |
| 199 |
} |
| 200 |
|
| 201 |
// Send success response. |
| 202 |
wp_send_json_success( $result ); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
Sellkit_Admin_List_Table::get_instance(); |
| 207 |
|