base
9 years ago
class-wpfront-scroll-top-options.php
9 years ago
class-wpfront-scroll-top.php
9 years ago
class-wpfront-scroll-top.php
289 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 { |
| 37 | |
| 38 | //Constants |
| 39 | const VERSION = '1.5'; |
| 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 | $this->add_menu($this->__('WPFront Scroll Top'), $this->__('Scroll Top')); |
| 66 | } |
| 67 | |
| 68 | //add scripts |
| 69 | public function enqueue_scripts() { |
| 70 | if ($this->enabled() == FALSE) |
| 71 | return; |
| 72 | |
| 73 | $jsRoot = $this->pluginURLRoot . 'js/'; |
| 74 | |
| 75 | wp_enqueue_script('jquery'); |
| 76 | wp_enqueue_script('wpfront-scroll-top', $jsRoot . 'wpfront-scroll-top' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION); |
| 77 | |
| 78 | $this->scriptLoaded = TRUE; |
| 79 | } |
| 80 | |
| 81 | //add styles |
| 82 | public function enqueue_styles() { |
| 83 | if ($this->enabled() == FALSE) |
| 84 | return; |
| 85 | |
| 86 | $cssRoot = $this->pluginURLRoot . 'css/'; |
| 87 | |
| 88 | wp_enqueue_style('wpfront-scroll-top', $cssRoot . 'wpfront-scroll-top' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 89 | } |
| 90 | |
| 91 | public function admin_init() { |
| 92 | register_setting(self::OPTIONS_GROUP_NAME, self::OPTION_NAME); |
| 93 | |
| 94 | $this->enqueue_styles(); |
| 95 | $this->enqueue_scripts(); |
| 96 | } |
| 97 | |
| 98 | public function enqueue_options_scripts() { |
| 99 | $this->enqueue_scripts(); |
| 100 | |
| 101 | $jsRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/js/'; |
| 102 | wp_enqueue_script('jquery.eyecon.colorpicker', $jsRoot . 'colorpicker' . $this->min_file_suffix . '.js', array('jquery'), self::VERSION); |
| 103 | } |
| 104 | |
| 105 | //options page styles |
| 106 | public function enqueue_options_styles() { |
| 107 | $styleRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/css/'; |
| 108 | wp_enqueue_style('jquery.eyecon.colorpicker.colorpicker', $styleRoot . 'colorpicker' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 109 | |
| 110 | $styleRoot = $this->pluginURLRoot . 'css/'; |
| 111 | wp_enqueue_style('wpfront-scroll-top-options', $styleRoot . 'options' . $this->min_file_suffix . '.css', array(), self::VERSION); |
| 112 | } |
| 113 | |
| 114 | public function plugins_loaded() { |
| 115 | //load plugin options |
| 116 | $this->options = new WPFront_Scroll_Top_Options(self::OPTION_NAME, self::PLUGIN_SLUG); |
| 117 | |
| 118 | if($this->options->javascript_async()) |
| 119 | add_filter('script_loader_tag', array($this, 'script_loader_tag'), 999999, 3); |
| 120 | } |
| 121 | |
| 122 | public function script_loader_tag($tag, $handle, $src) { |
| 123 | if($handle === 'wpfront-scroll-top') |
| 124 | return '<script type="text/javascript" src="' . $src . '" async="true"></script>' . "\n"; |
| 125 | |
| 126 | return $tag; |
| 127 | } |
| 128 | |
| 129 | public function shutdown_callback() { |
| 130 | if ($this->markupLoaded) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | $headers = headers_list(); |
| 135 | $flag = FALSE; |
| 136 | foreach ($headers as $value) { |
| 137 | $value = strtolower(str_replace(' ', '', $value)); |
| 138 | if (strpos($value, 'content-type:text/html') === 0) { |
| 139 | $flag = TRUE; |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if ($flag) |
| 145 | $this->write_markup(); |
| 146 | } |
| 147 | |
| 148 | //writes the html and script for the button |
| 149 | public function write_markup() { |
| 150 | if ($this->markupLoaded) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | if ($this->scriptLoaded != TRUE) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | if (WPFront_Static_ST::doing_ajax()) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if ($this->enabled()) { |
| 163 | include($this->pluginDIRRoot . 'templates/scroll-top-template.php'); |
| 164 | |
| 165 | echo '<script type="text/javascript">'; |
| 166 | echo 'function wpfront_scroll_top_init() {'; |
| 167 | echo 'if(typeof wpfront_scroll_top == "function" && typeof jQuery !== "undefined") {'; |
| 168 | echo 'wpfront_scroll_top(' . json_encode(array( |
| 169 | 'scroll_offset' => $this->options->scroll_offset(), |
| 170 | 'button_width' => $this->options->button_width(), |
| 171 | 'button_height' => $this->options->button_height(), |
| 172 | 'button_opacity' => $this->options->button_opacity() / 100, |
| 173 | 'button_fade_duration' => $this->options->button_fade_duration(), |
| 174 | 'scroll_duration' => $this->options->scroll_duration(), |
| 175 | 'location' => $this->options->location(), |
| 176 | 'marginX' => $this->options->marginX(), |
| 177 | 'marginY' => $this->options->marginY(), |
| 178 | 'hide_iframe' => $this->options->hide_iframe(), |
| 179 | 'auto_hide' => $this->options->auto_hide(), |
| 180 | 'auto_hide_after' => $this->options->auto_hide_after(), |
| 181 | )) . ');'; |
| 182 | echo '} else {'; |
| 183 | echo 'setTimeout(wpfront_scroll_top_init, 100);'; |
| 184 | echo '}'; |
| 185 | echo '}'; |
| 186 | echo 'wpfront_scroll_top_init();'; |
| 187 | echo '</script>'; |
| 188 | } |
| 189 | |
| 190 | $this->markupLoaded = TRUE; |
| 191 | } |
| 192 | |
| 193 | private function enabled() { |
| 194 | if (!$this->options->enabled()) |
| 195 | return FALSE; |
| 196 | |
| 197 | if ($this->options->hide_wpadmin() && is_admin()) |
| 198 | return FALSE; |
| 199 | |
| 200 | if (!$this->filter_pages()) |
| 201 | return FALSE; |
| 202 | |
| 203 | return TRUE; |
| 204 | } |
| 205 | |
| 206 | private function filter_pages() { |
| 207 | if (is_admin()) |
| 208 | return TRUE; |
| 209 | |
| 210 | switch ($this->options->display_pages()) { |
| 211 | case 1: |
| 212 | return TRUE; |
| 213 | case 2: |
| 214 | case 3: |
| 215 | global $post; |
| 216 | $ID = FALSE; |
| 217 | $type = FALSE; |
| 218 | if (is_home()) { |
| 219 | $ID = 'home'; |
| 220 | $type = 1; |
| 221 | } elseif (is_singular()) { |
| 222 | $post_type = get_post_type(); |
| 223 | if ($post_type == 'page') { |
| 224 | $ID = $post->ID; |
| 225 | $type = 1; |
| 226 | } elseif ($post_type == 'post') { |
| 227 | $ID = $post->ID; |
| 228 | $type = 2; |
| 229 | } |
| 230 | } |
| 231 | if ($this->options->display_pages() == 2) { |
| 232 | if ($ID !== FALSE && $type !== FALSE) { |
| 233 | if ($this->filter_pages_contains($this->options->include_pages(), $type . '.' . $ID) === FALSE) |
| 234 | return FALSE; |
| 235 | else |
| 236 | return TRUE; |
| 237 | } |
| 238 | return FALSE; |
| 239 | } |
| 240 | if ($this->options->display_pages() == 3) { |
| 241 | if ($ID !== FALSE && $type !== FALSE) { |
| 242 | if ($this->filter_pages_contains($this->options->exclude_pages(), $type . '.' . $ID) === FALSE) |
| 243 | return TRUE; |
| 244 | else |
| 245 | return FALSE; |
| 246 | } |
| 247 | return TRUE; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return TRUE; |
| 252 | } |
| 253 | |
| 254 | public function filter_pages_contains($list, $key) { |
| 255 | return strpos(',' . $list . ',', ',' . $key . ','); |
| 256 | } |
| 257 | |
| 258 | private function image() { |
| 259 | if ($this->options->image() == 'custom') |
| 260 | return $this->options->custom_url(); |
| 261 | return $this->iconsURL . $this->options->image(); |
| 262 | } |
| 263 | |
| 264 | protected function get_filter_objects() { |
| 265 | $objects = array(); |
| 266 | |
| 267 | $objects['1.home'] = $this->__('[Page]') . ' ' . $this->__('Home'); |
| 268 | |
| 269 | $pages = get_pages(); |
| 270 | foreach ($pages as $page) { |
| 271 | $objects['1.' . $page->ID] = $this->__('[Page]') . ' ' . $page->post_title; |
| 272 | } |
| 273 | |
| 274 | $posts = get_posts(); |
| 275 | foreach ($posts as $post) { |
| 276 | $objects['2.' . $post->ID] = $this->__('[Post]') . ' ' . $post->post_title; |
| 277 | } |
| 278 | |
| 279 | // $categories = get_categories(); |
| 280 | // foreach ($categories as $category) { |
| 281 | // $objects['3.' . $category->cat_ID] = $this->__('[Category]') . ' ' . $category->cat_name; |
| 282 | // } |
| 283 | |
| 284 | return $objects; |
| 285 | } |
| 286 | |
| 287 | } |
| 288 | |
| 289 | } |