Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.06 KB | None | 0 0
  1. <?php
  2. class I15_Table_Builder {
  3.  
  4.     /**
  5.      * The loader that's responsible for maintaining and registering all hooks that power
  6.      * the plugin.
  7.      *
  8.      * @since    1.0.0
  9.      * @access   protected
  10.      * @var      I15_Table_Builder_Loader    $loader    Maintains and registers all hooks for the plugin.
  11.      */
  12.     protected $loader;
  13.  
  14.     /**
  15.      * The unique identifier of this plugin.
  16.      *
  17.      * @since    1.0.0
  18.      * @access   protected
  19.      * @var      string    $plugin_name    The string used to uniquely identify this plugin.
  20.      */
  21.     protected $plugin_name;
  22.  
  23.     /**
  24.      * The current version of the plugin.
  25.      *
  26.      * @since    1.0.0
  27.      * @access   protected
  28.      * @var      string    $version    The current version of the plugin.
  29.      */
  30.     protected $version;
  31.  
  32.     /**
  33.      * Define the core functionality of the plugin.
  34.      *
  35.      * Set the plugin name and the plugin version that can be used throughout the plugin.
  36.      * Load the dependencies, define the locale, and set the hooks for the admin area and
  37.      * the public-facing side of the site.
  38.      *
  39.      * @since    1.0.0
  40.      */
  41.     public function __construct() {
  42.  
  43.         $this->plugin_name = 'i15-table-builder';
  44.         $this->version = '1.0.0';
  45.  
  46.         $this->load_dependencies();
  47.         $this->set_locale();
  48.         $this->define_admin_hooks();
  49.         $this->define_admin_filters();
  50.         $this->define_public_hooks();
  51.  
  52.     }
  53.  
  54.     /**
  55.      * Load the required dependencies for this plugin.
  56.      *
  57.      * Include the following files that make up the plugin:
  58.      *
  59.      * - I15_Table_Builder_Loader. Orchestrates the hooks of the plugin.
  60.      * - I15_Table_Builder_i18n. Defines internationalization functionality.
  61.      * - I15_Table_Builder_Admin. Defines all hooks for the admin area.
  62.      * - I15_Table_Builder_Public. Defines all hooks for the public side of the site.
  63.      *
  64.      * Create an instance of the loader which will be used to register the hooks
  65.      * with WordPress.
  66.      *
  67.      * @since    1.0.0
  68.      * @access   private
  69.      */
  70.     private function load_dependencies() {
  71.  
  72.     require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-tags.php';
  73.         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-shortcode.php';
  74.         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-tabbed-shortcode.php';
  75.         /**
  76.          * The class responsible for orchestrating the actions and filters of the
  77.          * core plugin.
  78.          */
  79.         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-loader.php';
  80.  
  81.         /**
  82.          * The class responsible for defining internationalization functionality
  83.          * of the plugin.
  84.          */
  85.         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-i18n.php';
  86.  
  87.         /**
  88.          * The class responsible for defining all actions that occur in the admin area.
  89.          */
  90.         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-i15-table-builder-admin.php';
  91.  
  92.         /**
  93.          * The class responsible for defining all actions that occur in the public-facing
  94.          * side of the site.
  95.          */
  96.         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-i15-table-builder-public.php';
  97.  
  98.       require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-custom-css-builder.php';
  99.  
  100.         $this->loader = new I15_Table_Builder_Loader();
  101.         $this->tags = new I15_Table_Builder_Tags();
  102.         $this->shortcode = new I15_Table_Builder_Shortcode();
  103.         $this->tabbedshortcode = new I15_Table_Builder_Tabbed_Shortcode();
  104.  
  105.         if ( function_exists( 'acf_add_options_sub_page' ) ){
  106.             acf_add_options_sub_page(array(
  107.                 'title'      => 'TB Settings',
  108.                 'parent'     => 'i15-table-builder',
  109.                 'capability' => 'manage_options'
  110.             ));
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * Define the locale for this plugin for internationalization.
  116.      *
  117.      * Uses the I15_Table_Builder_i18n class in order to set the domain and to register the hook
  118.      * with WordPress.
  119.      *
  120.      * @since    1.0.0
  121.      * @access   private
  122.      */
  123.     private function set_locale() {
  124.  
  125.         $plugin_i18n = new I15_Table_Builder_i18n();
  126.  
  127.         $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
  128.  
  129.     }
  130.  
  131.     private function define_admin_filters() {
  132.  
  133.          $plugin_admin = new I15_Table_Builder_Admin( $this->get_plugin_name(), $this->get_version() );
  134.  
  135.          $this->loader->add_filter( 'user_can_richedit', $this, 'disable_wyswyg_for_custom_post_types' );
  136.  
  137.          $this->loader->add_filter( 'manage_tb_table_posts_columns', $this, 'add_tb_table_columns' );
  138.  
  139.          $this->loader->add_filter( 'page_row_actions', $this, 'duplicate_post_link', 10, 2 );
  140.  
  141.          $this->loader->add_filter( 'acf/load_value/name=tb_table_operators', $this, 'tb_table_operators_load_value' );
  142.          $this->loader->add_filter( 'acf/load_value/name=tab_operators', $this, 'tab_operators_load_value' );
  143.  
  144.     }
  145.  
  146.     function tb_table_operators_load_value( $value, $post_id, $field ) {
  147.         $order = array();
  148.  
  149.         foreach( $value as $i => $row ) {
  150.             $order[ $i ] = $row['field_595d5fb9be2b5'];
  151.         }
  152.  
  153.         array_multisort( $order, SORT_DESC, $value );
  154.  
  155.         return $value;
  156.     }
  157.  
  158.     function tab_operators_load_value( $value, $post_id, $field ) {
  159.         $order = array();
  160.  
  161.         foreach( $value as $i => $row ) {
  162.             $order[ $i ] = $row['field_595d605ebe2b8'];
  163.         }
  164.  
  165.         array_multisort( $order, SORT_DESC, $value );
  166.  
  167.         return $value;
  168.     }
  169.  
  170.     /**
  171.      * Register all of the hooks related to the admin area functionality
  172.      * of the plugin.
  173.      *
  174.      * @since    1.0.0
  175.      * @access   private
  176.      */
  177.     private function define_admin_hooks() {
  178.  
  179.         $plugin_admin = new I15_Table_Builder_Admin( $this->get_plugin_name(), $this->get_version() );
  180.         $plugin_custom_css = new I15_Table_Builder_Custom_CSS_Builder( $this->get_plugin_name(), $this->get_version() );
  181.  
  182.         $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
  183.         $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
  184.  
  185.         $this->loader->add_action( 'init', $this, 'tb_template_post_type' );
  186.         $this->loader->add_action( 'init', $this, 'tb_table_post_type' );
  187.  
  188.         $this->loader->add_action( 'admin_menu', $this, 'tb_admin_menu' );
  189.  
  190.         $this->loader->add_action( 'add_meta_boxes', $this, 'tb_template_register_meta_boxes' );
  191.  
  192.         $this->loader->add_action( 'manage_tb_table_posts_custom_column', $this, 'tb_table_custom_column', 10, 2 );
  193.  
  194.         $this->loader->add_action( 'admin_action_duplicate_post_as_draft', $this, 'duplicate_post_as_draft' );
  195.  
  196.         $this->loader->add_action( 'save_post', $plugin_custom_css, 'build_table_custom_css' );
  197.  
  198.         $this->loader->add_action( 'acf/save_post', $this, 'write_base_css_file' );
  199.         //$this->loader->add_action( 'save_post', $this, 'log_it_save' );
  200.  
  201.     }
  202.  
  203.      function log_it_save( $message ) {
  204.        $this->log_it($_POST);
  205.      $this->log_it('The value for ' . $custom_field . ' is ' . $_POST[$custom_field]);
  206.      }
  207.  
  208.      function log_it( $message ) {
  209.        if( WP_DEBUG === true ){
  210.          if( is_array( $message ) || is_object( $message ) ){
  211.            error_log( print_r( $message, true ) );
  212.          } else {
  213.            error_log( $message );
  214.          }
  215.        }
  216.      }
  217.  
  218.     /**
  219.      * Register all of the hooks related to the public-facing functionality
  220.      * of the plugin.
  221.      *
  222.      * @since    1.0.0
  223.      * @access   private
  224.      */
  225.     private function define_public_hooks() {
  226.  
  227.         $plugin_public = new I15_Table_Builder_Public( $this->get_plugin_name(), $this->get_version() );
  228.  
  229.         $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
  230.         $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
  231.  
  232.         $this->loader->add_action( 'init', $this, 'tb_template_post_type' );
  233.         $this->loader->add_action( 'init', $this, 'tb_table_post_type' );
  234.  
  235.     }
  236.  
  237.     function write_base_css_file() {
  238.         $screen = get_current_screen();
  239.         if (strpos($screen->id, "acf-options-tb-settings") == true) {
  240.           $file_path = plugin_dir_path( dirname( __FILE__ ) ) . 'public/css/i15-table-builder-public.css';
  241.       chmod($file_path, 0777);
  242.           $opened_file = fopen($file_path, "w") or die("Unable to open ".$file_path." file!");
  243.             $base_css = get_field('base_css', 'option');
  244.             fwrite($opened_file, $base_css."\n");
  245.             fclose($opened_file);
  246.         }
  247.     }
  248.  
  249.     function tb_admin_menu() {
  250.     add_menu_page(
  251.       'Table Builder',
  252.       'Table Builder',
  253.       'read',
  254.       'i15-table-builder',
  255.       '',
  256.       'dashicons-screenoptions',
  257.       21 // Position
  258.     );
  259.  
  260.         add_submenu_page('i15-table-builder', 'Import', 'Import', 'manage_options', 'tb-import', array($this, 'import_display'));
  261.         add_submenu_page('i15-table-builder', 'Export', 'Export', 'manage_options', 'tb-export', array($this, 'export_display'));
  262.     }
  263.  
  264.     public function tb_template_post_type() {
  265.         register_post_type(
  266.         'tb_template',
  267.         array(
  268.             'labels' => array(
  269.                 'name' => __('Templates'),
  270.                 'singular_name' => __('Template'),
  271.                 'add_new_item' => __('Add New Template'),
  272.                 'edit_item' => __('Edit Template'),
  273.                 'view_item' => __('View Template'),
  274.                 'search_items' =>  __('Search Templates'),
  275.                 'not_found' => __('No Templates found!'),
  276.                 'not_found_in_trash' => __('No Templates found in trash'),
  277.                 'menu_name' => __('Templates'),
  278.                 'all_items' => __('All Templates')
  279.             ),
  280.             'description' => 'I15 Table Templates',
  281.             'public' => false,
  282.             'show_ui' => true,
  283.             'menu_position' => 20,
  284.             'hierarchical' => true,
  285.             'supports' => array('title', 'editor'),
  286.             'show_in_menu' => 'i15-table-builder',
  287.             'can_export' => true,
  288.             'has_archive' => false,
  289.             'exclude_from_search' => true
  290.         ));
  291.  
  292.     }
  293.  
  294.     function tb_template_register_meta_boxes() {
  295.       add_meta_box('tb_template_meta', "Template Tags", array($this, 'tb_template_tags_callback'), 'tb_template');
  296.     }
  297.  
  298.     function tb_template_tags_callback($post) {
  299.     foreach($this->tags->get_tags_info() AS $tag => $desc) {
  300.       echo '<p>';
  301.         echo '<h3>'.$tag.'</h3>';
  302.         echo $desc;
  303.         echo '</p>';
  304.     }
  305.     }
  306.  
  307.     public function tb_table_post_type() {
  308.         register_post_type(
  309.         'tb_table',
  310.         array(
  311.             'labels' => array(
  312.                 'name' => __('Tables'),
  313.                 'singular_name' => __('Table'),
  314.                 'add_new_item' => __('Add New Table'),
  315.                 'edit_item' => __('Edit Table'),
  316.                 'view_item' => __('View Table'),
  317.                 'search_items' =>  __('Search Tables'),
  318.                 'not_found' => __('No Tables found!'),
  319.                 'not_found_in_trash' => __('No Tables found in trash'),
  320.                 'menu_name' => __('Tables'),
  321.                 'all_items' => __('All Tables')
  322.             ),
  323.             'description' => 'I15 Tables',
  324.             'public' => false,
  325.             'show_ui' => true,
  326.             'menu_position' => 20,
  327.             'hierarchical' => true,
  328.             'supports' => array('title'),
  329.             'show_in_menu' => 'i15-table-builder',
  330.             'can_export' => true,
  331.             'has_archive' => false,
  332.             'exclude_from_search' => true
  333.         ));
  334.  
  335.     }
  336.  
  337.     function add_tb_table_columns($columns) {
  338.     unset($columns['date']);
  339.     return array_merge($columns,
  340.                  array('shortcode' => __('Shortcode')),
  341.                              array('date' => __('Date'))
  342.            );
  343.     }
  344.  
  345.     function tb_table_custom_column( $column, $post_id ) {
  346.         switch ( $column ) {
  347.             case 'shortcode':
  348.  
  349.                 $operators_objects = get_field('tb_table_operators', $post_id);
  350.                 if($operators_objects) {
  351.                   echo "[i15-table-builder tableid='".$post_id."']<br />";
  352.         }
  353.  
  354.         if(have_rows('tab_data', $post_id)) {
  355.                   echo "[i15-tabbed-table-builder tableid='".$post_id."']<br />";
  356.         }
  357.  
  358.                 break;
  359.         }
  360.     }
  361.  
  362.     function disable_wyswyg_for_custom_post_types( $default ){
  363.       global $post;
  364.       if( 'tb_template' === get_post_type($post)) return false;
  365.       return $default;
  366.     }
  367.  
  368.     function duplicate_post_as_draft(){
  369.         global $wpdb;
  370.         if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
  371.             wp_die('No post to duplicate has been supplied!');
  372.         }
  373.  
  374.         /*
  375.          * Nonce verification
  376.          */
  377.         if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
  378.             return;
  379.  
  380.         /*
  381.          * get the original post id
  382.          */
  383.         $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
  384.         /*
  385.          * and all the original post data then
  386.          */
  387.         $post = get_post( $post_id );
  388.  
  389.         /*
  390.          * if you don't want current user to be the new post author,
  391.          * then change next couple of lines to this: $new_post_author = $post->post_author;
  392.          */
  393.         $current_user = wp_get_current_user();
  394.         $new_post_author = $current_user->ID;
  395.  
  396.         /*
  397.          * if post data exists, create the post duplicate
  398.          */
  399.         if (isset( $post ) && $post != null) {
  400.  
  401.             /*
  402.              * new post data array
  403.              */
  404.             $args = array(
  405.                 'comment_status' => $post->comment_status,
  406.                 'ping_status'    => $post->ping_status,
  407.                 'post_author'    => $new_post_author,
  408.                 'post_content'   => $post->post_content,
  409.                 'post_excerpt'   => $post->post_excerpt,
  410.                 'post_name'      => $post->post_name,
  411.                 'post_parent'    => $post->post_parent,
  412.                 'post_password'  => $post->post_password,
  413.                 'post_status'    => 'draft',
  414.                 'post_title'     => "Copy of ".$post->post_title,
  415.                 'post_type'      => $post->post_type,
  416.                 'to_ping'        => $post->to_ping,
  417.                 'menu_order'     => $post->menu_order
  418.             );
  419.  
  420.             /*
  421.              * insert the post by wp_insert_post() function
  422.              */
  423.             $new_post_id = wp_insert_post( $args );
  424.  
  425.             /*
  426.              * get all current post terms ad set them to the new post draft
  427.              */
  428.             $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
  429.             foreach ($taxonomies as $taxonomy) {
  430.                 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
  431.                 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
  432.             }
  433.  
  434.             /*
  435.              * duplicate all post meta just in two SQL queries
  436.              */
  437.             $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
  438.             if (count($post_meta_infos)!=0) {
  439.                 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
  440.                 foreach ($post_meta_infos as $meta_info) {
  441.                     $meta_key = $meta_info->meta_key;
  442.                     if( $meta_key == '_wp_old_slug' ) continue;
  443.                     $meta_value = addslashes($meta_info->meta_value);
  444.                     $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
  445.                 }
  446.                 $sql_query.= implode(" UNION ALL ", $sql_query_sel);
  447.                 $wpdb->query($sql_query);
  448.             }
  449.  
  450.  
  451.             /*
  452.              * finally, redirect to the edit post screen for the new draft
  453.              */
  454.             wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
  455.             exit;
  456.         } else {
  457.             wp_die('Post creation failed, could not find original post: ' . $post_id);
  458.         }
  459.     }
  460.  
  461.     /*
  462.      * Add the duplicate link to action list for post_row_actions
  463.      */
  464.     function duplicate_post_link( $actions, $post ) {
  465.         if (current_user_can('edit_posts') && ($post->post_type == "tb_template" || $post->post_type == "tb_table")) {
  466.             $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
  467.         }
  468.         return $actions;
  469.     }
  470.  
  471.  
  472.   function import_display(){
  473.       include('class-i15-table-builder-import.php');
  474.   }
  475.  
  476.  
  477.   function export_display(){
  478.     include('class-i15-table-builder-export.php');
  479.   }
  480.  
  481.     /**
  482.      * Run the loader to execute all of the hooks with WordPress.
  483.      *
  484.      * @since    1.0.0
  485.      */
  486.     public function run() {
  487.         $this->loader->run();
  488.     }
  489.  
  490.     /**
  491.      * The name of the plugin used to uniquely identify it within the context of
  492.      * WordPress and to define internationalization functionality.
  493.      *
  494.      * @since     1.0.0
  495.      * @return    string    The name of the plugin.
  496.      */
  497.     public function get_plugin_name() {
  498.         return $this->plugin_name;
  499.     }
  500.  
  501.     /**
  502.      * The reference to the class that orchestrates the hooks with the plugin.
  503.      *
  504.      * @since     1.0.0
  505.      * @return    I15_Table_Builder_Loader    Orchestrates the hooks of the plugin.
  506.      */
  507.     public function get_loader() {
  508.         return $this->loader;
  509.     }
  510.  
  511.     /**
  512.      * Retrieve the version number of the plugin.
  513.      *
  514.      * @since     1.0.0
  515.      * @return    string    The version number of the plugin.
  516.      */
  517.     public function get_version() {
  518.         return $this->version;
  519.     }
  520.  
  521. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement