Admin
2 years ago
Api
2 years ago
Blocks
2 years ago
Hooks
2 years ago
AjaxController.php
2 years ago
BlocksController.php
2 years ago
ElementorController.php
2 years ago
GutenBergController.php
2 years ago
MyAccountController.php
2 years ago
PageTemplateController.php
2 years ago
ScriptController.php
2 years ago
ShortcodeController.php
2 years ago
WidgetController.php
2 years ago
MyAccountController.php
391 lines
| 1 | <?php |
| 2 | /** |
| 3 | * MyAccountController Controller class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Controllers; |
| 9 | |
| 10 | // Do not allow directly accessing this file. |
| 11 | use RT\ThePostGrid\Helpers\Fns; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit( 'This script cannot be accessed directly.' ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * MyAccountController class |
| 19 | */ |
| 20 | class MyAccountController { |
| 21 | |
| 22 | public static $endpoint = 'my-account'; |
| 23 | |
| 24 | private static $current_user = ''; |
| 25 | |
| 26 | /** |
| 27 | * PageTemplateController constructor |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | add_shortcode( 'tpg_my_account', [ $this, 'shortcode' ] ); |
| 31 | add_action( 'init', [ __CLASS__, 'add_endpoint' ] ); |
| 32 | add_action( 'tpg_account_navigation', [ __CLASS__, 'navigation' ] ); |
| 33 | add_action( 'tpg_account_content', [ __CLASS__, 'account_content' ] ); |
| 34 | add_action( 'admin_post_tpg_post_save', [ $this, 'tpg_save_if_submitted' ] ); |
| 35 | add_action( 'admin_post_tpg_post_update', [ $this, 'tpg_save_if_submitted' ] ); |
| 36 | add_action( 'wp_ajax_tpg_tag_search', [ $this, 'tpg_tag_search' ] ); |
| 37 | add_action( 'wp_ajax_nopriv_tpg_tag_search', [ $this, 'tpg_tag_search' ] ); |
| 38 | add_action( 'wp_ajax_tpg_delete_post', [ $this, 'delete_post' ] ); |
| 39 | add_action( 'wp_ajax_nopriv_tpg_delete_post', [ $this, 'delete_post' ] ); |
| 40 | } |
| 41 | |
| 42 | public function shortcode( $atts ) { |
| 43 | if ( is_admin() ) { |
| 44 | return ''; |
| 45 | } |
| 46 | |
| 47 | $settings = get_option( rtTPG()->options['settings'] ); |
| 48 | $max_upload_size = ! empty( $settings['max_upload_file'] ) ? $settings['max_upload_file'] : '1048576'; |
| 49 | wp_enqueue_style( 'rt-select2' ); |
| 50 | wp_enqueue_script( 'rt-tpg-myaccount' ); |
| 51 | |
| 52 | wp_localize_script( |
| 53 | 'rt-tpg-myaccount', |
| 54 | 'rtTpg', |
| 55 | [ |
| 56 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 57 | 'confirm_text' => __( 'Are you sure?', 'the-post-grid' ), |
| 58 | 'uid' => get_current_user_id(), |
| 59 | 'nonceID' => esc_attr( rtTPG()->nonceId() ), |
| 60 | 'nonce' => esc_attr( wp_create_nonce( rtTPG()->nonceText() ) ), |
| 61 | 'max_upload_size' => $max_upload_size, |
| 62 | 'file_exceeds_text' => sprintf( esc_html__( 'File size exceeds %s bytes limit. Please choose a smaller file.', 'the-post-grid' ), $max_upload_size ), |
| 63 | ] |
| 64 | ); |
| 65 | wp_enqueue_media(); |
| 66 | self::$current_user = get_user_by( 'id', get_current_user_id() ); |
| 67 | |
| 68 | $output = "<div class='tpgMyAccount'>"; |
| 69 | if ( ! is_user_logged_in() ) { |
| 70 | $output .= wp_login_form( [ 'echo' => false ] ); |
| 71 | } else { |
| 72 | $data = [ |
| 73 | 'layout' => 'dashboard', |
| 74 | 'post_id' => get_the_ID(), |
| 75 | 'user_id' => get_current_user_id(), |
| 76 | ]; |
| 77 | |
| 78 | ob_start(); |
| 79 | Fns::tpg_template( $data, 'my-account' ); |
| 80 | $output .= ob_get_clean(); |
| 81 | } |
| 82 | $output .= '</div>'; |
| 83 | |
| 84 | return $output; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * End point |
| 89 | * |
| 90 | * @return void |
| 91 | */ |
| 92 | public static function add_endpoint() { |
| 93 | $end_points = Fns::get_endpoint(); |
| 94 | foreach ( $end_points as $end_point ) { |
| 95 | add_rewrite_endpoint( $end_point, EP_PAGES ); |
| 96 | } |
| 97 | flush_rewrite_rules(); |
| 98 | } |
| 99 | |
| 100 | public static function navigation() { |
| 101 | $data = [ |
| 102 | 'layout' => 'navigation', |
| 103 | 'current_user' => get_user_by( 'id', get_current_user_id() ), |
| 104 | ]; |
| 105 | Fns::tpg_template( $data, 'my-account' ); |
| 106 | } |
| 107 | |
| 108 | public static function account_content() { |
| 109 | |
| 110 | global $wp_query; |
| 111 | $allVars = $wp_query->query_vars; |
| 112 | |
| 113 | $data = [ |
| 114 | 'layout' => 'user-info', |
| 115 | 'current_user' => get_user_by( 'id', get_current_user_id() ), |
| 116 | ]; |
| 117 | |
| 118 | foreach ( Fns::get_endpoint() as $endpoint ) { |
| 119 | |
| 120 | if ( isset( $allVars[ $endpoint ] ) ) { |
| 121 | $data = [ |
| 122 | 'layout' => $endpoint, |
| 123 | 'current_user' => get_user_by( 'id', get_current_user_id() ), |
| 124 | ]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // No endpoint found? Default to dashboard. |
| 129 | Fns::tpg_template( $data, 'my-account' ); |
| 130 | } |
| 131 | |
| 132 | /*** |
| 133 | * Check form submitted or not |
| 134 | * Validate form data |
| 135 | ***/ |
| 136 | public function tpg_save_if_submitted() { |
| 137 | $settings = get_option( rtTPG()->options['settings'] ); |
| 138 | $url = $_SERVER['HTTP_REFERER']; |
| 139 | |
| 140 | // Verify nonce |
| 141 | if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'tpg-frontend-post' ) ) { |
| 142 | wp_safe_redirect( |
| 143 | esc_url_raw( |
| 144 | add_query_arg( 'status', 'error', $url ) |
| 145 | ) |
| 146 | ); |
| 147 | exit; |
| 148 | } |
| 149 | |
| 150 | // Stop running function if form wasn't submitted. |
| 151 | |
| 152 | if ( ! isset( $_POST['submit'] ) ) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | $category = []; |
| 157 | |
| 158 | if ( isset( $_POST['post_category'] ) ) { |
| 159 | $category = array_filter( array_map( 'absint', $_POST['post_category'] ) ); |
| 160 | } |
| 161 | |
| 162 | $user_id = get_current_user_id(); |
| 163 | |
| 164 | if ( $user_id != $_POST['uid'] ) { |
| 165 | wp_safe_redirect( |
| 166 | esc_url_raw( |
| 167 | add_query_arg( 'status', 'error', $url ) |
| 168 | ) |
| 169 | ); |
| 170 | exit; |
| 171 | } |
| 172 | |
| 173 | $post_args = [ |
| 174 | 'post_title' => ! empty( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '', |
| 175 | 'post_content' => ! empty( $_POST['content'] ) ? wp_kses_post( wp_unslash( $_POST['content'] ) ) : '', |
| 176 | 'post_excerpt' => ! empty( $_POST['excerpt'] ) ? sanitize_text_field( wp_unslash( $_POST['excerpt'] ) ) : '', |
| 177 | 'post_author' => $user_id, |
| 178 | 'post_category' => $category, |
| 179 | 'post_type' => 'post', |
| 180 | ]; |
| 181 | |
| 182 | if ( ! empty( $_POST['rtpg_post_tag'] ) ) { |
| 183 | $post_args['tags_input'] = sanitize_text_field( wp_unslash( $_POST['rtpg_post_tag'] ) ); |
| 184 | } |
| 185 | |
| 186 | if ( isset( $_POST['action'] ) && 'tpg_post_update' === $_POST['action'] ) { |
| 187 | $post_args['ID'] = ! empty( $_REQUEST['post_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_id'] ) ) : ''; |
| 188 | $post_args['post_status'] = ! empty( $_POST['post_status'] ) ? sanitize_text_field( wp_unslash( $_POST['post_status'] ) ) : 'pending'; |
| 189 | $insert_id = wp_update_post( $post_args ); |
| 190 | } else { |
| 191 | $post_args['post_status'] = ! empty( $settings['post_status'] ) ? $settings['post_status'] : 'pending'; |
| 192 | $insert_id = wp_insert_post( $post_args ); |
| 193 | } |
| 194 | |
| 195 | if ( $insert_id ) { |
| 196 | if ( current_user_can( 'upload_files' ) ) { |
| 197 | if ( ! empty( $_POST['tpg_feature_image'] ) ) { |
| 198 | set_post_thumbnail( $insert_id, intval( $_POST['tpg_feature_image'] ) ); |
| 199 | } |
| 200 | } else { |
| 201 | if ( ! empty( $_FILES['tpg-feature-image2']['name'] ) ) { |
| 202 | |
| 203 | /*Insert Featured Image*/ |
| 204 | $theFile = $_FILES['tpg-feature-image2']; |
| 205 | $file_path = sanitize_file_name( $theFile['name'] ); |
| 206 | $file_type = sanitize_mime_type( $theFile['type'] ); |
| 207 | $sanitized_file_data = array( |
| 208 | 'name' => $file_path, |
| 209 | 'full_path' => $file_path, |
| 210 | 'type' => $file_type, |
| 211 | 'tmp_name' => sanitize_text_field( $theFile['tmp_name'] ), |
| 212 | 'error' => sanitize_text_field( $theFile['error'] ), |
| 213 | 'size' => absint( $theFile['size'] ), |
| 214 | ); |
| 215 | |
| 216 | $fileName = $sanitized_file_data['name']; |
| 217 | $tempFile = $sanitized_file_data['tmp_name']; |
| 218 | |
| 219 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 220 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 221 | require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 222 | |
| 223 | $upload = wp_handle_upload( $sanitized_file_data, [ 'test_form' => false ] ); |
| 224 | |
| 225 | $wp_filetype = wp_check_filetype( basename( $upload['file'] ), null ); |
| 226 | $attachment = [ |
| 227 | 'post_mime_type' => $wp_filetype['type'], |
| 228 | 'post_title' => sanitize_file_name( $fileName ), |
| 229 | 'post_content' => '', |
| 230 | 'post_status' => 'inherit', |
| 231 | ]; |
| 232 | $attach_id = wp_insert_attachment( $attachment, $upload['file'], $insert_id ); |
| 233 | $attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] ); |
| 234 | wp_update_attachment_metadata( $attach_id, $attach_data ); |
| 235 | set_post_thumbnail( $insert_id, $attach_id ); |
| 236 | } |
| 237 | } |
| 238 | wp_safe_redirect( |
| 239 | esc_url_raw( |
| 240 | add_query_arg( 'status', 'success', Fns::get_account_endpoint_url( 'my-post' ) ) |
| 241 | ) |
| 242 | ); |
| 243 | |
| 244 | } else { |
| 245 | wp_safe_redirect( |
| 246 | esc_url_raw( |
| 247 | add_query_arg( 'status', 'fail', $url ) |
| 248 | ) |
| 249 | ); |
| 250 | } |
| 251 | exit; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | public static function tpg_tag_search() { |
| 256 | $taxonomy = 'post_tag'; |
| 257 | $taxonomy_object = get_taxonomy( $taxonomy ); |
| 258 | |
| 259 | if ( ! $taxonomy_object ) { |
| 260 | wp_die( 0 ); |
| 261 | } |
| 262 | |
| 263 | if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) { |
| 264 | wp_die( 0 ); |
| 265 | } |
| 266 | |
| 267 | if ( isset( $_GET['nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'tpg-frontend-post' ) ) { |
| 268 | wp_die( - 1 ); |
| 269 | } |
| 270 | |
| 271 | $search = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : ''; |
| 272 | |
| 273 | $comma = _x( ',', 'tag delimiter' ); |
| 274 | if ( ',' !== $comma ) { |
| 275 | $search = str_replace( $comma, ',', $search ); |
| 276 | } |
| 277 | |
| 278 | if ( str_contains( $search, ',' ) ) { |
| 279 | $search = explode( ',', $search ); |
| 280 | $search = $search[ count( $search ) - 1 ]; |
| 281 | } |
| 282 | |
| 283 | $search = trim( $search ); |
| 284 | |
| 285 | $term_search_min_chars = (int) apply_filters( 'tpg_term_search_min_chars', 2, $taxonomy_object, $search ); |
| 286 | |
| 287 | if ( ( 0 == $term_search_min_chars ) || ( strlen( $search ) < $term_search_min_chars ) ) { |
| 288 | wp_die(); |
| 289 | } |
| 290 | |
| 291 | $results = get_terms( |
| 292 | [ |
| 293 | 'taxonomy' => $taxonomy, |
| 294 | 'name__like' => $search, |
| 295 | 'fields' => 'names', |
| 296 | 'hide_empty' => false, |
| 297 | 'number' => isset( $_GET['number'] ) ? (int) $_GET['number'] : 0, |
| 298 | ] |
| 299 | ); |
| 300 | |
| 301 | $results = apply_filters( 'tpg_ajax_term_search_results', $results, $taxonomy_object, $search ); |
| 302 | |
| 303 | $success = false; |
| 304 | |
| 305 | $existingVal = isset( $_GET['existingVal'] ) ? sanitize_text_field( wp_unslash( $_GET['existingVal'] ) ) : ''; |
| 306 | $existingArr = explode( ',', $existingVal ); |
| 307 | $existingTrim = array_map( 'trim', $existingArr ); |
| 308 | $html = ''; |
| 309 | if ( ! empty( $results ) ) { |
| 310 | $success = true; |
| 311 | $html .= '<ul>'; |
| 312 | foreach ( $results as $name ) { |
| 313 | if ( in_array( $name, $existingTrim ) ) { |
| 314 | $html .= "<li class='disabled'>$name</li>"; |
| 315 | } else { |
| 316 | $html .= "<li>$name</li>"; |
| 317 | } |
| 318 | } |
| 319 | $html .= '</ul>'; |
| 320 | } |
| 321 | |
| 322 | $response = [ |
| 323 | 'success' => $success, |
| 324 | 'list' => $html, |
| 325 | ]; |
| 326 | |
| 327 | wp_send_json( $response ); |
| 328 | } |
| 329 | |
| 330 | public static function delete_post() { |
| 331 | $settings = get_option( rtTPG()->options['settings'] ); |
| 332 | |
| 333 | $delete_status = $settings['delete_post_status'] ?? 'trash'; |
| 334 | $success = false; |
| 335 | $message = $msg_class = $redirect_url = $post_id = null; |
| 336 | |
| 337 | if ( Fns::verifyNonce() ) { |
| 338 | $post_id = absint( $_REQUEST['post_id'] ); |
| 339 | $post_info = get_post( $post_id ); |
| 340 | |
| 341 | if ( $post_info && get_current_user_id() == $post_info->post_author && current_user_can( 'edit_post', $post_id ) ) { |
| 342 | $children = get_children( |
| 343 | apply_filters( |
| 344 | 'tpg_before_delete_post_attachment_query_args', |
| 345 | [ |
| 346 | 'post_parent' => $post_id, |
| 347 | 'post_type' => 'attachment', |
| 348 | 'posts_per_page' => - 1, |
| 349 | 'post_status' => 'inherit', |
| 350 | ], |
| 351 | $post_id |
| 352 | ) |
| 353 | ); |
| 354 | if ( 'delete' == $delete_status ) { |
| 355 | if ( ! empty( $children ) ) { |
| 356 | foreach ( $children as $child ) { |
| 357 | wp_delete_attachment( $child->ID, true ); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | do_action( 'tpe_before_delete_post', $post_id ); |
| 363 | if ( 'delete' == $delete_status ) { |
| 364 | wp_delete_post( $post_id, true ); |
| 365 | } else { |
| 366 | wp_trash_post( $post_id ); |
| 367 | } |
| 368 | $success = true; |
| 369 | $message .= esc_html__( 'Successfully deleted.', 'the-post-grid' ); |
| 370 | $redirect_url = Fns::get_account_endpoint_url( 'my-post' ); |
| 371 | } else { |
| 372 | $message .= esc_html__( 'Permission Error.', 'the-post-grid' ); |
| 373 | } |
| 374 | } else { |
| 375 | $message .= esc_html__( 'Session expired.', 'the-post-grid' ); |
| 376 | } |
| 377 | |
| 378 | wp_send_json( |
| 379 | apply_filters( |
| 380 | 'tpg_delete_post_ajax_response', |
| 381 | [ |
| 382 | 'success' => $success, |
| 383 | 'post_id' => $post_id, |
| 384 | 'message' => $message, |
| 385 | 'redirect_url' => $redirect_url, |
| 386 | ] |
| 387 | ) |
| 388 | ); |
| 389 | } |
| 390 | } |
| 391 |