» » » Building WordPress Plugins with Object Oriented Concept

Building WordPress Plugins with Object Oriented Concept

posted in: WordPress | 0
Facebook
Google+
https://applerinquest.com/building-wordpress-plugins-with-object-oriented-concept">
LinkedIn
Follow by Email

When I develop the custom plugin, I prefer to build the custom plugin with Object Oriented Programming(OOP) to avoid the conflict from other plugins or the themes. Plus my code is cleaner and more organized. Today I will share how to create the report plugin with OOP.

 

Here what the plugin does:

The plugin will display the data regarding the criteria. I use the datatable plugin for the data table with search, pagination and column sort features. The datatable plugin has more features you can use. The datepicker plugin I use jquery-ui plugin.

 

Reports plugin structure:

  • mct-reports
    • classes
      • class-reports.php
      • class-translation.php
    • css
      • images (this is for jquery-ui)
      • datepicker-bootstrap3.css
      • jquery-ui.min.css
      • mct-reports.css (my custom style for the reports)
    • js
    • languages
      • mct-reports-th_TH.mo
      • mct-reports-th_TH.po
    • lib
    • mct-reports.php (main plugin file)

 

mct-reports.php

<?php
/*
  Plugin Name: Reports
  Description: 
  Version: 1.0
  Author: <a  href="http://applerinquest.com" target="_BLANK">AppleRinquest</a>
  Text Domain: mct-reports
  Domain Path: /languages/  
 */

//  declare the constant
define('MCT_REPORTS_PLUGIN_PATH', dirname(__FILE__));  // this constant uses in the class files
define('MCT_REPORTS_PLUGIN_URL' , WP_PLUGIN_URL . '/mct-reports/' ); // this constant uses in enqueue file and style
define('MCT_REPORTS_LANG_PATH', basename( dirname( __FILE__ ) )); 

// our custom classes
require_once( dirname(__FILE__) . '/classes/class-translation.php' );
require_once( dirname(__FILE__) . '/classes/class-reports.php' );

 

class-reports.php

