Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class I15_Table_Builder {
- /**
- * The loader that's responsible for maintaining and registering all hooks that power
- * the plugin.
- *
- * @since 1.0.0
- * @access protected
- * @var I15_Table_Builder_Loader $loader Maintains and registers all hooks for the plugin.
- */
- protected $loader;
- /**
- * The unique identifier of this plugin.
- *
- * @since 1.0.0
- * @access protected
- * @var string $plugin_name The string used to uniquely identify this plugin.
- */
- protected $plugin_name;
- /**
- * The current version of the plugin.
- *
- * @since 1.0.0
- * @access protected
- * @var string $version The current version of the plugin.
- */
- protected $version;
- /**
- * Define the core functionality of the plugin.
- *
- * Set the plugin name and the plugin version that can be used throughout the plugin.
- * Load the dependencies, define the locale, and set the hooks for the admin area and
- * the public-facing side of the site.
- *
- * @since 1.0.0
- */
- public function __construct() {
- $this->plugin_name = 'i15-table-builder';
- $this->version = '1.0.0';
- $this->load_dependencies();
- $this->set_locale();
- $this->define_admin_hooks();
- $this->define_admin_filters();
- $this->define_public_hooks();
- }
- /**
- * Load the required dependencies for this plugin.
- *
- * Include the following files that make up the plugin:
- *
- * - I15_Table_Builder_Loader. Orchestrates the hooks of the plugin.
- * - I15_Table_Builder_i18n. Defines internationalization functionality.
- * - I15_Table_Builder_Admin. Defines all hooks for the admin area.
- * - I15_Table_Builder_Public. Defines all hooks for the public side of the site.
- *
- * Create an instance of the loader which will be used to register the hooks
- * with WordPress.
- *
- * @since 1.0.0
- * @access private
- */
- private function load_dependencies() {
- require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-tags.php';
- require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-shortcode.php';
- require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-tabbed-shortcode.php';
- /**
- * The class responsible for orchestrating the actions and filters of the
- * core plugin.
- */
- require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-loader.php';
- /**
- * The class responsible for defining internationalization functionality
- * of the plugin.
- */
- require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-i18n.php';
- /**
- * The class responsible for defining all actions that occur in the admin area.
- */
- require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-i15-table-builder-admin.php';
- /**
- * The class responsible for defining all actions that occur in the public-facing
- * side of the site.
- */
- require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-i15-table-builder-public.php';
- require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-i15-table-builder-custom-css-builder.php';
- $this->loader = new I15_Table_Builder_Loader();
- $this->tags = new I15_Table_Builder_Tags();
- $this->shortcode = new I15_Table_Builder_Shortcode();
- $this->tabbedshortcode = new I15_Table_Builder_Tabbed_Shortcode();
- if ( function_exists( 'acf_add_options_sub_page' ) ){
- acf_add_options_sub_page(array(
- 'title' => 'TB Settings',
- 'parent' => 'i15-table-builder',
- 'capability' => 'manage_options'
- ));
- }
- }
- /**
- * Define the locale for this plugin for internationalization.
- *
- * Uses the I15_Table_Builder_i18n class in order to set the domain and to register the hook
- * with WordPress.
- *
- * @since 1.0.0
- * @access private
- */
- private function set_locale() {
- $plugin_i18n = new I15_Table_Builder_i18n();
- $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
- }
- private function define_admin_filters() {
- $plugin_admin = new I15_Table_Builder_Admin( $this->get_plugin_name(), $this->get_version() );
- $this->loader->add_filter( 'user_can_richedit', $this, 'disable_wyswyg_for_custom_post_types' );
- $this->loader->add_filter( 'manage_tb_table_posts_columns', $this, 'add_tb_table_columns' );
- $this->loader->add_filter( 'page_row_actions', $this, 'duplicate_post_link', 10, 2 );
- $this->loader->add_filter( 'acf/load_value/name=tb_table_operators', $this, 'tb_table_operators_load_value' );
- $this->loader->add_filter( 'acf/load_value/name=tab_operators', $this, 'tab_operators_load_value' );
- }
- function tb_table_operators_load_value( $value, $post_id, $field ) {
- $order = array();
- foreach( $value as $i => $row ) {
- $order[ $i ] = $row['field_595d5fb9be2b5'];
- }
- array_multisort( $order, SORT_DESC, $value );
- return $value;
- }
- function tab_operators_load_value( $value, $post_id, $field ) {
- $order = array();
- foreach( $value as $i => $row ) {
- $order[ $i ] = $row['field_595d605ebe2b8'];
- }
- array_multisort( $order, SORT_DESC, $value );
- return $value;
- }
- /**
- * Register all of the hooks related to the admin area functionality
- * of the plugin.
- *
- * @since 1.0.0
- * @access private
- */
- private function define_admin_hooks() {
- $plugin_admin = new I15_Table_Builder_Admin( $this->get_plugin_name(), $this->get_version() );
- $plugin_custom_css = new I15_Table_Builder_Custom_CSS_Builder( $this->get_plugin_name(), $this->get_version() );
- $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
- $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
- $this->loader->add_action( 'init', $this, 'tb_template_post_type' );
- $this->loader->add_action( 'init', $this, 'tb_table_post_type' );
- $this->loader->add_action( 'admin_menu', $this, 'tb_admin_menu' );
- $this->loader->add_action( 'add_meta_boxes', $this, 'tb_template_register_meta_boxes' );
- $this->loader->add_action( 'manage_tb_table_posts_custom_column', $this, 'tb_table_custom_column', 10, 2 );
- $this->loader->add_action( 'admin_action_duplicate_post_as_draft', $this, 'duplicate_post_as_draft' );
- $this->loader->add_action( 'save_post', $plugin_custom_css, 'build_table_custom_css' );
- $this->loader->add_action( 'acf/save_post', $this, 'write_base_css_file' );
- //$this->loader->add_action( 'save_post', $this, 'log_it_save' );
- }
- function log_it_save( $message ) {
- $this->log_it($_POST);
- $this->log_it('The value for ' . $custom_field . ' is ' . $_POST[$custom_field]);
- }
- function log_it( $message ) {
- if( WP_DEBUG === true ){
- if( is_array( $message ) || is_object( $message ) ){
- error_log( print_r( $message, true ) );
- } else {
- error_log( $message );
- }
- }
- }
- /**
- * Register all of the hooks related to the public-facing functionality
- * of the plugin.
- *
- * @since 1.0.0
- * @access private
- */
- private function define_public_hooks() {
- $plugin_public = new I15_Table_Builder_Public( $this->get_plugin_name(), $this->get_version() );
- $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
- $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
- $this->loader->add_action( 'init', $this, 'tb_template_post_type' );
- $this->loader->add_action( 'init', $this, 'tb_table_post_type' );
- }
- function write_base_css_file() {
- $screen = get_current_screen();
- if (strpos($screen->id, "acf-options-tb-settings") == true) {
- $file_path = plugin_dir_path( dirname( __FILE__ ) ) . 'public/css/i15-table-builder-public.css';
- chmod($file_path, 0777);
- $opened_file = fopen($file_path, "w") or die("Unable to open ".$file_path." file!");
- $base_css = get_field('base_css', 'option');
- fwrite($opened_file, $base_css."\n");
- fclose($opened_file);
- }
- }
- function tb_admin_menu() {
- add_menu_page(
- 'Table Builder',
- 'Table Builder',
- 'read',
- 'i15-table-builder',
- '',
- 'dashicons-screenoptions',
- 21 // Position
- );
- add_submenu_page('i15-table-builder', 'Import', 'Import', 'manage_options', 'tb-import', array($this, 'import_display'));
- add_submenu_page('i15-table-builder', 'Export', 'Export', 'manage_options', 'tb-export', array($this, 'export_display'));
- }
- public function tb_template_post_type() {
- register_post_type(
- 'tb_template',
- array(
- 'labels' => array(
- 'name' => __('Templates'),
- 'singular_name' => __('Template'),
- 'add_new_item' => __('Add New Template'),
- 'edit_item' => __('Edit Template'),
- 'view_item' => __('View Template'),
- 'search_items' => __('Search Templates'),
- 'not_found' => __('No Templates found!'),
- 'not_found_in_trash' => __('No Templates found in trash'),
- 'menu_name' => __('Templates'),
- 'all_items' => __('All Templates')
- ),
- 'description' => 'I15 Table Templates',
- 'public' => false,
- 'show_ui' => true,
- 'menu_position' => 20,
- 'hierarchical' => true,
- 'supports' => array('title', 'editor'),
- 'show_in_menu' => 'i15-table-builder',
- 'can_export' => true,
- 'has_archive' => false,
- 'exclude_from_search' => true
- ));
- }
- function tb_template_register_meta_boxes() {
- add_meta_box('tb_template_meta', "Template Tags", array($this, 'tb_template_tags_callback'), 'tb_template');
- }
- function tb_template_tags_callback($post) {
- foreach($this->tags->get_tags_info() AS $tag => $desc) {
- echo '<p>';
- echo '<h3>'.$tag.'</h3>';
- echo $desc;
- echo '</p>';
- }
- }
- public function tb_table_post_type() {
- register_post_type(
- 'tb_table',
- array(
- 'labels' => array(
- 'name' => __('Tables'),
- 'singular_name' => __('Table'),
- 'add_new_item' => __('Add New Table'),
- 'edit_item' => __('Edit Table'),
- 'view_item' => __('View Table'),
- 'search_items' => __('Search Tables'),
- 'not_found' => __('No Tables found!'),
- 'not_found_in_trash' => __('No Tables found in trash'),
- 'menu_name' => __('Tables'),
- 'all_items' => __('All Tables')
- ),
- 'description' => 'I15 Tables',
- 'public' => false,
- 'show_ui' => true,
- 'menu_position' => 20,
- 'hierarchical' => true,
- 'supports' => array('title'),
- 'show_in_menu' => 'i15-table-builder',
- 'can_export' => true,
- 'has_archive' => false,
- 'exclude_from_search' => true
- ));
- }
- function add_tb_table_columns($columns) {
- unset($columns['date']);
- return array_merge($columns,
- array('shortcode' => __('Shortcode')),
- array('date' => __('Date'))
- );
- }
- function tb_table_custom_column( $column, $post_id ) {
- switch ( $column ) {
- case 'shortcode':
- $operators_objects = get_field('tb_table_operators', $post_id);
- if($operators_objects) {
- echo "[i15-table-builder tableid='".$post_id."']<br />";
- }
- if(have_rows('tab_data', $post_id)) {
- echo "[i15-tabbed-table-builder tableid='".$post_id."']<br />";
- }
- break;
- }
- }
- function disable_wyswyg_for_custom_post_types( $default ){
- global $post;
- if( 'tb_template' === get_post_type($post)) return false;
- return $default;
- }
- function duplicate_post_as_draft(){
- global $wpdb;
- if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
- wp_die('No post to duplicate has been supplied!');
- }
- /*
- * Nonce verification
- */
- if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
- return;
- /*
- * get the original post id
- */
- $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
- /*
- * and all the original post data then
- */
- $post = get_post( $post_id );
- /*
- * if you don't want current user to be the new post author,
- * then change next couple of lines to this: $new_post_author = $post->post_author;
- */
- $current_user = wp_get_current_user();
- $new_post_author = $current_user->ID;
- /*
- * if post data exists, create the post duplicate
- */
- if (isset( $post ) && $post != null) {
- /*
- * new post data array
- */
- $args = array(
- 'comment_status' => $post->comment_status,
- 'ping_status' => $post->ping_status,
- 'post_author' => $new_post_author,
- 'post_content' => $post->post_content,
- 'post_excerpt' => $post->post_excerpt,
- 'post_name' => $post->post_name,
- 'post_parent' => $post->post_parent,
- 'post_password' => $post->post_password,
- 'post_status' => 'draft',
- 'post_title' => "Copy of ".$post->post_title,
- 'post_type' => $post->post_type,
- 'to_ping' => $post->to_ping,
- 'menu_order' => $post->menu_order
- );
- /*
- * insert the post by wp_insert_post() function
- */
- $new_post_id = wp_insert_post( $args );
- /*
- * get all current post terms ad set them to the new post draft
- */
- $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
- foreach ($taxonomies as $taxonomy) {
- $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
- wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
- }
- /*
- * duplicate all post meta just in two SQL queries
- */
- $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
- if (count($post_meta_infos)!=0) {
- $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
- foreach ($post_meta_infos as $meta_info) {
- $meta_key = $meta_info->meta_key;
- if( $meta_key == '_wp_old_slug' ) continue;
- $meta_value = addslashes($meta_info->meta_value);
- $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
- }
- $sql_query.= implode(" UNION ALL ", $sql_query_sel);
- $wpdb->query($sql_query);
- }
- /*
- * finally, redirect to the edit post screen for the new draft
- */
- wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
- exit;
- } else {
- wp_die('Post creation failed, could not find original post: ' . $post_id);
- }
- }
- /*
- * Add the duplicate link to action list for post_row_actions
- */
- function duplicate_post_link( $actions, $post ) {
- if (current_user_can('edit_posts') && ($post->post_type == "tb_template" || $post->post_type == "tb_table")) {
- $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>';
- }
- return $actions;
- }
- function import_display(){
- include('class-i15-table-builder-import.php');
- }
- function export_display(){
- include('class-i15-table-builder-export.php');
- }
- /**
- * Run the loader to execute all of the hooks with WordPress.
- *
- * @since 1.0.0
- */
- public function run() {
- $this->loader->run();
- }
- /**
- * The name of the plugin used to uniquely identify it within the context of
- * WordPress and to define internationalization functionality.
- *
- * @since 1.0.0
- * @return string The name of the plugin.
- */
- public function get_plugin_name() {
- return $this->plugin_name;
- }
- /**
- * The reference to the class that orchestrates the hooks with the plugin.
- *
- * @since 1.0.0
- * @return I15_Table_Builder_Loader Orchestrates the hooks of the plugin.
- */
- public function get_loader() {
- return $this->loader;
- }
- /**
- * Retrieve the version number of the plugin.
- *
- * @since 1.0.0
- * @return string The version number of the plugin.
- */
- public function get_version() {
- return $this->version;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement