define_constants(); // Hooks add_action( 'init', array( $this, 'init' ) ); add_action( 'init', array( $this, 'register_post_type' ) ); add_action( 'init', array( $this, 'register_shortcode' ) ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'action_links' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'styles' ) ); add_action( 'admin_head', array( $this, 'menu_highlight' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10 ); add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 ); add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) ); add_action( 'wp_ajax_ajax_dropdown', array( $this, 'ajax_callback' ) ); add_action( 'wp_ajax_nopriv_ajax_dropdown', array( $this, 'ajax_callback' ) ); } /** * Define constants */ private function define_constants() { if ( !defined( 'AJAX_DROPDOWNS_VERSION' ) ) define( 'AJAX_DROPDOWNS_VERSION', '0.9' ); if ( !defined( 'AJAX_DROPDOWNS_URL' ) ) define( 'AJAX_DROPDOWNS_URL', plugin_dir_url( __FILE__ ) ); if ( !defined( 'AJAX_DROPDOWNS_DIR' ) ) define( 'AJAX_DROPDOWNS_DIR', plugin_dir_path( __FILE__ ) ); } /** * Init plugin when WordPress Initialises. */ public function init() { // Set up localisation $this->load_plugin_textdomain(); } /** * Load Localisation files. * * Note: the first-loaded translation file overrides any following ones if the same translation is present */ public static function load_plugin_textdomain() { $locale = apply_filters( 'plugin_locale', get_locale(), 'ajaxd' ); // Global + Frontend Locale load_plugin_textdomain( 'ajaxd', false, plugin_basename( dirname( __FILE__ ) . "/languages" ) ); } /** * Show action links on the plugin screen. * * @param mixed $links * @return array */ public function action_links( $links ) { return array_merge( array( '' . __( 'Manage', 'ajaxd' ) . '', ), $links ); } /** * Add menu item */ public static function register_post_type() { register_post_type( 'ajax_dropdown', array( 'labels' => array( 'name' => __( 'Dropdowns', 'ajaxd' ), 'singular_name' => __( 'Dropdown', 'ajaxd' ), 'add_new_item' => __( 'Add New Dropdown', 'ajaxd' ), 'edit_item' => __( 'Edit Dropdown', 'ajaxd' ), 'new_item' => __( 'New Dropdown', 'ajaxd' ), 'view_item' => __( 'View Dropdown', 'ajaxd' ), 'search_items' => __( 'Search Dropdowns', 'ajaxd' ), 'not_found' => __( 'No dropdowns found.', 'ajaxd' ), 'not_found_in_trash' => __( 'No dropdowns found in trash.', 'ajaxd' ), ), 'public' => false, 'show_ui' => true, 'capability_type' => 'post', 'map_meta_cap' => true, 'publicly_queryable' => false, 'exclude_from_search' => true, 'hierarchical' => false, 'rewrite' => array( 'slug' => 'group' ), 'supports' => array( 'title' ), 'has_archive' => false, 'show_in_nav_menus' => false, 'show_in_admin_bar' => false, 'menu_icon' => 'dashicons-randomize', ) ); } /** * Add menu item */ public function register_shortcode() { add_shortcode( 'ajax_dropdown', array( $this, 'shortcode' ) ); } /** * Enqueue styles */ public static function styles() { wp_enqueue_style( 'ajaxd-styles', AJAX_DROPDOWNS_URL . '/assets/css/ajaxd.css', array(), AJAX_DROPDOWNS_VERSION ); } /** * Highlights the correct top level admin menu item for the post type add screen. * * @access public * @return void */ public static function menu_highlight() { global $typenow, $submenu; if ( 'ajax_dropdown' == $typenow ): global $submenu_file; $submenu_file = 'edit.php?post_type=ajax_dropdown'; endif; } /** * Enqueue admin styles */ public static function admin_scripts() { $screen = get_current_screen(); if ( 'ajax_dropdown' == $screen->id ): wp_enqueue_style( 'jquery-chosen', AJAX_DROPDOWNS_URL . '/assets/css/chosen.css', array(), '1.1.0' ); wp_enqueue_style( 'ajaxd-admin', AJAX_DROPDOWNS_URL . '/assets/css/admin.css', array(), AJAX_DROPDOWNS_VERSION ); wp_enqueue_script( 'jquery-ui-sortable' ); wp_enqueue_script( 'chosen', AJAX_DROPDOWNS_URL . '/assets/js/chosen.jquery.min.js', array( 'jquery' ), '1.1.0', true ); wp_enqueue_script( 'ajaxd-admin', AJAX_DROPDOWNS_URL . '/assets/js/ajaxd-admin.js', array( 'jquery', 'jquery-ui-sortable', 'chosen' ), AJAX_DROPDOWNS_VERSION, true ); endif; } /** * Add meta boxes */ public function add_meta_boxes() { add_meta_box( 'ajaxd_postsdiv', __( 'Posts', 'ajaxd' ), array( $this, 'posts_meta_box' ), 'ajax_dropdown', 'advanced', 'high' ); add_meta_box( 'ajaxd_methoddiv', __( 'Method', 'ajaxd' ), array( $this, 'method_meta_box' ), 'ajax_dropdown', 'side', 'default' ); add_meta_box( 'ajaxd_shortcodediv', __( 'Shortcode', 'ajaxd' ), array( $this, 'shortcode_meta_box' ), 'ajax_dropdown', 'side', 'default' ); } /** * Posts meta box */ public static function posts_meta_box( $post, $args ) { $post_types = get_post_types( array( 'public' => true ) ); $ajax_posts = get_post_meta( $post->ID, 'ajax_post' ); ?>
| labels->singular_name; ?> | |||
ID, 'ajaxd_method', true ); $method_options = array( 'ajax' => __( 'Ajax', 'ajaxd' ), 'inline' => __( 'Inline', 'ajaxd' ), 'redirect' => __( 'Redirect', 'ajaxd' ) ); ?>
post_type ) return; // Save delete_post_meta( $post_id, 'ajax_post' ); if ( isset( $_POST['ajax_post'] ) && is_array ( $_POST['ajax_post'] ) ): foreach ( $_POST['ajax_post'] as $value ): add_post_meta( $post_id, 'ajax_post', $value ); endforeach; endif; update_post_meta( $post_id, 'ajaxd_layout', 'dropdown' ); if ( isset( $_POST['ajaxd_method'] ) ) update_post_meta( $post_id, 'ajaxd_method', $_POST['ajaxd_method'] ); } /** * Remove link from post updated messages */ public static function post_updated_messages( $messages ) { global $typenow, $post; if ( 'ajax_dropdown' == $typenow ): for ( $i = 0; $i <= 10; $i++ ): $messages['post'][ $i ] = '' . __( 'Settings saved.', 'ajaxd' ) . ''; endfor; endif; return $messages; } /** * Shortcode content */ public static function shortcode( $atts ) { // Get shortcode attributes if ( ! is_array( $atts ) || ! sizeof( $atts ) ) return; // Get group ID $id = array_shift( $atts ); // Get group post ids $include = get_post_meta( $id, 'ajax_post' ); if ( ! $include || ! is_array( $include ) || ! sizeof( $include ) ) return; // Get current group post if ( isset( $_REQUEST['ajax_post'] ) && in_array( $_REQUEST['ajax_post'], $include ) ) $current = $_REQUEST['ajax_post']; else $current = reset( $include ); // Get method $method = get_post_meta( $id, 'ajaxd_method', true ); /** * Select options */ $select = ''; /** * Select script (defaults to ajax) */ if ( 'inline' == $method ): $script = '$("#ajaxd-select-' . $id . '").change(function(){$("#ajaxd-posts-' . $id . ' #ajaxd-post-"+$(this).val()).show().siblings().hide();});'; elseif ( 'redirect' == $method ): $script = '$("#ajaxd-select-' . $id . '").change(function(){window.location="' . add_query_arg( 'ajax_post', '', remove_query_arg( 'ajax_post', get_permalink() ) ) . '="+$(this).val();});'; else: $script = '$("#ajaxd-select-' . $id . '").change(function(){$.post("' . admin_url('admin-ajax.php') . '",{"action":"ajax_dropdown","post_id":$(this).val()},function(response){if(response!=0){$("#ajaxd-posts-' . $id . '").html(response)};});});'; endif; // Get global $wp_query and hold onto original queried object global $wp_query; $queried_object = $wp_query->queried_object; // Limit posts to current if not inline if ( 'inline' != $method ): $include = array( $current ); endif; // Loop through posts $content = '