add_shortcode(); $this->table = 'md_metas';// '". $wpdb->prefix ."allinmenu'; $this->version = '1.1.3'; register_activation_hook(__FILE__, array( $this, 'activate' )); //$this->add_scripts(); //$this->add_styles(); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); //add_action( 'plugins_loaded', array( $this, 'init' ), 10 ); add_action( 'wp_footer', array( $this, 'wp_footer') ); // Add Settings js if ( isset( $_GET['page'] ) && $_GET['page'] == 'all-in-settings' ){ add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); } add_action( 'wp_ajax_load_panel_category', array( $this, 'load_panel_category') ); add_action( 'wp_ajax_nopriv_load_panel_category',array( $this, 'load_panel_category') ); add_action( 'wp_ajax_load_panel_post_tag', array( $this, 'load_panel_category') ); add_action( 'wp_ajax_nopriv_load_panel_post_tag',array( $this, 'load_panel_category') ); add_action( 'wp_ajax_load_panel_post', array( $this, 'load_panel_post') ); add_action( 'wp_ajax_nopriv_load_panel_post', array( $this, 'load_panel_post') ); add_action( 'wp_ajax_load_panel_youtube', array( $this, 'load_panel_youtube') ); add_action( 'wp_ajax_nopriv_load_panel_youtube',array( $this, 'load_panel_youtube') ); add_action( 'wp_ajax_load_panel_custom', array( $this, 'load_panel_custom') ); add_action( 'wp_ajax_nopriv_load_panel_custom', array( $this, 'load_panel_custom') ); add_action( 'after_setup_theme', array( $this, 'theme_setup') ); } /**** * Create the table in db to store the menu values * We will create one table to avoid inner joins * */ public function activate(){ global $wpdb; // create the database table if( $wpdb->get_var("show tables like '".$this->table ."'") == null) { require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $sql = "CREATE TABLE `".$this->table."` ( `md_id` bigint(20) NOT NULL AUTO_INCREMENT, `md_ref` bigint(20) NOT NULL, `md_name` varchar(255), `md_values` text, `md_type` tinyint, PRIMARY KEY (`md_id`), UNIQUE KEY `md_id` (`md_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; dbDelta($sql); $sql = "INSERT INTO `".$this->table."` VALUES ('1', '0', 'Home','{\"type\":\"static\",\"title\":\"Home\",\"values\":{}}','1'), ('2', '0', 'Search','{\"type\":\"search\",\"title\":\"Search\",\"values\":{}}','1')"; dbDelta($sql); } } public function theme_setup(){ add_image_size( 'medium_fixed', 300, 300, true ); // 300 pixels wide (and unlimited height) } /** * Add the menu dash shortcode */ public function add_shortcode() { add_shortcode( 'menu_dash', array( $this, 'do_shortcode' ) ); } /** * Do the shortcode when called in theme */ public function do_shortcode( $atts ){ $atts = shortcode_atts( array( 'id' => '0', ), $atts, 'menu_dash' ); $items = $this->get_menu( $atts['id'] ); // If menu doesn't exists $items will be false so nothing will happen if ( $items ){ $this->export_html( $items ); } } /** * Enqueue scripts and styles */ public function enqueue_scripts(){ wp_enqueue_script( 'all_in_js', plugins_url( 'js/all-in.js', __FILE__ ), array('jquery'), null, true ); wp_localize_script( 'all_in_js', 'Ajax', array( 'url' => admin_url('admin-ajax.php') ) ); //wp_enqueue_script( 'scrollbarjs', plugins_url('js/jquery.mCustomScrollbar.min.js', __FILE__), array('jquery'), true ); //wp_enqueue_script( 'mousewheeljs', plugins_url('js/jquery.mousewheel.min.js', __FILE__), array('jquery'), true ); //jquery.mCustomScrollbar.min wp_enqueue_style( 'all_in_css', plugins_url( 'css/all-in.css', __FILE__ ) ); //wp_enqueue_style( 'scrollbarcss', plugins_url( 'css/jquery.mCustomScrollbar.min.css', __FILE__ ) ); } /** * Enqueue scripts and styles in admin settings page */ public function admin_enqueue_scripts(){ wp_enqueue_script( 'tmpljs', plugins_url( 'js/jquery.tmpl.min.js', __FILE__ ) ,array('jquery'), $this->version, true ); wp_enqueue_script( 'jquery-ui-sortable' ); wp_enqueue_script( 'jquery-ui-draggable' ); wp_enqueue_script( 'jquery-ui-droppable' ); wp_enqueue_script( 'jquery-ui-accordion' ); wp_enqueue_script( 'jquery-ui-tabs'); wp_enqueue_script( 'touch_punch', plugins_url( 'js/jquery.ui.touch-punch.min.js', __FILE__ ) ,array('jquery-ui-tabs'), $this->version, true ); wp_enqueue_media(); wp_enqueue_style( 'all_in_settings_css', plugins_url( 'css/all-in-settings.css', __FILE__ ), null, $this->version ); wp_enqueue_script( 'all_in_settings_js', plugins_url( 'js/all-in-settings.js', __FILE__ ), array('jquery'), $this->version, true ); wp_localize_script( 'all_in_settings_js', 'Ajax', array( 'url' => admin_url('admin-ajax.php') ) ); } /** * With the wp_footer function we print the all the necessary script templates * in the footer of every page */ public function wp_footer(){ //$this->export_html_scripts(); } /** * Get the items of the selected menu */ private function get_items( $atts ){ $taxonomies = array( $atts['term'], ); $args = array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => $atts['parent'], 'hierarchical' => true, 'child_of' => 0,//$atts['parent'], 'get' => '', 'name__like' => '', 'description__like' => '', 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' ); $items = get_terms( $taxonomies, $args ); //var_dump( $items ); return $items; } /** * Decode stored values */ public static function decode_values( $items ){ // DECODE VALUES if ( is_array( $items ) ){ foreach ( $items as &$item ){ $item = json_decode( $item ); } } return $items; } /** * Constructing the menu html to be rendered */ private function export_html( $items ){ // Get the stored settings $settings = get_option('allin_settings'); // Checking if cache exists and is not older than the user defined $cache = $this->check_cache( $items->md_id, $settings ); // If cache is false then we must create a new file if ( $cache === false ){ // Check if is sticky or not $sticky = ( isset( $items->md_values->sticky ) && intval($items->md_values->sticky) == 'checked' ) ? 'sticky' : ''; // Get the color theme $color = isset( $items->md_values->color ) ? $items->md_values->color : 'light'; $html = '
'; var_dump( $item ); print ''; $html = ''; // Get the post and the feature image $post = get_post( $item->values->postid ); $image = wp_get_attachment_image_src(get_post_thumbnail_id($item->values->postid), 'medium_fixed' ); if ( $image !== false || ( $item->values->show_excerpt == 'true' && !empty($post->post_excerpt) )){ $html .= ''; } $html .= ''; return $html; } private function menu_expanded_type_youtube( $item ){ $html = ''; if ( !empty( $item->values->header) || !empty( $item->values->description) ){ $html .= ''; } $html .= ''; return $html; } private function menu_expanded_type_custom( $item ){ $html = ''; $total_columns = count( $item->values ); // Define all the template for custom tab $tpl = array( 'holder' => '', ); // Loop the columns foreach ( $item->values as $x => $col){ $col_content = ''; // Loop the tools foreach( $col as $y => $row ){ $type = $row->md_tool_type; //$row = (array)$row; //var_dump( $row ); /*$row_values = array_values( $row ); $row_keys = array_keys($row); foreach( $row_keys as &$value ){ $value = '${'.$value.'}'; } $row = array_combine($row_keys, $row_values); */ // Add each tool html switch ( $type ){ case 'header': $col_content .= ''; break; case 'link': $col_content .= ''; break; case 'paragraph': $col_content .= ''; break; case 'image': $col_content .= ''; break; case 'map': $col_content .= ''; break; case 'youtube': if ( !empty( $row->md_tool_youtube_url )){ $col_content .= ''; } break; case 'html': $col_content .= ''; break; } } $html .= strtr( $tpl['holder'], array( '${column}' => $x, '${total_columns}' => $total_columns, '${content}' => $col_content, )); } return $html; /* console.log(data); var values = data.extra.values; // First of all check how much columns the menu tab contains var columns = values.length; // Foreach column we render content for ( var x in values ){ // Render content foreach md_tool $('#menu_custom_tool_holder').tmpl({ column : x, total_columns : columns, }).appendTo('#menu-expanded-lobby'); for ( var y in values[x] ){ var tool = values[x][y]; $('#menu_custom_tool_'+tool.md_tool_type) .tmpl(tool).appendTo('#menu_custom_holder_'+x+' > .menu_custom_inside'); } } */ } /** * Export search form * */ private function search_form( &$html ){ $html .= ' '; $html .= ''; } /** * Export social links */ private function social_link( &$html, $provider, $link ){ $html .= ''; } /** * Register the ajax calls */ /* public function load_panel_category(){ $response = new stdClass(); $response->error = 0; //$response->term_id = isset( $_POST['term'] ) ? $_POST['term'] : 0; if ( is_array( $_GET ) ){ foreach( $_GET as $key => $value ){ $response->{$key} = $value; } } if ( !empty( $response->extra ) ){ $response->extra = json_decode( stripslashes( $response->extra ) ); } // $this->render_posts( $term_id ); // BIG LOBBY if ( $response->id != 0 ){ $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'meta_query' => array(array('key' => '_thumbnail_id')), 'tax_query' => array( array( 'taxonomy' => $response->type, 'field' => 'term_id', 'terms' => $response->id, ), ), ); if ( !empty( $response->extra->from_term_id )){ $args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $response->extra->from_term_id, ); } $posts = new WP_Query( $args ); if ( $posts->post_count > 0 ){ $response->count = $posts->post_count; $response->posts = $posts->posts; for ( $i = 0; $i < $posts->post_count; $i++ ){ $img = wp_get_attachment_image_src( get_post_thumbnail_id( $response->posts[$i]->ID ), 'medium_fixed' ); $response->posts[$i]->img_src = $img[0]; $response->posts[$i]->permalink = get_permalink( $response->posts[$i]->ID); } } else{ $response->error++; $response->link = get_term_link( intval( $response->id ), $response->type ); wp_send_json( $response ); } // SMALL LOBBY if ( $response->id != 0 && !empty( $response->extra->show_small_thumbs ) ){ $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'meta_query' => array(array('key' => '_thumbnail_id')), 'tax_query' => array( array( 'taxonomy' => $response->type, 'field' => 'term_id', 'terms' => $response->id, ), ), ); $smallposts = new WP_Query( $args ); if ( $smallposts->post_count > 0 ){ $response->count = $smallposts->post_count; $response->smallposts = $smallposts->posts; for ( $i = 0; $i < $smallposts->post_count; $i++ ){ $img = wp_get_attachment_image_src( get_post_thumbnail_id( $response->smallposts[$i]->ID ), 'thumbnail' ); $response->smallposts[$i]->img_src = $img[0]; $response->smallposts[$i]->permalink = get_permalink( $response->smallposts[$i]->ID); } } } // children categories $args = array( 'hide_empty' => false, 'parent' => $response->id, 'child_of' => '',//$response->id, //'childless' => false, //'number' => 6, 'hierarchical' => true, ); $response->terms = get_terms('category', $args); for ( $i = 0; $i < count($response->terms) ; $i++ ){ @$response->terms[$i]->term_link = get_term_link( $response->terms[$i]->term_id ); } } else{ $response->error++; $response->message = 'The term id is not defined'; } wp_send_json( $response ); }*/ /*public function load_panel_post( ){ $response = new stdClass(); $response->error = 0; //$response->term_id = isset( $_POST['term'] ) ? $_POST['term'] : 0; if ( is_array( $_GET ) ){ foreach( $_GET as $key => $value ){ $response->{$key} = $value; } } if ( !empty( $response->extra ) ){ $response->extra = json_decode( stripslashes( $response->extra ) ); } $img = wp_get_attachment_image_src( get_post_thumbnail_id( $response->id ), 'medium_fixed' ); $response->post = get_post( $response->id ); $response->post->image = $img[0]; $response->post->post_content = apply_filters('the_content', $response->post->post_content); wp_send_json( $response ); } public function load_panel_youtube( ){ $response = new stdClass(); $response->error = 0; if ( is_array( $_GET ) ){ foreach( $_GET as $key => $value ){ $response->{$key} = $value; } } if ( !empty( $response->extra ) ){ $response->extra = json_decode( stripslashes( $response->extra ) ); $response->iframe = ''; } wp_send_json( $response ); }*/ /*public function load_panel_custom(){ $response = new stdClass(); $response->error = 0; if ( is_array( $_GET ) ){ foreach( $_GET as $key => $value ){ $response->{$key} = $value; } } if ( !empty( $response->extra ) ){ $response->extra = json_decode( stripslashes( $response->extra ) ); } wp_send_json( $response ); }*/ /** * Retrieve posts from database */ private function retrieve_posts( $term_id ){ $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $term_id, ), ), ); $posts = new WP_Query( $args ); return $posts; } /** * Render posts html */ private function render_posts( $term_id ){ $posts = $this->retrieve_posts( $term_id ); if ( $posts->post_count > 1 ){ foreach ( $posts->posts as $post ){ echo $post->post_title; } } } protected function stored_settings(){ $Settings = get_option( 'menudash_values' ); return $Settings; } protected function get_menu( $md_id ){ //$cache = $this->check_cache( $md_id ); /*if ( $cache ){ $results = json_decode( file_get_contents( $cache ) ); return $results; } else{*/ global $wpdb; // Get the menu itself $query = "SELECT * FROM ". $this->table ." WHERE md_id = '".$md_id."'"; $results = $wpdb->get_results($query); // Check if the query above returned result (if the menu exists) if ( count($results) > 0 ){ $results = array_shift($results); $results->md_values = json_decode( $results->md_values ); // Menu ids concentration of the extracted menu $concetrate_ids = array(); $concetrate_ids = @array_merge( $results->md_values->left, $results->md_values->center, $results->md_values->right ); // $concetrate_ids contains all the ids if ( is_array( $concetrate_ids) && count($concetrate_ids) > 0 ){ foreach ( $concetrate_ids as &$id ){ $id = "md_id = '".$id."'"; } // Form the where clause $where = implode( ' OR ', $concetrate_ids ); $query = "SELECT * FROM ". $this->table ." WHERE ". $where; $items = $wpdb->get_results($query); foreach( $items as $key => &$item ){ $key = $item->md_id; } } // Final formation of the array $aligns = array( 'left', 'center', 'right' ); // Foreach menu alignment we parse the md_values of the menu foreach ( $aligns as $align ){ if ( isset( $results->md_values->{$align} ) && is_array( $results->md_values->{$align} ) ): foreach ( $results->md_values->{$align} as &$value ){ reset( $items ); while ( $value != current( $items )->md_id && current( $items ) !== false ){ if ( next( $items ) === false ){ break; } } if ( current( $items ) ){ $value = current( $items )->md_values;} } endif; } //$this->create_cache( $results, $md_id ); return $results; } else{ return false; } //} } // Check if cache if older than the user's configuration private function check_cache( $md_id, $settings ){ if ( isset($settings['enable']) && $settings['enable'] === 'true' ){ $settings['expiry'] = isset( $settings['expiry'] ) ? 3600 : intval( $settings['expiry'] ); $upload_dir = wp_upload_dir(); // List the directory $ls = glob( $upload_dir['basedir'].'/allincache/md_cache_'.$md_id.'.json' ); // If file exists the result must be single if ( count( $ls ) == 1 ){ // Check the timestamp difference if ( time() - filemtime(current( $ls ) ) < $settings['expiry'] ){ return current( $ls ) ; } else{ return false; } } else{ return false; } } else{ return false; } } private function create_cache( $text, $md_id ){ $upload_dir = wp_upload_dir(); if (! file_exists( $upload_dir['basedir'].'/allincache/' )){ mkdir($upload_dir['basedir'].'/allincache/'); } $cache_file = fopen($upload_dir['basedir'].'/allincache/md_cache_'.$md_id.'.json', "w") or die("Unable to open file!"); //$txt = json_encode( $results, JSON_UNESCAPED_UNICODE ); // PHP >= 5.4 fwrite($cache_file, $text); fclose($cache_file); } /*private function export_html_scripts(){ // SIDEBAR SUBCATEGORIES TMPL echo ''; // SIDEBAR POST TMPL echo ''; // LOBBY TMPL echo ' '; // LOBBY POST TMPL echo ''; // SMALL LOBBY TMPL echo ' '; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; }*/ } endif; All_In_Menu::init();