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