<?php
    // If this file is called directly, abort.
    if (!defined('ABSPATH')) die("No cheating!");

    if(!class_exists('Mtc_Reports')) {
        
        class Mtc_Reports
        {
        
            /**
             * Start up
             */
            public function __construct() {
              // add admin menu
              add_action( 'admin_menu', array( $this, 'reports_menu' ) );
              
              // add scripts and style on the backend
              add_action( 'admin_enqueue_scripts', array( $this,'reports_admin_scripts') );  
              add_action( 'wp_ajax_get_job_applications', array( $this, 'get_job_applications') );
            }
        
        
            /**
             * add reports menu
             */
            public function reports_menu() {
              // add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
              add_menu_page(__('Reports','mct-reports'), __('Reports','mct-reports'), 'edit_others_posts', 'mct-reports', array( $this, 'reports_section') );        
            }
        
        
            /**
             * add jquery ui datepicker widget
             */
            public function reports_admin_scripts() 
            {
              // add the script and style on our plugin only
              if (isset($_GET['page']) && $_GET['page'] === 'mct-reports') {
            
                // # load style
                // load datepicker plugin style
                // @link https://gist.github.com/miwahall/7028640
                wp_enqueue_style('mct-reports-bootstrap-style', MCT_REPORTS_PLUGIN_URL . 'css/datepicker-bootstrap3.css' );
                
                // load datatable plugin style
                wp_enqueue_style('mct-reports-datatable-style', MCT_REPORTS_PLUGIN_URL . 'lib/DataTables/datatables.min.css' );
            
                // load custom style
                wp_enqueue_style('mct-reports-style', MCT_REPORTS_PLUGIN_URL . 'css/mct-reports.css' );
            
            
            
                // # load scripts
                // datepicker plugin
                wp_enqueue_script( 'mct-reports-datepicker' , MCT_REPORTS_PLUGIN_URL . 'js/jquery-ui.min.js', array('jquery') ,true);
                wp_enqueue_script( 'mct-reports' , MCT_REPORTS_PLUGIN_URL . 'js/mct-reports.js', array('jquery','mct-reports-datepicker') ,true);
                wp_localize_script( 'mct-reports', 'mctReportsAjax', array(
                    'ajaxurl' => admin_url('admin-ajax.php')
                ));
            
                // datatable plugin
                wp_enqueue_script( 'mct-reports-datatable' , MCT_REPORTS_PLUGIN_URL . 'lib/DataTables/datatables.min.js', array('jquery') ,true);
              }
            }
        
        
            /**
             * display the reports
             */
            public function reports_section()
            {
                // make sure the users have the access permision
                if (!current_user_can('edit_others_posts')) {
                  wp_die('Unauthorized user');
                }
                ?>
            
            
                <div class="wrap">
                    <!-- Title -->
                    <h1><?php echo __('Reports', 'mct-reports') ?></h1>
                    <br>
            
                    <div>
                        <!-- render a criteria -->
                        <table id="report_criteria" class="form-table">
                          <tr>
                            <th>
                              <?php echo __('Time period', 'mct-reports') ?>
                            </th>
                            <td id="report_date">
                              <!-- default date is last 7 days until today -->
                              <input type="text" id="txtStartDate" name="txtStartDate" value="<?php echo date('d/m/Y', strtotime('-7 days')); ?>">      
                              <?php echo __(' to ','mct-reports'); ?>              
                              <input type="text" id="txtEndDate" name="txtEndDate" value="<?php echo date('d/m/Y'); ?>"> 
                              <div class="help-block with-errors" style="display:none;"><?php echo __('Please choose the time period','mct-reports') ?></div>                     
                            </td>
                          </tr>       
                          <tr>
                            <th>
                              <?php echo __('Select a report','mct-reports') ?>
                            </th>
                            <td>
                              <select id="reports_selector" name="reports_selector">
                                <option value="total" selected><?php echo __('Total job applications','mct-reports') ?></option>
                                <option value="total_by_location"><?php echo __('Total job applications by locations','mct-reports') ?></option>
                                <option value="total_by_positionType"><?php echo __('Total job applications by position type','mct-reports') ?></option>
                                <option value="total_by_specialty"><?php echo __('Total job applications by specialty','mct-reports') ?></option>
                              </select>                  
                            </td>
                          </tr> 
                          <tr>
                            <td style="padding-left:0px;">
                              <button class="btn-add button-primary" id="btn_report" name="btn_report"><?php echo __('Show report','mct-reports') ?></button>
                            </td>
                          </tr>                 
                        </table>
                      </div>
                </div> 
                <!-- end div.wrap -->
            
                <br>
            
            
                <div class="wrap">
                    <!-- render a table -->
                    <!-- @link https://datatables.net/examples/data_sources/dom.html -->
                    <table id="mct_report" class="table table-striped table-bordered" style="width:100%; display:none;">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Name</th>
                                <th>Position Applied</th>
                                <th>Salary Expectation</th>
                                <th>Email</th>
                                <th>Date</th>
                            </tr>
                        </thead>
            
                        <!-- the body section will inject by ajax  -->
            
                        <tfoot>
                            <tr>
                                <th>#</th>
                                <th>Name</th>
                                <th>Position Applied</th>
                                <th>Salary Expectation</th>
                                <th>Email</th>
                                <th>Date</th>
                            </tr>
                        </tfoot>
                    </table>
                </div> 
                <!-- end div.wrap -->
                
            
            
                <div class="wrap">
                    <!-- @link https://datatables.net/examples/advanced_init/row_grouping.html -->
                    <table id="mct_report_grouping" class="table table-striped table-bordered" style="width:100%; display:none;">
                      <thead>
                          <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Position Applied</th>
                            <th>Salary Expectation</th>
                            <th>Email</th>
                            <th>Date</th>
                            <th>Localtion</th>
                            <th>Position Type</th>
                            <th>Specialty</th>
                          </tr>
                      </thead>
            
                             
                        <!-- the body section will inject by ajax  -->
            
                      <tfoot>
                          <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Position Applied</th>
                            <th>Salary Expectation</th>
                            <th>Email</th>
                            <th>Date</th>
                            <th>Localtion</th>
                            <th>Position Type</th>
                            <th>Specialty</th>
                          </tr>
                      </tfoot>
                    </table>        
                </div>
                <!-- end div.wrap -->
            
                <?php
            }
        
        
            /**
             * get data from table
             */
            public function get_job_applications() 
            {
                global $wpdb;
                $table_name = $wpdb->prefix . 'job_applications'; // do not forget about tables prefix
                $status = 'active';
                $orderby = 'date_created, firstname';
                $order = 'asc';
            
                // # the parameters from ajax are $_POST['start_date'] and $_POST['end_date']
                // set the query condition and convert date format for using in query
                $start_date = strtr( $_POST['start_date'], '/', '-' );
                $start_date = strtotime($start_date);
                $start_date = date('Y-m-d',$start_date);  
            
                $end_date = strtr( $_POST['end_date'], '/', '-' );
                $end_date = strtotime($end_date);
                $end_date = date('Y-m-d',$end_date);
            
                // get current user id
                $user_id = get_current_user_id();        
                
                // get user role
                $user_meta = get_userdata( $user_id );
                $user_roles = $user_meta->roles;
    
                // query data from the custom table
                $query_result = $wpdb->get_results(
                  $wpdb->prepare("SELECT * FROM $table_name WHERE status = '$status' AND date_created BETWEEN '$start_date' AND '$end_date' ORDER BY %s %s", array( $orderby, $order ) )
                  , OBJECT);
                
                
                // add post meta into the query result so we can use these values in ajax call
                foreach( $query_result  as $value ) {
                    // get post meta
                    $job_localtion_id = get_post_meta( $value->id_job_opp, 'job_location', true );
                    $position_type_id = get_post_meta( $value->id_job_opp, 'job_type_position', true );
                    $specialty_id = get_post_meta( $value->id_job_opp, 'job_specialty', true );    
            
                    // get post title from custom post type
                    $job_location = get_the_title( $job_localtion_id );
            
                    // get term name by ID
                    $position_type = get_term_by( 'id' , $position_type_id, 'job_types_position' )->name;
                    $specialty = get_term_by( 'id' , $specialty_id, 'job_specialty' )->name;
            
                    // add 3 additional columns for row grouping table
                    $value->job_location = ( trim($job_location)==='' ? 'No Location' : $job_location );
                    $value->position_type = ( trim($position_type)==='' ? 'No Position Type' : $position_type );
                    $value->specialty = ( trim($specialty)==='' ? 'No Specialty' : $specialty );
                }
            
                // echo as json object to ajax calling
                echo json_encode($query_result);
            
                // # MUST add wp_die() here to avoid the 0 number that appends to the result json object and send back to ajax calling
                wp_die();
            }

        } // class ends
    
    
        // # initiate class on in the WP backend
        if( is_admin() ) {
            $mtc_reports = new Mtc_Reports;
        }
     
    } // class_exists() ends

class-translation.php

<?php
    // If this file is called directly, abort.
    if (!defined('ABSPATH')) die("No cheating!");

    if(!class_exists('Mtc_Reports_Translation')) 
    {
        
     class Mtc_Reports_Translation
     {
      public function __construct() {
        // translation
        add_action( 'init', array( $this, 'mct_reports_load_textdomain' ) );   
      }
    
      public function mct_reports_load_textdomain() {
        load_plugin_textdomain( 'mct-reports', false, MCT_REPORTS_LANG_PATH . '/languages' ); 
      } 
     }
    
      // initialize class
      if( is_admin() ) {
        $mtc_reports_translation = new Mtc_Reports_Translation;
      }
    }

 

The sample code above shows you how to write the plugin in OOP way. Basically, it is the same PHP class. One thing you have to do in order to call the functions within the class, is passing $this in the array() just like the code below.

<?php
    add_action( 'init', array( $this, 'mct_reports_load_textdomain' ) );
?>

If you don’t know how to write the code in OOP. You may start with something like the video tutorial.

 

Yep, that’s it for today. Hope you enjoy!

Facebook
Google+
https://applerinquest.com/building-wordpress-plugins-with-object-oriented-concept">
LinkedIn
Follow by Email