base
8 years ago
class-wpfront-scroll-top-options.php
8 years ago
class-wpfront-scroll-top.php
8 years ago
class-wpfront-scroll-top.php
479 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | WPFront Scroll Top Plugin |
| 5 | Copyright (C) 2013, WPFront.com |
| 6 | Website: wpfront.com |
| 7 | Contact: syam@wpfront.com |
| 8 | |
| 9 | WPFront Scroll Top Plugin is distributed under the GNU General Public License, Version 3, |
| 10 | June 2007. Copyright (C) 2007 Free Software Foundation, Inc., 51 Franklin |
| 11 | St, Fifth Floor, Boston, MA 02110, USA |
| 12 | |
| 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 20 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | namespace WPFront\Scroll_Top; |
| 26 | |
| 27 | require_once("class-wpfront-scroll-top-options.php"); |
| 28 | |
| 29 | /** |
| 30 | * Main class of WPFront Scroll Top plugin |
| 31 | * |
| 32 | * @author Syam Mohan <syam@wpfront.com> |
| 33 | * @copyright 2013 WPFront.com |
| 34 | */ |
| 35 | class WPFront_Scroll_Top { |
| 36 | //Constants |
| 37 | const VERSION = '2.0.1'; |
| 38 | const OPTIONS_GROUP_NAME = 'wpfront-scroll-top-options-group'; |
| 39 | const OPTION_NAME = 'wpfront-scroll-top-options'; |
| 40 | const PLUGIN_SLUG = 'wpfront-scroll-top'; |
| 41 | const PLUGIN_FILE = 'wpfront-scroll-top/wpfront-scroll-top.php'; |
| 42 | |
| 43 | //Variables |
| 44 | protected $iconsDIR = '/tmp/icons/'; |
| 45 | protected $iconsURL = '//tmp/icons/'; |
| 46 | protected $pluginDIRRoot = '/tmp/'; |
| 47 | protected $pluginURLRoot = '//tmp/'; |
| 48 | protected $options; |
| 49 | protected $markupLoaded; |
| 50 | protected $scriptLoaded; |
| 51 | protected $min_file_suffix; |
| 52 | |
| 53 | private static $instance = null; |
| 54 | |
| 55 | protected function __construct() { |
| 56 | $this->markupLoaded = FALSE; |
| 57 | $this->min_file_suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min'; |
| 58 | } |
| 59 | |
| 60 | public static function Instance() { |
| 61 | if(empty(self::$instance)) { |
| 62 | self::$instance = new WPFront_Scroll_Top(); |
| 63 | } |
| 64 | |
| 65 | return self::$instance; |
| 66 | } |
| 67 | |
| 68 | public function init($pluginFile = null) { |
| 69 | //Root variables |
| 70 | $this->pluginURLRoot = plugins_url() . '/' . self::PLUGIN_SLUG . '/'; |
| 71 | $this->iconsURL = $this->pluginURLRoot . 'images/icons/'; |
| 72 | $this->pluginDIRRoot = plugin_dir_path($pluginFile); |
| 73 | $this->iconsDIR = $this->pluginDIRRoot . 'images/icons/'; |
| 74 | |
| 75 | add_action('plugins_loaded', array($this, 'plugins_loaded')); |
| 76 | |
| 77 | add_action('wp_footer', array($this, 'write_markup')); |
| 78 | add_action('shutdown', array($this, 'shutdown_callback')); |
| 79 | |
| 80 | $this->add_activation_redirect(); |
| 81 | |
| 82 | if (is_admin()) { |
| 83 | add_action('admin_init', array($this, 'admin_init')); |
| 84 | add_action('admin_menu', array($this, 'admin_menu')); |
| 85 | add_filter('plugin_action_links', array($this, 'action_links'), 10, 2); |
| 86 | } else { |
| 87 | add_action('wp_enqueue_scripts', array($this, 'enqueue_styles')); |
| 88 | add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts')); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public function action_links($links, $file) { |
| 93 | if ($file == self::PLUGIN_FILE) { |
| 94 | $settings_link = '<a href="' . menu_page_url(self::PLUGIN_SLUG, FALSE) . '">' . __('Settings', 'wpfront-scroll-top') . '</a>'; |
| 95 | array_unshift($links, $settings_link); |
| 96 | } |
| 97 | return $links; |
| 98 | } |
| 99 | |
| 100 | protected function add_activation_redirect() { |
| 101 | add_action('activated_plugin', array($this, 'activated_plugin_callback')); |
| 102 | add_action('admin_init', array($this, 'admin_init_callback'), 999999); |
| 103 | } |
| 104 | |
| 105 | public function activated_plugin_callback($plugin) { |
| 106 | if ($plugin !== self::PLUGIN_FILE) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if (is_network_admin() || isset($_GET['activate-multi'])) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | $key = self::PLUGIN_SLUG . '-activation-redirect'; |
| 115 | add_option($key, TRUE); |
| 116 | } |
| 117 | |
| 118 | public function admin_init_callback() { |
| 119 | $key = self::PLUGIN_SLUG . '-activation-redirect'; |
| 120 | |
| 121 | if (get_option($key, FALSE)) { |
| 122 | delete_option($key); |
| 123 | |
| 124 | if (is_network_admin() || isset($_GET['activate-multi'])) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | wp_safe_redirect(menu_page_url(self::PLUGIN_SLUG, FALSE)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | //add scripts |
| 133 | public function enqueue_scripts() { |
| 134 | if ($this->enabled() == FALSE) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | $jsRoot = $this->pluginURLRoot . 'js/'; |
| 139 | |
| 140 | wp_enqueue_script('jquery'); |
| 141 | wp_enqueue_script('wpfront-scroll-top', $jsRoot . 'wpfront-scroll-top' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION, TRUE); |
| 142 | |
| 143 | $this->scriptLoaded = TRUE; |
| 144 | } |
| 145 | |
| 146 | //add styles |
| 147 | public function enqueue_styles() { |
| 148 | if ($this->enabled() == FALSE) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $cssRoot = $this->pluginURLRoot . 'css/'; |
| 153 | |
| 154 | wp_enqueue_style('wpfront-scroll-top', $cssRoot . 'wpfront-scroll-top' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 155 | |
| 156 | if($this->options->button_style() == 'font-awesome') { |
| 157 | if(!$this->options->fa_button_exclude_URL() || is_admin()) { |
| 158 | $url = trim($this->options->fa_button_URL()); |
| 159 | $ver = FALSE; |
| 160 | if(empty($url)) { |
| 161 | $url = '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'; |
| 162 | $ver = '4.7.0'; |
| 163 | } |
| 164 | wp_enqueue_style('font-awesome', $url, array(), $ver); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public function admin_init() { |
| 170 | register_setting(self::OPTIONS_GROUP_NAME, self::OPTION_NAME); |
| 171 | |
| 172 | $this->enqueue_styles(); |
| 173 | $this->enqueue_scripts(); |
| 174 | } |
| 175 | |
| 176 | public function admin_menu() { |
| 177 | $page_hook_suffix = add_options_page(__('WPFront Scroll Top', 'wpfront-scroll-top'), __('Scroll Top', 'wpfront-scroll-top'), 'manage_options', self::PLUGIN_SLUG, array($this, 'options_page')); |
| 178 | |
| 179 | add_action('admin_print_scripts-' . $page_hook_suffix, array($this, 'enqueue_options_scripts')); |
| 180 | add_action('admin_print_styles-' . $page_hook_suffix, array($this, 'enqueue_options_styles')); |
| 181 | } |
| 182 | |
| 183 | public function enqueue_options_scripts() { |
| 184 | wp_enqueue_media(); |
| 185 | |
| 186 | $this->enqueue_scripts(); |
| 187 | |
| 188 | $jsRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/js/'; |
| 189 | wp_enqueue_script('jquery.eyecon.colorpicker', $jsRoot . 'colorpicker' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION); |
| 190 | |
| 191 | $jsRoot = $this->pluginURLRoot . 'js/'; |
| 192 | wp_enqueue_script('wpfront-scroll-top-options', $jsRoot . 'options' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION); |
| 193 | } |
| 194 | |
| 195 | //options page styles |
| 196 | public function enqueue_options_styles() { |
| 197 | $styleRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/css/'; |
| 198 | wp_enqueue_style('jquery.eyecon.colorpicker', $styleRoot . 'colorpicker' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 199 | |
| 200 | $styleRoot = $this->pluginURLRoot . 'css/'; |
| 201 | wp_enqueue_style('wpfront-scroll-top-options', $styleRoot . 'options' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 202 | } |
| 203 | |
| 204 | public function set_options($options) { |
| 205 | $this->options = $options; |
| 206 | } |
| 207 | |
| 208 | public function plugins_loaded() { |
| 209 | //load plugin options |
| 210 | $this->set_options(new WPFront_Scroll_Top_Options(self::OPTION_NAME, self::PLUGIN_SLUG)); |
| 211 | |
| 212 | if($this->options->javascript_async()) { |
| 213 | add_filter('script_loader_tag', array($this, 'script_loader_tag'), 999999, 3); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | public function script_loader_tag($tag, $handle, $src) { |
| 218 | if($handle === 'wpfront-scroll-top') { |
| 219 | return '<script type="text/javascript" src="' . $src . '" async="async" defer="defer"></script>' . "\n"; |
| 220 | } |
| 221 | |
| 222 | return $tag; |
| 223 | } |
| 224 | |
| 225 | public function shutdown_callback() { |
| 226 | if ($this->markupLoaded) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | $headers = headers_list(); |
| 231 | $flag = FALSE; |
| 232 | foreach ($headers as $value) { |
| 233 | $value = strtolower(str_replace(' ', '', $value)); |
| 234 | if (strpos($value, 'content-type:text/html') === 0) { |
| 235 | $flag = TRUE; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if ($flag) { |
| 241 | $this->write_markup(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | //writes the html and script for the button |
| 246 | public function write_markup() { |
| 247 | if ($this->markupLoaded) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | if (!$this->scriptLoaded) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | if ($this->doing_ajax()) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | if ($this->enabled()) { |
| 260 | if(is_admin()) { |
| 261 | $this->options->set_button_action('top'); |
| 262 | } |
| 263 | |
| 264 | include($this->pluginDIRRoot . 'templates/scroll-top-template.php'); |
| 265 | |
| 266 | echo '<script type="text/javascript">'; |
| 267 | echo 'function wpfront_scroll_top_init() {'; |
| 268 | echo 'if(typeof wpfront_scroll_top == "function" && typeof jQuery !== "undefined") {'; |
| 269 | echo 'wpfront_scroll_top(' . json_encode(array( |
| 270 | 'scroll_offset' => $this->options->scroll_offset(), |
| 271 | 'button_width' => $this->options->button_width(), |
| 272 | 'button_height' => $this->options->button_height(), |
| 273 | 'button_opacity' => $this->options->button_opacity() / 100, |
| 274 | 'button_fade_duration' => $this->options->button_fade_duration(), |
| 275 | 'scroll_duration' => $this->options->scroll_duration(), |
| 276 | 'location' => $this->options->location(), |
| 277 | 'marginX' => $this->options->marginX(), |
| 278 | 'marginY' => $this->options->marginY(), |
| 279 | 'hide_iframe' => $this->options->hide_iframe(), |
| 280 | 'auto_hide' => $this->options->auto_hide(), |
| 281 | 'auto_hide_after' => $this->options->auto_hide_after(), |
| 282 | 'button_action' => $this->options->button_action(), |
| 283 | 'button_action_element_selector' => $this->options->button_action_element_selector(), |
| 284 | 'button_action_container_selector' => $this->options->button_action_container_selector(), |
| 285 | 'button_action_element_offset' => $this->options->button_action_element_offset() |
| 286 | )) . ');'; |
| 287 | echo '} else {'; |
| 288 | echo 'setTimeout(wpfront_scroll_top_init, 100);'; |
| 289 | echo '}'; |
| 290 | echo '}'; |
| 291 | echo 'wpfront_scroll_top_init();'; |
| 292 | echo '</script>'; |
| 293 | } |
| 294 | |
| 295 | $this->markupLoaded = TRUE; |
| 296 | } |
| 297 | |
| 298 | protected function doing_ajax() { |
| 299 | if (defined('DOING_AJAX') && DOING_AJAX) { |
| 300 | return TRUE; |
| 301 | } |
| 302 | |
| 303 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
| 304 | return TRUE; |
| 305 | } |
| 306 | |
| 307 | if (!empty($_SERVER['REQUEST_URI']) && strtolower($_SERVER['REQUEST_URI']) == '/wp-admin/async-upload.php') { |
| 308 | return TRUE; |
| 309 | } |
| 310 | |
| 311 | return FALSE; |
| 312 | } |
| 313 | |
| 314 | protected function apply_button_action_html($html) { |
| 315 | if($this->options->button_action() == "url") { |
| 316 | return sprintf('<a href="%s">' . $html . '</a>', $this->options->button_action_page_url()); |
| 317 | } |
| 318 | |
| 319 | return $html; |
| 320 | } |
| 321 | |
| 322 | protected function enabled() { |
| 323 | $enabled = TRUE; |
| 324 | |
| 325 | if ($enabled && !$this->options->enabled()) { |
| 326 | $enabled = FALSE; |
| 327 | } |
| 328 | |
| 329 | if ($enabled && $this->options->hide_wpadmin() && is_admin()) { |
| 330 | $enabled = FALSE; |
| 331 | } |
| 332 | |
| 333 | if ($enabled && !$this->filter_pages()) { |
| 334 | $enabled = FALSE; |
| 335 | } |
| 336 | |
| 337 | $enabled = apply_filters('wpfront_scroll_top_enabled', $enabled); |
| 338 | |
| 339 | return $enabled; |
| 340 | } |
| 341 | |
| 342 | protected function filter_pages() { |
| 343 | if (is_admin()) { |
| 344 | return TRUE; |
| 345 | } |
| 346 | |
| 347 | switch ($this->options->display_pages()) { |
| 348 | case 1: |
| 349 | return TRUE; |
| 350 | case 2: |
| 351 | case 3: |
| 352 | global $post; |
| 353 | $ID = FALSE; |
| 354 | if (is_home()) { |
| 355 | $ID = 'home'; |
| 356 | } elseif(!empty($post)) { |
| 357 | $ID = $post->ID; |
| 358 | } |
| 359 | if ($this->options->display_pages() == 2) { |
| 360 | if ($ID !== FALSE) { |
| 361 | if ($this->filter_pages_contains($this->options->include_pages(), $ID) === FALSE) { |
| 362 | return FALSE; |
| 363 | } |
| 364 | else { |
| 365 | return TRUE; |
| 366 | } |
| 367 | } |
| 368 | return FALSE; |
| 369 | } |
| 370 | if ($this->options->display_pages() == 3) { |
| 371 | if ($ID !== FALSE) { |
| 372 | if ($this->filter_pages_contains($this->options->exclude_pages(), $ID) === FALSE) { |
| 373 | return TRUE; |
| 374 | } |
| 375 | else { |
| 376 | return FALSE; |
| 377 | } |
| 378 | } |
| 379 | return TRUE; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return TRUE; |
| 384 | } |
| 385 | |
| 386 | protected function filter_pages_contains($list, $key) { |
| 387 | return strpos(',' . $list . ',', ',' . $key . ','); |
| 388 | } |
| 389 | |
| 390 | protected function image() { |
| 391 | if ($this->options->image() == 'custom') { |
| 392 | return $this->options->custom_url(); |
| 393 | } |
| 394 | return $this->iconsURL . $this->options->image(); |
| 395 | } |
| 396 | |
| 397 | protected function get_filter_objects() { |
| 398 | $objects = array(); |
| 399 | |
| 400 | $objects['home'] = __('[Page]', 'wpfront-scroll-top') . ' ' . __('Home', 'wpfront-scroll-top'); |
| 401 | |
| 402 | $pages = get_pages(); |
| 403 | foreach ($pages as $page) { |
| 404 | $objects[$page->ID] = __('[Page]', 'wpfront-scroll-top') . ' ' . $page->post_title; |
| 405 | } |
| 406 | |
| 407 | $posts = get_posts(); |
| 408 | foreach ($posts as $post) { |
| 409 | $objects[$post->ID] = __('[Post]', 'wpfront-scroll-top') . ' ' . $post->post_title; |
| 410 | } |
| 411 | |
| 412 | // $categories = get_categories(); |
| 413 | // foreach ($categories as $category) { |
| 414 | // $objects['3.' . $category->cat_ID] = __('[Category]', 'wpfront-scroll-top') . ' ' . $category->cat_name; |
| 415 | // } |
| 416 | |
| 417 | return $objects; |
| 418 | } |
| 419 | |
| 420 | public function options_page() { |
| 421 | if (!current_user_can('manage_options')) { |
| 422 | wp_die(__('You do not have sufficient permissions to access this page.', 'wpfront-scroll-top')); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | include($this->pluginDIRRoot . 'templates/options-template.php'); |
| 427 | } |
| 428 | |
| 429 | protected function options_page_header($title, $optionsGroupName) { |
| 430 | echo '<div class="wrap">'; |
| 431 | echo '<h2>' . $title . '</h2>'; |
| 432 | echo '<div id="' . self::PLUGIN_SLUG . '-options" class="inside">'; |
| 433 | echo '<form method="post" action="options.php">'; |
| 434 | @settings_fields($optionsGroupName); |
| 435 | @do_settings_sections(self::PLUGIN_SLUG); |
| 436 | |
| 437 | if ((isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true') || (isset($_GET['updated']) && $_GET['updated'] == 'true')) { |
| 438 | echo ' |
| 439 | <div class="updated"> |
| 440 | <p> |
| 441 | <strong>' . __('If you have a caching plugin, clear the cache for the new settings to take effect.', 'wpfront-scroll-top') . '</strong> |
| 442 | </p> |
| 443 | </div> |
| 444 | '; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | protected function options_page_footer($settingsLink, $FAQLink) { |
| 449 | @$this->submit_button(); |
| 450 | |
| 451 | echo '</form>'; |
| 452 | echo '</div>'; |
| 453 | echo '</div>'; |
| 454 | |
| 455 | $this->settingsLink = $settingsLink; |
| 456 | $this->FAQLink = $FAQLink; |
| 457 | |
| 458 | add_filter('admin_footer_text', array($this, 'admin_footer_text')); |
| 459 | } |
| 460 | |
| 461 | public function admin_footer_text($text) { |
| 462 | $settingsLink = sprintf('<a href="%s" target="_blank">%s</a>', 'https://wpfront.com/' . $this->settingsLink, __('Settings Description', 'wpfront-scroll-top')); |
| 463 | $reviewLink = sprintf('<a href="%s" target="_blank">%s</a>', 'https://wordpress.org/support/plugin/' . self::PLUGIN_SLUG . '/reviews/', __('Write a Review', 'wpfront-scroll-top')); |
| 464 | $donateLink = sprintf('<a href="%s" target="_blank">%s</a>', 'https://wpfront.com/donate/', __('Buy me a Beer or Coffee', 'wpfront-scroll-top')); |
| 465 | |
| 466 | return sprintf('%s | %s | %s | %s', $settingsLink, $reviewLink, $donateLink, $text); |
| 467 | } |
| 468 | |
| 469 | //for compatibility |
| 470 | public function submit_button() { |
| 471 | if (function_exists('submit_button')) { |
| 472 | submit_button(); |
| 473 | } else { |
| 474 | echo '<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="' . __('Save Changes', 'wpfront-scroll-top') . '" /></p>'; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 |