PluginProbe ʕ •ᴥ•ʔ
WPFront Scroll Top / 2.0.3
WPFront Scroll Top v2.0.3
1.5 1.6 1.6.1 1.6.2 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1 2.1.1 2.2 3.0.0 3.0.1 trunk 1.0 1.0.1 1.1 1.1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5
wpfront-scroll-top / classes / class-wpfront-scroll-top.php
wpfront-scroll-top / classes Last commit date
base 5 years ago class-wpfront-scroll-top-options.php 5 years ago class-wpfront-scroll-top.php 5 years ago
class-wpfront-scroll-top.php
458 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 require_once(dirname(__DIR__) . '/templates/scroll-top-template.php');
29 require_once(dirname(__DIR__) . '/templates/options-template.php');
30
31 /**
32 * Main class of WPFront Scroll Top plugin
33 *
34 * @author Syam Mohan <syam@wpfront.com>
35 * @copyright 2013 WPFront.com
36 */
37 class WPFront_Scroll_Top {
38
39 //Constants
40 const VERSION = '2.0.3.03232';
41 const OPTIONS_GROUP_NAME = 'wpfront-scroll-top-options-group';
42 const OPTION_NAME = 'wpfront-scroll-top-options';
43 const PLUGIN_SLUG = 'wpfront-scroll-top';
44 const PLUGIN_FILE = 'wpfront-scroll-top/wpfront-scroll-top.php';
45
46 //Variables
47 protected $iconsDIR = '/tmp/icons/';
48 protected $iconsURL = '//tmp/icons/';
49 protected $pluginDIRRoot = '/tmp/';
50 protected $pluginURLRoot = '//tmp/';
51 protected $options;
52 protected $markupLoaded;
53 protected $scriptLoaded;
54 protected $min_file_suffix;
55 private static $instance = null;
56
57 protected function __construct() {
58
59 }
60
61 public static function Instance() {
62 if (empty(self::$instance)) {
63 self::$instance = new WPFront_Scroll_Top();
64 }
65
66 return self::$instance;
67 }
68
69 public function init($pluginFile = null) {
70 $this->markupLoaded = FALSE;
71 $this->min_file_suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
72
73 //Root variables
74 $this->pluginURLRoot = plugins_url() . '/' . self::PLUGIN_SLUG . '/';
75 $this->iconsURL = $this->pluginURLRoot . 'images/icons/';
76 $this->pluginDIRRoot = plugin_dir_path($pluginFile);
77 $this->iconsDIR = $this->pluginDIRRoot . 'images/icons/';
78
79 add_action('plugins_loaded', array($this, 'plugins_loaded'));
80
81 $this->add_activation_redirect();
82
83 if (is_admin()) {
84 add_action('admin_init', array($this, 'admin_init'));
85 add_action('admin_menu', array($this, 'admin_menu'));
86 add_filter('plugin_action_links', array($this, 'action_links'), 10, 2);
87
88 add_action('admin_footer', array($this, 'write_markup'));
89 } else {
90 add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
91 add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
92
93 add_action('wp_footer', array($this, 'write_markup'));
94 }
95 }
96
97 public function action_links($links, $file) {
98 if ($file == self::PLUGIN_FILE) {
99 $settings_link = '<a id="wpfront-scroll-top-settings-link" href="' . menu_page_url(self::PLUGIN_SLUG, FALSE) . '">' . __('Settings', 'wpfront-scroll-top') . '</a>';
100 array_unshift($links, $settings_link);
101 }
102 return $links;
103 }
104
105 protected function add_activation_redirect() {
106 add_action('activated_plugin', array($this, 'activated_plugin_callback'));
107 add_action('admin_init', array($this, 'admin_init_callback'), 999999);
108 }
109
110 public function activated_plugin_callback($plugin) {
111 if ($plugin !== self::PLUGIN_FILE) {
112 return;
113 }
114
115 if (is_network_admin() || isset($_GET['activate-multi'])) {
116 return;
117 }
118
119 $key = self::PLUGIN_SLUG . '-activation-redirect';
120 add_option($key, TRUE);
121 }
122
123 public function admin_init_callback() {
124 $key = self::PLUGIN_SLUG . '-activation-redirect';
125
126 if (get_option($key, FALSE)) {
127 delete_option($key);
128
129 if (is_network_admin() || isset($_GET['activate-multi'])) {
130 return;
131 }
132
133 wp_safe_redirect(menu_page_url(self::PLUGIN_SLUG, FALSE));
134 }
135 }
136
137 //add scripts
138 public function enqueue_scripts() {
139 if ($this->enabled() == FALSE) {
140 return;
141 }
142
143 $jsRoot = $this->pluginURLRoot . 'js/';
144
145 wp_enqueue_script('jquery');
146 wp_enqueue_script('wpfront-scroll-top', $jsRoot . 'wpfront-scroll-top' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION, TRUE);
147
148 $this->scriptLoaded = TRUE;
149 }
150
151 //add styles
152 public function enqueue_styles() {
153 if ($this->enabled() == FALSE) {
154 return;
155 }
156
157 $cssRoot = $this->pluginURLRoot . 'css/';
158
159 wp_enqueue_style('wpfront-scroll-top', $cssRoot . 'wpfront-scroll-top' . $this->min_file_suffix . '.css', array(), self::VERSION);
160
161 if ($this->options->button_style() == 'font-awesome') {
162 if (!$this->options->fa_button_exclude_URL() || is_admin()) {
163 $url = trim($this->options->fa_button_URL());
164 $ver = FALSE;
165 if (empty($url)) {
166 $url = '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css';
167 $ver = '4.7.0';
168 }
169 wp_enqueue_style('font-awesome', $url, array(), $ver);
170 }
171 }
172 }
173
174 public function admin_init() {
175 register_setting(self::OPTIONS_GROUP_NAME, self::OPTION_NAME);
176
177 $this->enqueue_styles();
178 $this->enqueue_scripts();
179 }
180
181 public function admin_menu() {
182 $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'));
183
184 add_action('admin_print_scripts-' . $page_hook_suffix, array($this, 'enqueue_options_scripts'));
185 add_action('admin_print_styles-' . $page_hook_suffix, array($this, 'enqueue_options_styles'));
186 }
187
188 public function enqueue_options_scripts() {
189 wp_enqueue_media();
190
191 $this->enqueue_scripts();
192
193 $jsRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/js/';
194 wp_enqueue_script('jquery.eyecon.colorpicker', $jsRoot . 'colorpicker' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION);
195
196 $jsRoot = $this->pluginURLRoot . 'js/';
197 wp_enqueue_script('wpfront-scroll-top-options', $jsRoot . 'options' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION);
198 }
199
200 //options page styles
201 public function enqueue_options_styles() {
202 $styleRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/css/';
203 wp_enqueue_style('jquery.eyecon.colorpicker', $styleRoot . 'colorpicker' . $this->min_file_suffix . '.css', array(), self::VERSION);
204
205 $styleRoot = $this->pluginURLRoot . 'css/';
206 wp_enqueue_style('wpfront-scroll-top-options', $styleRoot . 'options' . $this->min_file_suffix . '.css', array(), self::VERSION);
207 }
208
209 public function set_options($options = null) {
210 if ($options === null) {
211 $options = new WPFront_Scroll_Top_Options(self::OPTION_NAME, self::PLUGIN_SLUG);
212 }
213
214 $this->options = $options;
215 }
216
217 public function get_options() {
218 return $this->options;
219 }
220
221 public function plugins_loaded() {
222 //load plugin options
223 $this->set_options();
224
225 if ($this->options->javascript_async()) {
226 add_filter('script_loader_tag', array($this, 'script_loader_tag'), 999999, 3);
227 }
228
229 if (!is_admin()) {
230 if ($this->options->attach_on_shutdown()) {
231 add_action('shutdown', array($this, 'shutdown_callback'));
232 }
233 }
234 }
235
236 public function script_loader_tag($tag, $handle, $src) {
237 if ($handle === 'wpfront-scroll-top') {
238 return '<script type="text/javascript" src="' . $src . '" id="wpfront-scroll-top-js" async="async" defer="defer"></script>' . "\n";
239 }
240
241 return $tag;
242 }
243
244 public function shutdown_callback() {
245 if ($this->markupLoaded) {
246 return;
247 }
248
249 $headers = $this->get_headers();
250 $flag = FALSE;
251 foreach ($headers as $value) {
252 $value = strtolower(str_replace(' ', '', $value));
253 if (strpos($value, 'content-type:text/html') === 0) {
254 $flag = TRUE;
255 break;
256 }
257 }
258
259 if ($flag) {
260 $this->write_markup();
261 }
262 }
263
264 protected function get_headers() {
265 return headers_list();
266 }
267
268 //writes the html and script for the button
269 public function write_markup() {
270 if ($this->markupLoaded) {
271 return;
272 }
273
274 if (!$this->scriptLoaded) {
275 return;
276 }
277
278 if ($this->doing_ajax()) {
279 return;
280 }
281
282 if ($this->enabled()) {
283 if (is_admin()) {
284 $this->options->set_button_action('top');
285 }
286
287 $template = new WPFront_Scroll_Top_Template();
288 $template->write_markup($this);
289 }
290
291 $this->markupLoaded = TRUE;
292 }
293
294 protected function doing_ajax() {
295 if (defined('DOING_AJAX') && DOING_AJAX) {
296 return TRUE;
297 }
298
299 if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
300 return TRUE;
301 }
302
303 if (!empty($_SERVER['REQUEST_URI']) && strtolower($_SERVER['REQUEST_URI']) == '/wp-admin/async-upload.php') {
304 return TRUE;
305 }
306
307 if (wp_doing_ajax()) {
308 return TRUE;
309 }
310
311 if (wp_is_json_request()) {
312 return TRUE;
313 }
314
315 if (wp_is_jsonp_request()) {
316 return TRUE;
317 }
318
319 if (wp_is_xml_request()) {
320 return TRUE;
321 }
322
323 if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) {
324 return TRUE;
325 }
326
327 if (defined('WP_CLI') && WP_CLI) {
328 return TRUE;
329 }
330
331 return FALSE;
332 }
333
334 public function apply_button_action_html($html) {
335 if ($this->options->button_action() == "url") {
336 return sprintf('<a href="%s">' . $html . '</a>', $this->options->button_action_page_url());
337 }
338
339 return $html;
340 }
341
342 protected function enabled() {
343 $enabled = TRUE;
344
345 if ($enabled && !$this->options->enabled()) {
346 $enabled = FALSE;
347 }
348
349 if ($enabled && $this->options->hide_wpadmin() && is_admin()) {
350 $enabled = FALSE;
351 }
352
353 if ($enabled && !$this->filter_pages()) {
354 $enabled = FALSE;
355 }
356
357 $enabled = apply_filters('wpfront_scroll_top_enabled', $enabled);
358
359 return $enabled;
360 }
361
362 public function filter_pages() {
363 if (is_admin()) {
364 return TRUE;
365 }
366
367 switch ($this->options->display_pages()) {
368 case 1:
369 return TRUE;
370 case 2:
371 case 3:
372 global $post;
373 $ID = FALSE;
374 if (is_home()) {
375 $ID = 'home';
376 } elseif (!empty($post)) {
377 $ID = $post->ID;
378 }
379 if ($this->options->display_pages() == 2) {
380 if ($ID !== FALSE) {
381 if ($this->filter_pages_contains($this->options->include_pages(), $ID) === FALSE) {
382 return FALSE;
383 } else {
384 return TRUE;
385 }
386 }
387 return FALSE;
388 }
389 if ($this->options->display_pages() == 3) {
390 if ($ID !== FALSE) {
391 if ($this->filter_pages_contains($this->options->exclude_pages(), $ID) === FALSE) {
392 return TRUE;
393 } else {
394 return FALSE;
395 }
396 }
397 return TRUE;
398 }
399 }
400
401 return TRUE;
402 }
403
404 public function filter_pages_contains($list, $key) {
405 return strpos(',' . $list . ',', ',' . $key . ',');
406 }
407
408 public function image() {
409 $image = $this->options->image();
410 if ($image == 'custom') {
411 return $this->options->custom_url();
412 }
413 return $this->iconsURL . $image;
414 }
415
416 public function get_filter_objects() {
417 $objects = array();
418
419 $objects['home'] = __('[Page]', 'wpfront-scroll-top') . ' ' . __('Home', 'wpfront-scroll-top');
420
421 $pages = get_pages();
422 foreach ($pages as $page) {
423 $objects[$page->ID] = __('[Page]', 'wpfront-scroll-top') . ' ' . $page->post_title;
424 }
425
426 $posts = get_posts();
427 foreach ($posts as $post) {
428 $objects[$post->ID] = __('[Post]', 'wpfront-scroll-top') . ' ' . $post->post_title;
429 }
430
431 // $categories = get_categories();
432 // foreach ($categories as $category) {
433 // $objects['3.' . $category->cat_ID] = __('[Category]', 'wpfront-scroll-top') . ' ' . $category->cat_name;
434 // }
435
436 return $objects;
437 }
438
439 public function options_page() {
440 if (!current_user_can('manage_options')) {
441 wp_die(__('You do not have sufficient permissions to access this page.', 'wpfront-scroll-top'));
442 return;
443 }
444
445 $options_view = new WPFront_Scroll_Top_Options_View();
446 $options_view->view($this);
447 }
448
449 public function get_icon_dir() {
450 return $this->iconsDIR;
451 }
452
453 public function get_icon_url() {
454 return $this->iconsURL;
455 }
456
457 }
458