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
303 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 | require_once("base/class-wpfront-base.php"); |
| 26 | require_once("class-wpfront-scroll-top-options.php"); |
| 27 | |
| 28 | if (!class_exists('WPFront_Scroll_Top')) { |
| 29 | |
| 30 | /** |
| 31 | * Main class of WPFront Scroll Top plugin |
| 32 | * |
| 33 | * @author Syam Mohan <syam@wpfront.com> |
| 34 | * @copyright 2013 WPFront.com |
| 35 | */ |
| 36 | class WPFront_Scroll_Top extends WPFront_Base_ST { |
| 37 | |
| 38 | //Constants |
| 39 | const VERSION = '1.6'; |
| 40 | const OPTIONS_GROUP_NAME = 'wpfront-scroll-top-options-group'; |
| 41 | const OPTION_NAME = 'wpfront-scroll-top-options'; |
| 42 | const PLUGIN_SLUG = 'wpfront-scroll-top'; |
| 43 | |
| 44 | //Variables |
| 45 | protected $iconsDIR; |
| 46 | protected $iconsURL; |
| 47 | protected $options; |
| 48 | protected $markupLoaded; |
| 49 | protected $scriptLoaded; |
| 50 | protected $min_file_suffix; |
| 51 | |
| 52 | function __construct() { |
| 53 | parent::__construct(__FILE__, self::PLUGIN_SLUG); |
| 54 | |
| 55 | $this->markupLoaded = FALSE; |
| 56 | $this->min_file_suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min'; |
| 57 | |
| 58 | //Root variables |
| 59 | $this->iconsDIR = $this->pluginDIRRoot . 'images/icons/'; |
| 60 | $this->iconsURL = $this->pluginURLRoot . 'images/icons/'; |
| 61 | |
| 62 | add_action('wp_footer', array(&$this, 'write_markup')); |
| 63 | add_action('shutdown', array(&$this, 'shutdown_callback')); |
| 64 | } |
| 65 | |
| 66 | //add scripts |
| 67 | public function enqueue_scripts() { |
| 68 | if ($this->enabled() == FALSE) |
| 69 | return; |
| 70 | |
| 71 | $jsRoot = $this->pluginURLRoot . 'js/'; |
| 72 | |
| 73 | wp_enqueue_script('jquery'); |
| 74 | wp_enqueue_script('wpfront-scroll-top', $jsRoot . 'wpfront-scroll-top' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION, TRUE); |
| 75 | |
| 76 | $this->scriptLoaded = TRUE; |
| 77 | } |
| 78 | |
| 79 | //add styles |
| 80 | public function enqueue_styles() { |
| 81 | if ($this->enabled() == FALSE) |
| 82 | return; |
| 83 | |
| 84 | $cssRoot = $this->pluginURLRoot . 'css/'; |
| 85 | |
| 86 | wp_enqueue_style('wpfront-scroll-top', $cssRoot . 'wpfront-scroll-top' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 87 | |
| 88 | if($this->options->button_style() == 'font-awesome') { |
| 89 | if(!$this->options->fa_button_exclude_URL() || is_admin()) { |
| 90 | $url = trim($this->options->fa_button_URL()); |
| 91 | $ver = FALSE; |
| 92 | if(empty($url)) { |
| 93 | $url = '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'; |
| 94 | $ver = '4.7.0'; |
| 95 | } |
| 96 | wp_enqueue_style('font-awesome', $url, array(), $ver); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public function admin_init() { |
| 102 | register_setting(self::OPTIONS_GROUP_NAME, self::OPTION_NAME); |
| 103 | |
| 104 | $this->enqueue_styles(); |
| 105 | $this->enqueue_scripts(); |
| 106 | } |
| 107 | |
| 108 | public function admin_menu() { |
| 109 | if($this->options->move_admin_menu()) { |
| 110 | $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')); |
| 111 | |
| 112 | add_action('admin_print_scripts-' . $page_hook_suffix, array($this, 'enqueue_options_scripts')); |
| 113 | add_action('admin_print_styles-' . $page_hook_suffix, array($this, 'enqueue_options_styles')); |
| 114 | } |
| 115 | else { |
| 116 | $this->add_menu(__('WPFront Scroll Top', 'wpfront-scroll-top'), __('Scroll Top', 'wpfront-scroll-top')); |
| 117 | parent::admin_menu(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public function enqueue_options_scripts() { |
| 122 | $this->enqueue_scripts(); |
| 123 | |
| 124 | $jsRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/js/'; |
| 125 | wp_enqueue_script('jquery.eyecon.colorpicker', $jsRoot . 'colorpicker' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION); |
| 126 | } |
| 127 | |
| 128 | //options page styles |
| 129 | public function enqueue_options_styles() { |
| 130 | $styleRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/css/'; |
| 131 | wp_enqueue_style('jquery.eyecon.colorpicker.colorpicker', $styleRoot . 'colorpicker' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 132 | |
| 133 | $styleRoot = $this->pluginURLRoot . 'css/'; |
| 134 | wp_enqueue_style('wpfront-scroll-top-options', $styleRoot . 'options' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 135 | } |
| 136 | |
| 137 | public function plugins_loaded() { |
| 138 | //load plugin options |
| 139 | $this->options = new WPFront_Scroll_Top_Options(self::OPTION_NAME, self::PLUGIN_SLUG); |
| 140 | |
| 141 | if($this->options->javascript_async()) |
| 142 | add_filter('script_loader_tag', array($this, 'script_loader_tag'), 999999, 3); |
| 143 | } |
| 144 | |
| 145 | public function script_loader_tag($tag, $handle, $src) { |
| 146 | if($handle === 'wpfront-scroll-top') |
| 147 | return '<script type="text/javascript" src="' . $src . '" async defer></script>' . "\n"; |
| 148 | |
| 149 | return $tag; |
| 150 | } |
| 151 | |
| 152 | public function shutdown_callback() { |
| 153 | if ($this->markupLoaded) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | $headers = headers_list(); |
| 158 | $flag = FALSE; |
| 159 | foreach ($headers as $value) { |
| 160 | $value = strtolower(str_replace(' ', '', $value)); |
| 161 | if (strpos($value, 'content-type:text/html') === 0) { |
| 162 | $flag = TRUE; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if ($flag) |
| 168 | $this->write_markup(); |
| 169 | } |
| 170 | |
| 171 | //writes the html and script for the button |
| 172 | public function write_markup() { |
| 173 | if ($this->markupLoaded) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if ($this->scriptLoaded != TRUE) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (WPFront_Static_ST::doing_ajax()) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | if ($this->enabled()) { |
| 186 | include($this->pluginDIRRoot . 'templates/scroll-top-template.php'); |
| 187 | |
| 188 | echo '<script type="text/javascript">'; |
| 189 | echo 'function wpfront_scroll_top_init() {'; |
| 190 | echo 'if(typeof wpfront_scroll_top == "function" && typeof jQuery !== "undefined") {'; |
| 191 | echo 'wpfront_scroll_top(' . json_encode(array( |
| 192 | 'scroll_offset' => $this->options->scroll_offset(), |
| 193 | 'button_width' => $this->options->button_width(), |
| 194 | 'button_height' => $this->options->button_height(), |
| 195 | 'button_opacity' => $this->options->button_opacity() / 100, |
| 196 | 'button_fade_duration' => $this->options->button_fade_duration(), |
| 197 | 'scroll_duration' => $this->options->scroll_duration(), |
| 198 | 'location' => $this->options->location(), |
| 199 | 'marginX' => $this->options->marginX(), |
| 200 | 'marginY' => $this->options->marginY(), |
| 201 | 'hide_iframe' => $this->options->hide_iframe(), |
| 202 | 'auto_hide' => $this->options->auto_hide(), |
| 203 | 'auto_hide_after' => $this->options->auto_hide_after(), |
| 204 | )) . ');'; |
| 205 | echo '} else {'; |
| 206 | echo 'setTimeout(wpfront_scroll_top_init, 100);'; |
| 207 | echo '}'; |
| 208 | echo '}'; |
| 209 | echo 'wpfront_scroll_top_init();'; |
| 210 | echo '</script>'; |
| 211 | } |
| 212 | |
| 213 | $this->markupLoaded = TRUE; |
| 214 | } |
| 215 | |
| 216 | private function enabled() { |
| 217 | if (!$this->options->enabled()) |
| 218 | return FALSE; |
| 219 | |
| 220 | if ($this->options->hide_wpadmin() && is_admin()) |
| 221 | return FALSE; |
| 222 | |
| 223 | if (!$this->filter_pages()) |
| 224 | return FALSE; |
| 225 | |
| 226 | return TRUE; |
| 227 | } |
| 228 | |
| 229 | private function filter_pages() { |
| 230 | if (is_admin()) |
| 231 | return TRUE; |
| 232 | |
| 233 | switch ($this->options->display_pages()) { |
| 234 | case 1: |
| 235 | return TRUE; |
| 236 | case 2: |
| 237 | case 3: |
| 238 | global $post; |
| 239 | $ID = FALSE; |
| 240 | if (is_home()) { |
| 241 | $ID = 'home'; |
| 242 | } else { |
| 243 | $ID = $post->ID; |
| 244 | } |
| 245 | if ($this->options->display_pages() == 2) { |
| 246 | if ($ID !== FALSE && $type !== FALSE) { |
| 247 | if ($this->filter_pages_contains($this->options->include_pages(), $ID) === FALSE) |
| 248 | return FALSE; |
| 249 | else |
| 250 | return TRUE; |
| 251 | } |
| 252 | return FALSE; |
| 253 | } |
| 254 | if ($this->options->display_pages() == 3) { |
| 255 | if ($ID !== FALSE && $type !== FALSE) { |
| 256 | if ($this->filter_pages_contains($this->options->exclude_pages(), $ID) === FALSE) |
| 257 | return TRUE; |
| 258 | else |
| 259 | return FALSE; |
| 260 | } |
| 261 | return TRUE; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return TRUE; |
| 266 | } |
| 267 | |
| 268 | public function filter_pages_contains($list, $key) { |
| 269 | return strpos(',' . $list . ',', ',' . $key . ','); |
| 270 | } |
| 271 | |
| 272 | private function image() { |
| 273 | if ($this->options->image() == 'custom') |
| 274 | return $this->options->custom_url(); |
| 275 | return $this->iconsURL . $this->options->image(); |
| 276 | } |
| 277 | |
| 278 | protected function get_filter_objects() { |
| 279 | $objects = array(); |
| 280 | |
| 281 | $objects['home'] = __('[Page]', 'wpfront-scroll-top') . ' ' . __('Home', 'wpfront-scroll-top'); |
| 282 | |
| 283 | $pages = get_pages(); |
| 284 | foreach ($pages as $page) { |
| 285 | $objects[$page->ID] = __('[Page]', 'wpfront-scroll-top') . ' ' . $page->post_title; |
| 286 | } |
| 287 | |
| 288 | $posts = get_posts(); |
| 289 | foreach ($posts as $post) { |
| 290 | $objects[$post->ID] = __('[Post]', 'wpfront-scroll-top') . ' ' . $post->post_title; |
| 291 | } |
| 292 | |
| 293 | // $categories = get_categories(); |
| 294 | // foreach ($categories as $category) { |
| 295 | // $objects['3.' . $category->cat_ID] = __('[Category]', 'wpfront-scroll-top') . ' ' . $category->cat_name; |
| 296 | // } |
| 297 | |
| 298 | return $objects; |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | |
| 303 | } |