PluginProbe
LoftLoader / 1.0.1
LoftLoader v1.0.1
trunk 1.0.0 1.0.1 1.0.2 2.0.0 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.3 2.3.1 2.3.2 2.3.3 All 34 releases
loftloader / settings / class-loftloader-settings.php

class-loftloader-settings.php in LoftLoader 1.0.1, at settings/class-loftloader-settings.php

158 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Not allowed by directly accessing.
3 if(!defined('ABSPATH')){
4 die('Access not allowed!');
5 }
6
7 /**
8 * Main class for plugin option page
9 *
10 * @package LoftLoader
11 * @version 1.0
12 * @link http://www.loftocean.com/
13 * @author Suihai Huang from Loft Ocean Team
14
15 * @since version 1.0
16 */
17
18 if(!class_exists('LoftLoader_Settings')){
19 class LoftLoader_Settings{
20 private $page_id; // Plugin setting page id
21 private $setting_manager; // Settings section to render/save settings
22 public function __construct(){
23 $this->page_id = 'loftloader-settings';
24 $this->setting_manager = new LoftLoader_Setting_Manager();
25
26 add_filter('plugin_action_links_' . LOFTLOADER_NAME, array($this, 'plugin_action_links'));
27 add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
28 add_action('print_media_templates', array($this, 'media_styles')); // Print media styles for media uploader
29 add_action('admin_menu', array($this, 'add_settings_menu')); // Add plugin option menu item
30 add_action('admin_init', array($this, 'register_settings')); // Register setting fields
31 add_action('admin_notices', array($this, 'reset_update'));
32 add_action('wp_ajax_loftloader_reset_settings', array($this, 'reset_settings')); // Reset the plugin option settings
33 }
34 /**
35 * @description add a plugin action link to plugin setting page
36 */
37 public function plugin_action_links($links){
38 $action_links = array(
39 'settings' => '<a href="' . admin_url('options-general.php?page=' . $this->page_id) . '" title="' . esc_attr__('View LoftLoader Settings', 'loftloader') . '">' . esc_html__('Settings', 'loftloader') . '</a>'
40 );
41 return array_merge($action_links, $links);
42 }
43 /**
44 * @description enqueue the style when show media uploader template
45 */
46 public function media_styles(){
47 wp_enqueue_style('loftloader-media-style', LOFTLOADER_CSS_URI . 'settings/loftloader-media.css');
48 }
49 /**
50 * @description register and enqueu scripty and styles
51 */
52 public function enqueue_scripts(){
53 if(isset($_GET['page']) && ($_GET['page'] == $this->page_id)){
54 $js_vars = array(
55 'img_base' => (LOFTLOADER_URI . 'img/'),
56 'fail_text' => esc_html__('Sorry, but the request failed. Please try again later.', 'loftloader'),
57 'confirm_text' => esc_html__('Are you sure want to reset all settings?', 'loftloader'),
58 'ajax' => array(
59 'url' => admin_url('admin-ajax.php'),
60 'action' => 'loftloader_reset_settings',
61 'nonce' => wp_create_nonce('loftloader-reset-settings')
62 )
63 );
64 // Register the scripts and styles
65 wp_register_script('loftloader-setting', LOFTLOADER_JS_URI . 'settings/loftloader-settings.js', array('jquery', 'wp-color-picker', 'jquery-ui-slider'), '1.0');
66 wp_localize_script('loftloader-setting', 'loftloader_vars', $js_vars);
67
68 wp_register_style('loftloader-jqueryui', LOFTLOADER_CSS_URI . 'jquery-ui.css', array(), '1.0');
69 wp_register_style('loftloader-settings', LOFTLOADER_CSS_URI . 'settings/loftloader-settings.css', array('wp-color-picker', 'loftloader-jqueryui'), '1.0');
70
71 // Enqueue the scripts and styles
72 wp_enqueue_media();
73 wp_enqueue_script('loftloader-setting');
74 wp_enqueue_style('loftloader-settings');
75 wp_enqueue_style('loftloader-animation');
76 }
77 }
78 /**
79 * @description add plugin settings menu
80 */
81 public function add_settings_menu(){
82 add_options_page(
83 esc_html__('LoftLoader Settings', 'loftloader'), // Page title on html head
84 esc_html__('LoftLoader', 'loftloader'), // Menu item label
85 'manage_options',
86 $this->page_id,
87 array($this, 'render_settings_page')
88 ); // Register the plugin option subpage
89 }
90 /**
91 * @description register the settings for saving
92 */
93 public function register_settings(){
94 $this->setting_manager->register_setting();
95 }
96 /**
97 * @description get settings content
98 */
99 private function get_settings_content(){
100 ob_start();
101 $this->setting_manager->render_settings_section();
102 $content = ob_get_contents();
103 ob_end_clean();
104 return $content;
105 }
106 /**
107 * @description get settings page template
108 */
109 public function render_settings_page(){
110 $footer = '<div class="panel-footer">' . $this->get_buttons() . '</div>';
111 $content = $this->get_settings_content();
112 $action = admin_url('options.php');
113 $html = <<<ETO
114 <div class="wrap">
115 <div id="loftloader-options-panel" class="loftloader-options-panel">
116 <form method="post" action="$action">
117 <div class="panel-content">
118 $content
119 </div>
120 <!-- panel-content -->
121 $footer
122 </form>
123 </div>
124 </div>
125 ETO;
126 echo $html;
127 }
128 /**
129 * @description get save button
130 * @return save button html
131 */
132 private function get_buttons(){
133 $btns = '<p class="submit"><input type="submit" name="submit" id="submit" class="submit button button-primary" value="' . esc_attr__('Save Changes', 'loftloader') . '"></p>';
134 $btns .= '<p class="reset"><input type="submit" class="button button-primary reset" value="' . esc_attr__('Reset All Settings', 'loftloader') . '"></p>';
135 return $btns;
136 }
137 /**
138 * @description reset loftloader settings
139 */
140 public function reset_settings(){
141 check_ajax_referer('loftloader-reset-settings', 'nonce');
142 $this->setting_manager->reset_settings();
143 update_option('loftloader_settings_reset', 'done');
144 wp_send_json_success();
145 wp_die();
146 }
147 /**
148 * @description show reset confirm message
149 */
150 public function reset_update(){
151 if(get_option('loftloader_settings_reset', '') == 'done'){
152 add_settings_error('loftloader-options', esc_attr('loftloader_settings_reset_notice'), esc_html__('LoftLoader settings have been reset.', 'loftloader'), 'updated');
153 delete_option('loftloader_settings_reset');
154 }
155 }
156 }
157 new LoftLoader_Settings();
158 }