prefix . "alm";
// **********************************************
// If table exists
// **********************************************
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
// Updated 2.0.5
// Check column 'name' exists in $wpdb - this is an upgrade checker.
$row = $wpdb->get_col("Show columns from $table_name like 'name'");
if(empty($row)){
$wpdb->query("ALTER TABLE $table_name ADD name TEXT NOT NULL");
$wpdb->update($table_name , array('name' => 'default'), array('id' => 1));
}
// ********
// @TO-DO - Upgrade test, will remove in future versions
// REMOVED - 2.1.3
// ********
$test = $wpdb->get_col("Show columns from $table_name like 'test'");
if(!empty($test)){
$wpdb->query("ALTER TABLE $table_name DROP test");
}
//Add column for repeater template alias
$alias = $wpdb->get_col("Show columns from $table_name like 'alias'");
if(empty($alias)){
$wpdb->query("ALTER TABLE $table_name ADD alias TEXT NOT NULL");
}
// Compare versions of repeaters, if template versions do not match, update the repeater with value from DB
$version = $wpdb->get_var("SELECT pluginVersion FROM $table_name WHERE name = 'default'");
if($version != ALM_VERSION){ // First, make sure versions do not match.
//Write to repeater file
$data = $wpdb->get_var("SELECT repeaterDefault FROM $table_name WHERE name = 'default'");
$f = ALM_PATH. 'core/repeater/default.php'; // File
$o = fopen($f, 'w+') or die(__('Unable to open the default repeater template.', ALM_NAME)); //Open file
$w = fwrite($o, $data) or die(__('Unable to save the default repeater.', ALM_NAME)); //Save the file
$r = fread($o, 100000); //Read it
fclose($o); //now close it
}
}
// **********************************************
// If table DOES NOT exist, create it.
// **********************************************
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$createRepeater = '
';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name text NOT NULL,
repeaterDefault longtext NOT NULL,
pluginVersion text NOT NULL,
UNIQUE KEY id (id)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
//Insert default data in newly created table
$wpdb->insert($table_name , array('name' => 'default', 'repeaterDefault' => $createRepeater, 'pluginVersion' => ALM_VERSION));
}
}
/**
* alm_admin_menu
* Create Admin Menu
*
* @since 2.0.0
*/
add_action( 'admin_menu', 'alm_admin_menu' );
function alm_admin_menu() {
$icon = 'dashicons-plus-alt';
$icon = ALM_ADMIN_URL . "/img/alm-logo-16x16.png";
$alm_page = add_menu_page( 'Ajax Load More', 'Ajax Load More', 'edit_theme_options', 'ajax-load-more', 'alm_settings_page', $icon );
$alm_settings_page = add_submenu_page( 'ajax-load-more', 'Settings', 'Settings', 'edit_theme_options', 'ajax-load-more', 'alm_settings_page');
$alm_template_page = add_submenu_page( 'ajax-load-more', 'Repeater Templates', 'Repeater Templates', 'edit_theme_options', 'ajax-load-more-repeaters', 'alm_repeater_page');
$alm_shortcode_page = add_submenu_page( 'ajax-load-more', 'Shortcode Builder', 'Shortcode Builder', 'edit_theme_options', 'ajax-load-more-shortcode-builder', 'alm_shortcode_builder_page');
$alm_examples_page = add_submenu_page( 'ajax-load-more', 'Examples', 'Examples', 'edit_theme_options', 'ajax-load-more-examples', 'alm_example_page');
$alm_addons_page = add_submenu_page( 'ajax-load-more', 'Add-ons', 'Add-ons', 'edit_theme_options', 'ajax-load-more-add-ons', 'alm_add_ons_page');
if(has_action('alm_cache_installed')){
$alm_cache_page = add_submenu_page( 'ajax-load-more', 'Cache', 'Cache', 'edit_theme_options', 'ajax-load-more-cache', 'alm_cache_page');
add_action( 'load-' . $alm_cache_page, 'alm_load_admin_js' );
add_action( 'load-' . $alm_cache_page, 'alm_load_cache_admin_js' );
}
//Add our admin scripts
add_action( 'load-' . $alm_settings_page, 'alm_load_admin_js' );
add_action( 'load-' . $alm_template_page, 'alm_load_admin_js' );
add_action( 'load-' . $alm_shortcode_page, 'alm_load_admin_js' );
add_action( 'load-' . $alm_examples_page, 'alm_load_admin_js' );
add_action( 'load-' . $alm_addons_page, 'alm_load_admin_js' );
}
/**
* alm_load_admin_js
* Load Admin JS
*
* @since 2.0.15
*/
function alm_load_admin_js(){
add_action( 'admin_enqueue_scripts', 'alm_enqueue_admin_scripts' );
}
function alm_load_cache_admin_js(){
add_action( 'admin_enqueue_scripts', 'alm_enqueue_cache_admin_scripts' );
}
/**
* alm_enqueue_admin_scripts
* Enqueue Admin JS
*
* @since 2.0.15
*/
function alm_enqueue_admin_scripts(){
//Load Admin CSS
wp_enqueue_style( 'alm-admin-css', ALM_ADMIN_URL. 'css/admin.css');
wp_enqueue_style( 'alm-select2-css', ALM_ADMIN_URL. 'css/select2.css');
wp_enqueue_style( 'alm-core-css', ALM_URL. '/core/css/ajax-load-more.css');
wp_enqueue_style( 'alm-font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css');
//Load CodeMirror Syntax Highlighting if on Repater Template page
$screen = get_current_screen();
if ( in_array( $screen->id, array( 'ajax-load-more_page_ajax-load-more-repeaters') ) ){
//CodeMirror CSS
wp_enqueue_style( 'alm-codemirror-css', ALM_ADMIN_URL. 'codemirror/lib/codemirror.css' );
//CodeMirror JS
wp_enqueue_script( 'alm-codemirror', ALM_ADMIN_URL. 'codemirror/lib/codemirror.js');
wp_enqueue_script( 'alm-codemirror-matchbrackets', ALM_ADMIN_URL. 'codemirror/addon/edit/matchbrackets.js' );
wp_enqueue_script( 'alm-codemirror-htmlmixed', ALM_ADMIN_URL. 'codemirror/mode/htmlmixed/htmlmixed.js' );
wp_enqueue_script( 'alm-codemirror-xml', ALM_ADMIN_URL. 'codemirror/mode/xml/xml.js' );
wp_enqueue_script( 'alm-codemirror-javascript', ALM_ADMIN_URL. 'codemirror/mode/javascript/javascript.js' );
wp_enqueue_script( 'alm-codemirror-mode-css', ALM_ADMIN_URL. 'codemirror/mode/css/css.js' );
wp_enqueue_script( 'alm-codemirror-clike', ALM_ADMIN_URL. 'codemirror/mode/clike/clike.js' );
wp_enqueue_script( 'alm-codemirror-php', ALM_ADMIN_URL. 'codemirror/mode/php/php.js' );
}
//Load JS
wp_enqueue_script( 'jquery-form' );
wp_enqueue_script( 'alm-select2', ALM_ADMIN_URL. 'js/libs/select2.min.js', array( 'jquery' ));
wp_enqueue_script( 'alm-drops', ALM_ADMIN_URL. 'js/libs/jquery.drops.js', array( 'jquery' ));
wp_enqueue_script( 'alm-admin', ALM_ADMIN_URL. 'js/admin.js', array( 'jquery' ));
wp_enqueue_script( 'alm-shortcode-builder', ALM_ADMIN_URL. 'shortcode-builder/js/shortcode-builder.js', array( 'jquery' ));
}
function alm_enqueue_cache_admin_scripts(){
wp_enqueue_script( 'alm-cache-admin', ALM_CACHE_URL. '/js/alm-cache.js', array( 'jquery' ));
}
/*
* alm_settings_page
* Settings page
*
* @since 2.0.0
*/
function alm_settings_page(){
include_once( ALM_PATH . 'admin/views/settings.php');
}
/*
* alm_repeater_page
* Custom Repeaters
*
* @since 2.0.0
*/
function alm_repeater_page(){
include_once( ALM_PATH . 'admin/views/repeater-templates.php');
}
/*
* alm_shortcode_builder_page
* Shortcode Builder
*
* @since 2.0.0
*/
function alm_shortcode_builder_page(){
include_once( ALM_PATH . 'admin/views/shortcode-builder.php');
}
/*
* alm_example_page
* Examples Page
*
* @since 2.0.0
*/
function alm_example_page(){
include_once( ALM_PATH . 'admin/views/examples.php');
}
/*
* alm_add_ons_page
* Ajax Load More Add-ons
*
* @since 2.0.0
*/
function alm_add_ons_page(){
include_once( ALM_PATH . 'admin/views/add-ons.php');
}
/*
* alm_cache_page
* Cache Add-on page
*
* @since 2.6.0
*/
function alm_cache_page(){
include_once( ALM_CACHE_PATH . 'admin/views/cache.php');
}
/*
* alm_delete_cache
* Delete induvidual cached items
*
* @return null
* @since 2.6.0
*/
function alm_delete_cache(){
$nonce = $_POST["nonce"];
$cache = $_POST["cache"];
// Check our nonce, if they don't match then bounce!
if (! wp_verify_nonce( $nonce, 'alm_repeater_nonce' ))
die('Get Bounced!');
$dir = ALM_CACHE_PATH .'_cache/'.$cache;
if (is_dir($dir)) {
foreach (glob($dir."/*.*") as $filename) {
if (is_file($filename)) {
unlink($filename);
}
}
rmdir($dir);
}
die();
}
/*
* alm_save_repeater
* Repeater Save function
*
* @return response
* @since 2.0.0
*/
function alm_save_repeater(){
$nonce = $_POST["nonce"];
// Check our nonce, if they don't match then bounce!
if (! wp_verify_nonce( $nonce, 'alm_repeater_nonce' ))
die('Error - unable to verify nonce, please try again.');
// Get _POST Vars
$c = Trim(stripslashes($_POST["value"])); // Repeater Value
$n = Trim(stripslashes($_POST["repeater"])); // Repeater name
$t = Trim(stripslashes($_POST["type"])); // Repeater name
$a = Trim(stripslashes($_POST["alias"])); // Repeater alias
// Write to repeater file
if($t === 'default'){
$f = ALM_PATH. 'core/repeater/'.$n .'.php'; // File
}
elseif($t === 'unlimited'){
$f = ALM_UNLIMITED_PATH. 'repeaters/'.$n .'.php'; // File
}
else{
$f = ALM_REPEATER_PATH. 'repeaters/'.$n .'.php'; // File
}
$o_error = ''. __('Error Opening File', ALM_NAME) .'';
$o_error .= ''. $f .'';
$o_error .= __('Please check your file path and ensure your server is configured to allow Ajax Load More to read and write files within the /ajax-load-more/ plugin directory', ALM_NAME);
$w_error = ''. __('Error Saving File', ALM_NAME) .'';
$w_error .= ''. $f .'';
$w_error .= __('Please check your file path and ensure your server is configured to allow Ajax Load More to read and write files within the /ajax-load-more/ plugin directory', ALM_NAME);
// Open file
$o = fopen($f, 'w+') or die($o_error);
// Save/Write the file
$w = fwrite($o, $c) or die($w_error);
// $r = fread($o, 100000); //Read it
fclose($o); //now close it
//Save to database
global $wpdb;
$table_name = $wpdb->prefix . "alm";
if($t === 'default') {
$data_update = array('repeaterDefault' => "$c", 'pluginVersion' => ALM_VERSION);
$data_where = array('name' => "default");
}
elseif($t === 'unlimited'){ // Unlimited Repeaters
$table_name = $wpdb->prefix . "alm_unlimited";
$data_update = array('repeaterDefault' => "$c", 'alias' => "$a", 'pluginVersion' => ALM_UNLIMITED_VERSION);
$data_where = array('name' => $n);
}
else{ // Custom Repeaters
$data_update = array('repeaterDefault' => "$c", 'alias' => "$a", 'pluginVersion' => ALM_REPEATER_VERSION);
$data_where = array('name' => $n);
}
$wpdb->update($table_name , $data_update, $data_where);
//Our results
if($w){
echo 'Template Saved Successfully';
} else {
echo ''. __('Error Writing File', ALM_NAME) .' Something went wrong and the data could not be saved.';
}
die();
}
/*
* alm_update_repeater
* Update repeater template from database
*
* - User story: User deletes plugin, the installs again and the version has not change - their default repeater will be in the default state and unable to be updated.
*
* @return DB value
* @since 2.5.0
*/
function alm_update_repeater(){
$nonce = $_POST["nonce"];
// Check our nonce, if they don't match then bounce!
if (! wp_verify_nonce( $nonce, 'alm_repeater_nonce' ))
die('Error - unable to verify nonce, please try again.');
// Get _POST Vars
$n = Trim(stripslashes($_POST["repeater"])); // Repeater name
$t = Trim(stripslashes($_POST["type"])); // Repeater type (default | unlimited)
// Get value from database
global $wpdb;
$table_name = $wpdb->prefix . "alm";
if($t === 'default') $n = 'default';
if($t === 'unlimited') $table_name = $wpdb->prefix . "alm_unlimited";
$the_repeater = $wpdb->get_var("SELECT repeaterDefault FROM " . $table_name . " WHERE name = '$n'");
echo $the_repeater; // Return repeater value
die();
}
/*
* alm_get_tax_terms
* Get taxonomy terms for shortcode builder
*
* @return Taxonomy Terms
* @since 2.1.0
*/
function alm_get_tax_terms(){
$nonce = $_GET["nonce"];
// Check our nonce, if they don't match then bounce!
if (! wp_verify_nonce( $nonce, 'alm_repeater_nonce' ))
die('Get Bounced!');
$taxonomy = (isset($_GET['taxonomy'])) ? $_GET['taxonomy'] : '';
$tax_args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false
);
$terms = get_terms($taxonomy, $tax_args);
$returnVal = '';
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
$returnVal .= '