Advertisement
longnguyenwp

Custom Frontend Dashboard

Oct 13th, 2021 (edited)
1,374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.72 KB | None | 0 0
  1. class My_Dashboard {
  2.     private $query;
  3.     private $edit_page_atts;
  4.  
  5.     public function __construct() {
  6.         add_shortcode( 'custom_frontend_dashboard', [ $this, 'shortcode' ] );
  7.     }
  8.  
  9.     public function shortcode( $atts ) {
  10.         /*
  11.          * Do not render the shortcode in the admin.
  12.          * Prevent errors with enqueue assets in Gutenberg where requests are made via REST to preload the post content.
  13.          */
  14.         if ( is_admin() ) {
  15.             return '';
  16.         }
  17.  
  18.         if ( ! is_user_logged_in() ) {
  19.             return esc_html__( 'Please login to view the dashboard.', 'mb-frontend-submission' );
  20.         }
  21.  
  22.         $this->edit_page_atts = $this->get_edit_page_attrs( $atts['edit_page'] );
  23.         if ( is_wp_error( $this->edit_page_atts ) ) {
  24.             return '<div class="rwmb-error">' . $this->edit_page_atts->get_error_message() . '</div>';
  25.         }
  26.  
  27.         $atts = shortcode_atts( [
  28.             // Meta box id.
  29.             'id'           => $this->edit_page_atts['id'],
  30.  
  31.             // Edit page id.
  32.             'edit_page'    => '',
  33.  
  34.             // Add new post button text
  35.             'add_new'      => __( 'Add New', 'mb-frontend-submission' ),
  36.  
  37.             // Delete permanently.
  38.             'force_delete' => 'false',
  39.  
  40.             // Columns to display.
  41.             'columns'      => 'title,date,status',
  42.  
  43.             // Column header labels.
  44.             'label_title'   => __( 'Title', 'mb-frontend-submission' ),
  45.             'label_date'    => __( 'Date', 'mb-frontend-submission' ),
  46.             'label_status'  => __( 'Status', 'mb-frontend-submission' ),
  47.             'label_actions' => __( 'Actions', 'mb-frontend-submission' ),
  48.  
  49.             // Link action for post title (view|edit).
  50.             'title_link'    => '',
  51.  
  52.             // Flag to hide/show welcome message.
  53.             'show_welcome_message' => 'true',
  54.         ], $atts );
  55.  
  56.         ob_start();
  57.         $this->query_posts();
  58.  
  59.         if ( $atts['show_welcome_message'] === 'true' ) {
  60.             $this->show_welcome_message();
  61.         }
  62.  
  63.         $this->show_user_posts( $atts );
  64.  
  65.         return ob_get_clean();
  66.     }
  67.  
  68.     private function query_posts() {
  69.         $this->query = new WP_Query( [
  70.             'author'                 => get_current_user_id(),
  71.             'post_type'              => $this->edit_page_atts['post_type'],
  72.             'posts_per_page'         => -1,
  73.             'post_status'            => 'any',
  74.             'fields'                 => 'ids',
  75.             'no_found_rows'          => true,
  76.             'update_post_meta_cache' => false,
  77.             'update_post_term_cache' => false,
  78.         ] );
  79.     }
  80.  
  81.     private function show_welcome_message() {
  82.         $post_type        = $this->edit_page_atts['post_type'];
  83.         $post_type_object = get_post_type_object( $post_type );
  84.         $user             = wp_get_current_user();
  85.         $query            = $this->query;
  86.  
  87.         $message  = '<h3>'. esc_html( sprintf( __( 'Howdie, %s!', 'mb-frontend-submission' ), $user->display_name ) ).'</h3>';
  88.         $message .= '<p>'. esc_html( sprintf( __( 'You have %d %s.', 'mb-frontend-submission' ), $query->post_count, strtolower( $post_type_object->labels->name ) ) ).'</p>';
  89.         $output   = apply_filters( 'mbfs_welcome_message', $message, $user, $query );
  90.         echo $output;
  91.     }
  92.  
  93.     private function get_edit_page_attrs( $edit_page_id ) {
  94.         $edit_page = get_post( $edit_page_id );
  95.         $pattern   = get_shortcode_regex( ['mb_frontend_form'] );
  96.  
  97.         if ( ! preg_match_all( '/' . $pattern . '/s', $edit_page->post_content, $matches ) || empty( $matches[2] ) || ! in_array( 'mb_frontend_form', $matches[2] ) ) {
  98.             return new WP_Error( 'mbfs-no-edit-page', __( 'Something is not right with the shortcode on page ID: ', $edit_page_id ) );
  99.         }
  100.  
  101.         // Get shortcode attributes.
  102.         $key            = array_search( 'mb_frontend_form', $matches[2] );
  103.         $shortcode_atts = explode( ' ', $matches[3][ $key ] );
  104.  
  105.         // Get only 'id' and 'post_type' attributes.
  106.         $attributes = [
  107.             'id'        => '',
  108.             'post_type' => 'post',
  109.             'url'       => get_permalink( $edit_page ),
  110.         ];
  111.         foreach ( $shortcode_atts as $attribute ) {
  112.             $attribute = explode( '=', $attribute );
  113.  
  114.             if ( in_array( $attribute[0], ['id', 'post_type'] ) ) {
  115.                 $attributes[ $attribute[0] ] = str_replace( ['"', "'"], '', $attribute[1] );
  116.             }
  117.         }
  118.  
  119.         return $attributes;
  120.     }
  121.  
  122.     private function show_user_posts( $atts ) {
  123.         $this->enqueue();
  124.         ?>
  125.         <a class="mbfs-add-new-post" href="<?= esc_url( $this->edit_page_atts['url'] ) ?>">
  126.             <?= esc_html( $atts['add_new'] ) ?>
  127.         </a>
  128.         <?php
  129.         if ( ! $this->query->have_posts() ) {
  130.             return;
  131.         }
  132.         ?>
  133.         <table class="mbfs-posts">
  134.             <tr>
  135.                 <?php
  136.                 $columns = Arr::from_csv( $atts['columns'] );
  137.                 if ( in_array( 'title', $columns ) ) {
  138.                     echo '<th>', esc_html( $atts['label_title'] ), '</th>';
  139.                 }
  140.                 if ( in_array( 'date', $columns ) ) {
  141.                     echo '<th>', esc_html( $atts['label_date'] ), '</th>';
  142.                 }
  143.                 if ( in_array( 'status', $columns ) ) {
  144.                     echo '<th>', esc_html( $atts['label_status'] ), '</th>';
  145.                 }
  146.                 ?>
  147.                 <th><?= esc_html( $atts['label_actions'] ) ?></th>
  148.             </tr>
  149.             <?php while( $this->query->have_posts() ) : $this->query->the_post(); ?>
  150.                 <tr>
  151.                     <?php if ( in_array( 'title', $columns ) ) : ?>
  152.                         <?php
  153.                         if ( $atts['title_link'] === 'edit' ) {
  154.                             $title_link = add_query_arg( 'rwmb_frontend_field_post_id', get_the_ID(), $this->edit_page_atts['url'] );
  155.                         } else {
  156.                             $title_link = get_the_permalink();
  157.                         }
  158.                         ?>
  159.                         <td><a href="<?= esc_url( $title_link ) ?>"><?php the_title(); ?></a></td>
  160.                     <?php endif; ?>
  161.  
  162.                     <?php if ( in_array( 'date', $columns ) ) : ?>
  163.                         <td align="center"><?php the_time( get_option( 'date_format' ) ) ?></td>
  164.                     <?php endif; ?>
  165.  
  166.                     <?php if ( in_array( 'status', $columns ) ) : ?>
  167.                         <td align="center"><?= get_post_status() ?></td>
  168.                     <?php endif; ?>
  169.  
  170.                     <td align="center" class="mbfs-actions">
  171.                         <a href="<?= esc_url( add_query_arg( 'rwmb_frontend_field_post_id', get_the_ID(), $this->edit_page_atts['url'] ) ) ?>" title="<?php esc_html_e( 'Edit', 'mb-frontend-submission' ) ?>">
  172.                             <img src="<?= MBFS_URL . 'assets/pencil.svg' ?>">
  173.                         </a>
  174.                         <?= do_shortcode( '[mb_frontend_form id="' . $this->edit_page_atts['id'] . '" post_id="' . get_the_ID() . '" ajax="true" allow_delete="true" force_delete="' . $atts['force_delete'] . '" only_delete="true" delete_button="<img src=\'' . MBFS_URL . 'assets/trash.svg' . '\'>"]' ); ?>
  175.                     </td>
  176.                 </tr>
  177.             <?php endwhile ?>
  178.         </table>
  179.         <?php
  180.         wp_reset_postdata();
  181.     }
  182.  
  183.     private function enqueue() {
  184.         wp_enqueue_style( 'mbfs-dashboard', MBFS_URL . 'assets/dashboard.css', '', MBFS_VER );
  185.         wp_enqueue_script( 'mbfs', MBFS_URL . 'assets/frontend-submission.js', array( 'jquery' ), MBFS_VER, true );
  186.     }
  187. }
  188.  
  189. new My_Dashboard();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement