table = 'md_metas';// '". $wpdb->prefix ."allinmenu';
// Set the serialize variable;
$this->deprecated_php = version_compare("5.4.0", phpversion()) >= 0 ? true : false ;
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'wp_ajax_allin_save_settings', array( $this, 'save_settings') );
add_action( 'wp_ajax_allin_search_posts', array( $this, 'search_posts' ) );
add_action( 'wp_ajax_allin_save_options', array( $this, 'save_options' ) );
add_action( 'wp_ajax_allin_flush_cache', array( $this, 'flush_cache' ) );
add_action( 'wp_ajax_md_add_menu', array( $this, 'add_menu') );
add_action( 'wp_ajax_md_get_menus', array( $this, 'get_menus') );
add_action( 'wp_ajax_md_add_component', array( $this, 'add_component') );
add_action( 'wp_ajax_md_delete_component', array( $this, 'delete_component') );
add_action( 'wp_ajax_md_update_component', array( $this, 'update_component') );
add_action( 'wp_ajax_md_get_components', array( $this, 'get_components') );
add_action( 'wp_ajax_md_update_menu', array( $this, 'update_menu') );
add_action( 'wp_ajax_md_get_items', array( $this, 'get_items') );
}
//RENAME TABLE `md_metas` TO `$wpdb->prefix .allinmenu`;
/****
* ADMINISTRATION PAGE
* This page contains all the function to create and edit menus and tabs
*/
public function admin_menu () {
add_menu_page( 'All-In-Menu Settings', 'All-In-Menu Settings', 'manage_options', 'all-in-settings', array( $this, 'all_in_settings'), plugins_url( 'images/page_icon.jpg', __FILE__ ) );
}
/****
* START RENDER SETTINGS HTML
*/
public function all_in_settings () {
// Load the stored settings
$html = '
';
// The main menu of the setting page
$this->settings_side_menu( $html );
// General Settings for Menu Dashboard
$this->general_settings( $html );
// Export the tab for menus management
$this->manage_menus( $html );
// Export the tab for tabs (components) management
$this->manage_components( $html );
// The popup editor
$this->edit_popup( $html );
// Add the html indicators ( Loader + Message box );
$this->html_indicators( $html );
// The script templates used in the administration page
$html .= $this->script_templates();
$html .= '
';
// Echos the HTML from the previous functionality.
// This is the UNIQUE echo in the page
echo $html;
}
/****
* MAIN MENU OF THE SETTING PAGE
* Contains the main menu of the settings page shown as left sidebar
* asychronous altering the interface between the following states:
* GENERAL : Settings about the plugin
* MENUS : Menus management (create, edit, delete)
* TABS : The tabs are the menu components. At least one tab must be created
* in order to connect it one already created menu.
*/
protected function settings_side_menu( &$html ){
$html .= '
';
}
/****
* GENERAL SETTING
* This function contains the html for the general settings. These are:
*
*/
protected function general_settings( &$html ){
// Get already submitted options
$settings = get_option('allin_settings');
$html .= '
General Settings
Settings for all menus
';
$html .= '';
// Enable cache checkbox
$enabled = $settings['enable'] == 'true' ? 'checked="checked"' : '';
$html .= '';
// Expiry cache select option
$expiry = $settings['expiry'];
$expiry_values = array(
'3600' => '1 hour',
'7200' => '2 hours',
'21600' => '6 hours',
'86400' => '1 day',
'172800' => '2 days',
'604800' => '1 week',
'315360000' => 'never (until flush cache)',
);
$html .= '';
$html .= '
Save Settings
Flush Cache
';
$html .= '
';
$html .= ' ';
}
/****
* MENUS MANAGEMENT
* This function has three main parts.
* - Add new menu
* - Available tabs (components)
* - Menus List
*/
protected function manage_menus( &$html ){
$html .= '';
}
/****
* TABS (COMPONENTS) MANAGEMENT
* The tabs (components) are parts of a menu. One menu can have one or more tabs
* and a tab can be part of more than one menu. One menu is a unordered list ()
* and a tab is one list item (). There are several tab types that the user can
* create and this can be done in the following interface.
*/
protected function manage_components( &$html ){
$html .= '
Tabs (Components)
Create Tabs for your menus
';
$html .= '';
$html .= '
Link
Category
Tag
Post
Social Link
Youtube
Custom
✚ Add New Tab
';
// The container hosting each component submission form
$html .= '
';
// Create the placeholders
$html .= '
';
$html .= $this->placeholder_toolbar();
$html .= '
';
// Show the already submitted components
$html .= '
';
$html .= '
'; // END OF div.md_settings_inside
$html .= ' ';
}
/****
* POPUP EDITOR HTML
* This function returns the html for the editor popup editor
* Editor popup is an empty div with a submit button filled with
* the appropriate form template (and values) for editing
*/
protected function edit_popup( &$html ){
$html .= '';
}
/****
* HTML INDICATORS
* The function ontains and export two empty divs.
* The first one is the md_loader div which is an full page overlay visible
* when ajax requests are in progress and the second is the message indicator is
* a position absolute div, visible when a message to the user must be shown.
*/
protected function html_indicators( &$html ){
$html .= '
';
}
/****
* RETURNS THE TERMS OF A TAXONOMY
* This function returns an array from the get_terms() wordpress function
*/
protected function get_taxonomy( $type ){
$taxonomies = array(
$type,
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'number' => '',
'fields' => 'all',
'slug' => '',
'parent' => 0,
'hierarchical' => true,
'child_of' => '',//$atts['parent'],
'get' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'offset' => '',
'search' => '',
'cache_domain' => 'core'
);
$items = get_terms( $taxonomies, $args );
return $items;
}
/****
* THE TOOLBAR IN THE CUSTOM MENU TAB SECTION
* This function contains the html with the tools available in the creation
* of the custom menu tab (Tabs Section).
*/
protected function placeholder_toolbar(){
$html = '';
return $html;
}
/****
* CREATE A SELECT HTML TAG
* The function accepts three parameters and creates a select html tag:
* The first one is an array containing the total tag that the final
* will have available. The second is the name attribute of the
* select tag and the third (optional) is an array with the data-values attributes
* of the select tag. The following parameters for example:
* $terms = array( 'example' );
* $data = array(
* 'need' => 'required',
* 'id' => 27,
* );
* create_term_select_tag( $terms, 'the_selected_name', $data )
* will create the following select tag:
*
* example
*
*/
protected function create_term_select_tag( $terms, $name, $data = array() ){
foreach ( $data as $k => &$d ){
$d = 'data-'.$k.'="'.$d.'"';
}
$html = '';
$html .= '-- No select -- ';
foreach ( $terms as $term ){
$html .= ''.$term->name.' ';
}
$html .= ' ';
return $html;
}
/****
* CREATE AN INPUT HTML TAG
* The function accepts three parameters and creates an input html tag:
* The first one is the type of input produced input tag (e.x. hidden). The second
* is the name attribute of the tag and the third the data-values attributes.
* The following parameters for example:
* $type = 'text';
* $data = array(
* 'need' => 'required',
* 'id' => 27,
* );
* create_input_tag( $type, 'the_selected_name', $data )
* will create the following input tag:
*
*/
protected function create_input_tag( $type, $name, $data = array(), $label = '', $class = '', $placeholder = '' ){
// Looping the $data array converting values to data-key="value" format
foreach ( $data as $k => &$d ){
$d = 'data-'.$k.'="'.$d.'"';
}
// Create a unique id for the input
$id = $type .'_'. $name .'_'. rand( 1, 99999999);
// Generate the html with the input tag
$html = ' ';
// If label is set then add the tag
if ( $label ){
$html .= ''.$label.' ';
}
$html .= ' ';
return $html;
}
protected function create_textarea_tag( $name, $data = array() ){
// Looping the $data array converting values to data-key="value" format
foreach ( $data as $k => &$d ){
$d = 'data-'.$k.'="'.$d.'"';
}
// Create a unique id for the input
$id = $name .'_'. rand( 1, 99999999);
$html = '';
$html .= ' ';
return $html;
}
/**** -------------------------------------------------------------
AJAX RESPONSES
Every ajax response returns with wp_send_json($response) all the
asynchonous data contained in the response object. The response->error
is always initializing with the value 0. If any error occurs the
$response->error value becomes 1 and a $response->msg is set with
the appropriate value so we can manage the javascript more easily
-------------------------------------------------------------*/
public function save_settings(){
$response = new stdClass();
$response->error = 0;
// Collecting $_POST data
if ( isset( $_POST ) && is_array( $_POST )){
foreach ( $_POST as $key => $value ){
$response->{$key} = $value;
}
}
update_option( 'allin_settings', $response->settings );
wp_send_json($response);
}
/****
* SEARCH FOR POST BY STRING
* The function collects the search string from $.ajax data object
* and makes a WP_Query with s=[search_string] parameter
*/
public function search_posts(){
$response = new stdClass();
$response->error = 0;
// Collecting $_GET data
if ( isset( $_GET ) && is_array( $_GET )){
foreach ( $_GET as $key => $value ){
$response->{$key} = $value;
}
}
// Making the database search
$posts = new WP_Query( 's='. $response->s );
// Check if no posts returned and create the appropriate message
if ( $posts->post_count == 0 ){
$response->error++; // Increment the error
$response->msg = 'No posts returned'; // Set response message
wp_send_json($response);
}
else{
$response->posts = $posts->posts;
wp_send_json($response);
}
}
public function save_options(){
$response = new stdClass();
$response->error = 0;
// Collecting $_POST data
if ( isset( $_POST ) && is_array( $_POST )){
foreach ( $_POST as $key => $value ){
$response->{$key} = $value;
}
}
if ( ! update_option( 'menudash_values', $response->values ) ){
$response->error++; // Increment the error
$response->msg = 'No posts returned'; // Set response message
}
wp_send_json($response);
}
public function add_menu(){
global $wpdb;
$response = new stdClass();
$response->error = 0;
if ( isset( $_POST ) && is_array( $_POST )){
foreach ( $_POST as $key => $value ){
$response->{$key} = $value;
}
}
// The default empty values json is the following
$jsonValues = '{"left":[],"center":[],"right":[],"color":"light","sticky":"","max_width":""}';
$query = "INSERT INTO ". $this->table ." ( md_ref, md_name, md_values, md_type )
VALUES ( '0', '".$response->name."', '".$jsonValues."', '0')";
$response->result = $wpdb->query( $query );
if ( $response->result == 0 ){
$response->error++;
}
wp_send_json($response);
}
public function update_menu(){
global $wpdb;
$response = new stdClass();
$response->error = 0;
if ( isset( $_POST ) && is_array( $_POST )){
foreach ( $_POST as $key => $value ){
$response->{$key} = $value;
}
}
$query = "UPDATE ". $this->table ."
SET md_name = '".strip_tags($response->name)."',
md_values = '".$response->values."'
WHERE md_id = ".$response->menu_id;
$response->result = $wpdb->query( $query );
wp_send_json($response);
}
public function flush_cache(){
$response = new stdClass();
$response->error = 0;
if ( isset( $_POST ) && is_array( $_POST )){
foreach ( $_POST as $key => $value ){
$response->{$key} = $value;
}
}
$upload_dir = wp_upload_dir();
$response->files = glob( $upload_dir['basedir'].'/allincache/*' ); // get all file names
foreach($response->files as $file){ // iterate files
if(is_file($file)){ unlink($file); }// delete file
}
wp_send_json($response);
}
public function get_menus(){
global $wpdb;
$response = new stdClass();
$response->error = 0;
if ( isset( $_GET ) && is_array( $_GET )){
foreach ( $_GET as $key => $value ){
$response->{$key} = $value;
}
}
$query = "SELECT * FROM ". $this->table ." WHERE md_type = '0' ORDER BY md_id DESC";
$response->items = $wpdb->get_results( $query );
wp_send_json($response);
}
public function serialize_menu( &$value ){
global $wpdb;
$sections = json_decode( $value->md_values );
$align = array( 'left', 'center', 'right' );
foreach ( $align as $a ){
foreach ( $sections->{$a} as $menu ){
$query = "SELECT * FROM ". $this->table ." WHERE md_id = '".$menu."'";
$results = $wpdb->get_results( $query );
$results = array_shift( $results );
var_dump( $results );
}
//$value->md_values =
}
}
/**** -------------------------------------------------------------
AJAX response for save the menu tabs (components)
-------------------------------------------------------------*/
public function add_component(){
global $wpdb;
$response = new stdClass();
$response->error = 0;
if ( isset( $_POST ) && is_array( $_POST )){
foreach ( $_POST as $key => $value ){
$response->{$key} = $value;
}
}
// Checking the PHP version in order to use the JSON_UNESCAPED_UNICODE
// Must be greater than 5.4
if ( version_compare(PHP_VERSION, '5.4') >= 0 ){
$values = json_encode( $response->values, JSON_UNESCAPED_UNICODE );
}
else{
$values = json_encode( $response->values );
}
// Ready to submit the new values to the db
// Check first if we must update or insert the row
if ( empty( $response->menu_id ) ){
$query = "INSERT INTO ". $this->table ." ( md_ref, md_name, md_values, md_type )
VALUES ( '0', '".$response->name."', '".$values."', '1')";
}
else{
$query = "UPDATE ". $this->table ." SET
md_name = '".$response->name."',
md_values = '".$values."'
WHERE md_id = '".$response->menu_id."'";
}
$response->result = $wpdb->query( $query );
wp_send_json($response);
}
public function delete_component(){
global $wpdb;
$response = new stdClass();
$response->error = 0;
if ( isset( $_POST ) && is_array( $_POST )){
foreach ( $_POST as $key => $value ){
$response->{$key} = $value;
}
}
$query = "DELETE FROM ". $this->table ."
WHERE md_id = ".$response->menu_id;
$response->result = $wpdb->query( $query );
if ( $response->result == 0 ){
$response->error++;
}
wp_send_json($response);
}
public function update_component(){
global $wpdb;
$response = new stdClass();
$response->error = 0;
if ( isset( $_POST ) && is_array( $_POST )){
foreach ( $_POST as $key => $value ){
$response->{$key} = $value;
}
}
$query = "UPDATE ". $this->table ."
SET md_values = '".$response->values."'
WHERE md_id = ".$response->menu_id;
$response->result = $wpdb->query( $query );
/*if ( $response->result == 0 ){
$response->error++;
}*/
wp_send_json($response);
}
public function get_components(){
global $wpdb;
$response = new stdClass();
$response->error = 0;
if ( isset( $_GET ) && is_array( $_GET )){
foreach ( $_GET as $key => $value ){
$response->{$key} = $value;
}
}
$query = "SELECT * FROM ". $this->table ." WHERE md_type = 1 ORDER BY md_id DESC";
$response->items = $wpdb->get_results( $query );
foreach ( $response->items as &$value ){
if ( !empty($value->md_values)){
$value->md_values = json_decode( $value->md_values );
}
}
wp_send_json($response);
}
/****
* GET ITEMS ( MENUS & TABS )
* The function returns the submitted menus and tabs in the ajax request
* It is used when the page first loads but also when a new menu/tab is created
* or deleted
*/
public function get_items(){
global $wpdb;
$response = new stdClass();
$response->error = 0;
// Collecting $_GET data
if ( isset( $_GET ) && is_array( $_GET )){
foreach ( $_GET as $key => $value ){
$response->{$key} = $value;
}
}
// The select query is really simple without any filter
$query = "SELECT * FROM ". $this->table ." ORDER BY md_id DESC";
// Executing the query
$results = $wpdb->get_results( $query );
// Initializing the arrays.
// $response->menus for menus
// $response->tabs for menu tabs
$response->tabs = $response->menus = array();
// Start looping the query results
foreach ( $results as $result ){
$result->md_values = json_decode( $result->md_values );
if ( $result->md_type == 0 ){
array_push( $response->menus, $result );
}
else{
array_push( $response->tabs, $result );
}
}
wp_send_json($response);
}
// Include all the script templates needed for the administration page
public function script_templates(){
// Get categories and tags to use them inside components creation
$Categories = $this->get_taxonomy('category');
$Tags = $this->get_taxonomy('post_tag');
/* -----------------------------
MENU ITEMS
SCRIPT TEMPLATES
----------------------------- */
/* The template for the the list of the submitted menus
* Contains all the properties of the menu
* MENU PROPERTIES
* ------------------------------------------
* Menu Name : Menu name (Only for administrative reasons)
* Items Order : The alignment of the menu items (left, center, right)
* Colors : The color theme of the menu
* More Options :
*/
$html = '';
// List of menu components to add in the menus
$html .= '';
/* -----------------------------
MENU TABS
SCRIPT TEMPLATES
----------------------------- */
/* The template for static simple links
* CATEGORY PROPERTIES
* Title -> The title of the new menu item
* Link url -> The url where the link target to.
*/
$html .= '';
/* The template for category component form
* CATEGORY PROPERTIES
* Title -> The title of the new menu item
* Main Category -> The category (parent=0) where the posts be load from
* Tag -> Show category posts of a specific tag
* Small Thumbs -> Shows the latest posts the selected category in small thumbs
*/
$html .= '';
/* The template for tag component form
* TAG PROPERTIES
* Title -> The title of the new menu item
* Tag -> Show posts filtered by the selected tag
*/
$html .= '';
/* The template for post component form
* POST PROPERTIES
* Title -> The title of the new menu item
* Post -> Select the post to show
* Show Title -> Show the post title
* Show Excerpt -> Show the post excerpt
*/
$html .= '';
/* The template for social component form
* SOCIAL PROPERTIES
* Social Provider -> The social provider such as Facebook
* Url -> The social url target
*/
$html .= '';
/* The template for youtube component form
* YOUTUBE PROPERTIES
* Youtube url -> Enter the youtube url or id
* Autoplay -> Autoplay the video when the menu opens
* Show controls -> Show the youtube controls (play, progress bar )
*/
$html .= '';
// THE TEMPLATE FOR THE MENU TABS LIST
$html .= '';
// The template for the post autocomplete
$html .= '';
/* -----------------------------
MENU TOOLS ( FOR CUSTOM MENU TABS )
SCRIPT TEMPLATES
----------------------------- */
/* The script pattern templates
* ------------------------------------------
* id = md_tool_[TOOL NAME]
*
* AVAILABLE MENU TOOLS
* ------------------------------------------
* Header : An h3 header text
* Link : A two row text which can be used as link
* Paragraph : A simple text paragraph
* Image : Enter an image into custom tab
* Map : Submit an adress to show it in a map
* Youtube : A youtube video can be also added in custom tab
*/
// The placeholder containers (columns) for the tools
$html .= '';
// The template for the header tool
$html .= '';
$html .= '';
$html .= '';
$html .= '';
// MAP TOOL
$html .= '';
$html .= '';
$html .= '';
return $html;
}
}
endif;
All_In_Menu_Settings::init();