| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-specific functionality of the plugin. |
| 4 |
* |
| 5 |
* @package tableberg |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Tableberg\Admin; |
| 9 |
|
| 10 |
use Tableberg; |
| 11 |
use Tableberg\Version_Control; |
| 12 |
/** |
| 13 |
* Manage Tableberg Admin |
| 14 |
*/ |
| 15 |
class Tableberg_Admin { |
| 16 |
/** |
| 17 |
* The ID of this plugin. |
| 18 |
* |
| 19 |
* @access private |
| 20 |
* @var string $plugin_name The ID of this plugin. |
| 21 |
*/ |
| 22 |
private $plugin_name; |
| 23 |
|
| 24 |
/** |
| 25 |
* The version of this plugin. |
| 26 |
* |
| 27 |
* @access private |
| 28 |
* @var string $version The current version of this plugin. |
| 29 |
*/ |
| 30 |
private $version; |
| 31 |
/** |
| 32 |
* The PATH of this plugin. |
| 33 |
* |
| 34 |
* @access private |
| 35 |
* @var string $plugin_path The PATH of this plugin. |
| 36 |
*/ |
| 37 |
private $plugin_path; |
| 38 |
|
| 39 |
/** |
| 40 |
* The URL of this plugin. |
| 41 |
* |
| 42 |
* @access private |
| 43 |
* @var string $plugin_url The URL of this plugin. |
| 44 |
*/ |
| 45 |
private $plugin_url; |
| 46 |
|
| 47 |
/** |
| 48 |
* Initialize the class and set its properties. |
| 49 |
*/ |
| 50 |
public function __construct() { |
| 51 |
|
| 52 |
$this->plugin_name = 'tableberg'; |
| 53 |
$this->version = TABLEBERG_VERSION; |
| 54 |
$this->plugin_path = TABLEBERG_DIR_PATH; |
| 55 |
$this->plugin_url = TABLEBERG_URL; |
| 56 |
$this->add_tableberg_admin_hook(); |
| 57 |
|
| 58 |
// initialize version sync manager. |
| 59 |
Tableberg\Version_Sync_Manager::init(); |
| 60 |
|
| 61 |
// initialize version control manager. |
| 62 |
Version_Control::init(); |
| 63 |
|
| 64 |
add_action( 'admin_menu', array( $this, 'register_admin_menus' ) ); |
| 65 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_script' ) ); |
| 66 |
add_filter( 'tableberg/filter/admin_settings_menu_data', array( $this, 'add_settings_menu_data' ), 1, 1 ); |
| 67 |
} |
| 68 |
/** |
| 69 |
* Add hook |
| 70 |
*/ |
| 71 |
public function add_tableberg_admin_hook() { |
| 72 |
add_action( 'wp_ajax_toggle_control', array( $this, 'update_toggle_control' ) ); |
| 73 |
add_action( 'wp_ajax_block_properties', array( $this, 'update_block_properties' ) ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Block properties control |
| 78 |
*/ |
| 79 |
private function update_block_properties() { |
| 80 |
check_ajax_referer( 'block_properties' ); |
| 81 |
|
| 82 |
if ( isset( $_POST['value'] ) && isset( $_POST['property_name'] ) ) { |
| 83 |
$value = sanitize_text_field( wp_unslash( $_POST['value'] ) ); |
| 84 |
$property_name = sanitize_text_field( wp_unslash( $_POST['property_name'] ) ); |
| 85 |
$saved_properties = get_option( 'tableberg_block_properties', false ); |
| 86 |
if ( $saved_properties ) { |
| 87 |
foreach ( $saved_properties as $key => $property ) { |
| 88 |
if ( $property['name'] === $property_name ) { |
| 89 |
$saved_properties[ $key ]['value'] = (int) $value; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
update_option( 'tableberg_block_properties', $saved_properties ); |
| 95 |
} |
| 96 |
|
| 97 |
die(); |
| 98 |
} |
| 99 |
/** |
| 100 |
* Toggle control |
| 101 |
*/ |
| 102 |
private function update_toggle_control() { |
| 103 |
check_ajax_referer( 'toggle_control' ); |
| 104 |
|
| 105 |
if ( isset( $_POST['enable'] ) && isset( $_POST['toggle_name'] ) ) { |
| 106 |
$enable = sanitize_text_field( wp_unslash( $_POST['enable'] ) ); |
| 107 |
$toggle_name = sanitize_text_field( wp_unslash( $_POST['toggle_name'] ) ); |
| 108 |
update_option( $toggle_name, $enable ); |
| 109 |
} |
| 110 |
die(); |
| 111 |
} |
| 112 |
/** |
| 113 |
* Add data for admin settings menu frontend. |
| 114 |
* |
| 115 |
* @param array $data frontend data. |
| 116 |
* |
| 117 |
* @return array filtered frontend data |
| 118 |
*/ |
| 119 |
public function add_settings_menu_data( $data ) { |
| 120 |
$data['assets'] = array( |
| 121 |
'logo' => trailingslashit( $this->plugin_url ) . 'includes/Admin/images/logos/menu-icon-colored.svg', |
| 122 |
); |
| 123 |
|
| 124 |
$data['individual_control'] = array( |
| 125 |
'data' => get_option( 'tableberg_individual_control', false ), |
| 126 |
'ajax' => array( |
| 127 |
'toggleControl' => array( |
| 128 |
'url' => admin_url( 'admin-ajax.php' ), |
| 129 |
'action' => 'toggle_control', |
| 130 |
'nonce' => wp_create_nonce( 'toggle_control' ), |
| 131 |
), |
| 132 |
), |
| 133 |
); |
| 134 |
$data['global_control'] = array( |
| 135 |
'data' => get_option( 'tableberg_global_control', false ), |
| 136 |
'ajax' => array( |
| 137 |
'toggleControl' => array( |
| 138 |
'url' => admin_url( 'admin-ajax.php' ), |
| 139 |
'action' => 'toggle_control', |
| 140 |
'nonce' => wp_create_nonce( 'toggle_control' ), |
| 141 |
), |
| 142 |
), |
| 143 |
); |
| 144 |
$data['block_properties'] = array( |
| 145 |
'data' => get_option( 'tableberg_block_properties', false ), |
| 146 |
'ajax' => array( |
| 147 |
'blockProperties' => array( |
| 148 |
'url' => admin_url( 'admin-ajax.php' ), |
| 149 |
'action' => 'block_properties', |
| 150 |
'nonce' => wp_create_nonce( 'block_properties' ), |
| 151 |
), |
| 152 |
), |
| 153 |
); |
| 154 |
|
| 155 |
$data = array_merge( $data, Tableberg\Utils\Utils::welcome_page() ); |
| 156 |
return $data; |
| 157 |
} |
| 158 |
/** |
| 159 |
* Enqueue admin scripts. |
| 160 |
*/ |
| 161 |
public function enqueue_admin_script() { |
| 162 |
$tableberg_assets = new Tableberg\Assets(); |
| 163 |
$tableberg_assets->register_admin_assets(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Set template for main setting page |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public function main_menu_template_cb() { |
| 172 |
require_once $this->plugin_path . 'includes/Admin/templates/menus/main-menu.php'; |
| 173 |
} |
| 174 |
/** |
| 175 |
* Register Setting Pages for the admin area. |
| 176 |
*/ |
| 177 |
public function register_admin_menus() { |
| 178 |
|
| 179 |
// assign global variables. |
| 180 |
global $menu_page; |
| 181 |
global $menu_page_slug; |
| 182 |
|
| 183 |
$menu_page_slug = 'tableberg-settings'; |
| 184 |
$menu_page = add_menu_page( |
| 185 |
'Tableberg Settings', |
| 186 |
'Tableberg', |
| 187 |
'manage_options', |
| 188 |
$menu_page_slug, |
| 189 |
array( $this, 'main_menu_template_cb' ), |
| 190 |
plugin_dir_url( __FILE__ ) . 'images/logos/menu-icon.svg' |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|