class-wpfront-options-base.php
12 years ago
class-wpfront-scroll-top-options.php
12 years ago
class-wpfront-scroll-top.php
12 years ago
class-wpfront-scroll-top.php
232 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("class-wpfront-scroll-top-options.php"); |
| 26 | |
| 27 | if (!class_exists('WPFront_Scroll_Top')) { |
| 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 | |
| 37 | //Constants |
| 38 | const VERSION = '1.1'; |
| 39 | const OPTIONSPAGE_SLUG = 'wpfront-scroll-top'; |
| 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 | private $pluginURLRoot; |
| 46 | private $pluginDIRRoot; |
| 47 | private $iconsDIR; |
| 48 | private $iconsURL; |
| 49 | private $options; |
| 50 | private $markupLoaded; |
| 51 | private $scriptLoaded; |
| 52 | |
| 53 | function __construct() { |
| 54 | $this->markupLoaded = FALSE; |
| 55 | |
| 56 | //Root variables |
| 57 | $this->pluginURLRoot = plugins_url() . '/wpfront-scroll-top/'; |
| 58 | $this->pluginDIRRoot = dirname(__FILE__) . '/../'; |
| 59 | $this->iconsDIR = $this->pluginDIRRoot . 'images/icons/'; |
| 60 | $this->iconsURL = $this->pluginURLRoot . 'images/icons/'; |
| 61 | |
| 62 | add_action('init', array(&$this, 'init')); |
| 63 | |
| 64 | //register actions |
| 65 | if (is_admin()) { |
| 66 | add_action('admin_init', array(&$this, 'admin_init')); |
| 67 | add_action('admin_menu', array(&$this, 'admin_menu')); |
| 68 | add_filter('plugin_action_links', array(&$this, 'action_links'), 10, 2); |
| 69 | } else { |
| 70 | add_action('wp_enqueue_scripts', array(&$this, 'enqueue_styles')); |
| 71 | add_action('wp_enqueue_scripts', array(&$this, 'enqueue_scripts')); |
| 72 | } |
| 73 | |
| 74 | add_action('wp_footer', array(&$this, 'write_markup')); |
| 75 | add_action('shutdown', array(&$this, 'write_markup')); |
| 76 | add_action('plugins_loaded', array(&$this, 'plugins_loaded')); |
| 77 | } |
| 78 | |
| 79 | public function init() { |
| 80 | |
| 81 | } |
| 82 | |
| 83 | //add scripts |
| 84 | public function enqueue_scripts() { |
| 85 | if ($this->enabled() == FALSE) |
| 86 | return; |
| 87 | |
| 88 | $jsRoot = $this->pluginURLRoot . 'js/'; |
| 89 | |
| 90 | wp_enqueue_script('jquery'); |
| 91 | wp_enqueue_script('wpfront-scroll-top', $jsRoot . 'wpfront-scroll-top.js', array('jquery'), self::VERSION); |
| 92 | |
| 93 | $this->scriptLoaded = TRUE; |
| 94 | } |
| 95 | |
| 96 | //add styles |
| 97 | public function enqueue_styles() { |
| 98 | if ($this->enabled() == FALSE) |
| 99 | return; |
| 100 | |
| 101 | $cssRoot = $this->pluginURLRoot . 'css/'; |
| 102 | |
| 103 | wp_enqueue_style('wpfront-scroll-top', $cssRoot . 'wpfront-scroll-top.css', array(), self::VERSION); |
| 104 | } |
| 105 | |
| 106 | public function admin_init() { |
| 107 | register_setting(self::OPTIONS_GROUP_NAME, self::OPTION_NAME); |
| 108 | |
| 109 | $this->enqueue_styles(); |
| 110 | $this->enqueue_scripts(); |
| 111 | } |
| 112 | |
| 113 | public function admin_menu() { |
| 114 | $page_hook_suffix = add_options_page($this->__('WPFront Scroll Top'), $this->__('Scroll Top'), 'manage_options', self::OPTIONSPAGE_SLUG, array($this, 'options_page')); |
| 115 | |
| 116 | //register for options page scripts and styles |
| 117 | add_action('admin_print_scripts-' . $page_hook_suffix, array($this, 'enqueue_options_scripts')); |
| 118 | add_action('admin_print_styles-' . $page_hook_suffix, array($this, 'enqueue_options_styles')); |
| 119 | } |
| 120 | |
| 121 | //options page scripts |
| 122 | public function enqueue_options_scripts() { |
| 123 | //$this->enqueue_scripts(); |
| 124 | // $jsRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/js/'; |
| 125 | // wp_enqueue_script('jquery.eyecon.colorpicker', $jsRoot . 'colorpicker.js', array('jquery'), self::VERSION); |
| 126 | // $jsRoot = $this->pluginURLRoot . 'js/'; |
| 127 | // wp_enqueue_script('wpfront-notification-bar-options', $jsRoot . 'options.js', array(), self::VERSION); |
| 128 | } |
| 129 | |
| 130 | //options page styles |
| 131 | public function enqueue_options_styles() { |
| 132 | //$this->enqueue_styles(); |
| 133 | // $styleRoot = $this->pluginURLRoot . 'jquery-plugins/colorpicker/css/'; |
| 134 | // wp_enqueue_style('jquery.eyecon.colorpicker.colorpicker', $styleRoot . 'colorpicker.css', array(), self::VERSION); |
| 135 | |
| 136 | $styleRoot = $this->pluginURLRoot . 'css/'; |
| 137 | wp_enqueue_style('wpfront-scroll-top-options', $styleRoot . 'options.css', array(), self::VERSION); |
| 138 | } |
| 139 | |
| 140 | //creates options page |
| 141 | public function options_page() { |
| 142 | if (!current_user_can('manage_options')) { |
| 143 | wp_die($this->__('You do not have sufficient permissions to access this page.')); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | include($this->pluginDIRRoot . 'templates/options-template.php'); |
| 148 | } |
| 149 | |
| 150 | //add "settings" link |
| 151 | public function action_links($links, $file) { |
| 152 | if ($file == 'wpfront-scroll-top/wpfront-scroll-top.php') { |
| 153 | $settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=' . self::OPTIONSPAGE_SLUG . '">' . $this->__('Settings') . '</a>'; |
| 154 | array_unshift($links, $settings_link); |
| 155 | } |
| 156 | return $links; |
| 157 | } |
| 158 | |
| 159 | public function plugins_loaded() { |
| 160 | //load plugin options |
| 161 | $this->options = new WPFront_Scroll_Top_Options(self::OPTION_NAME, self::PLUGIN_SLUG); |
| 162 | |
| 163 | //for localization |
| 164 | load_plugin_textdomain(self::PLUGIN_SLUG, FALSE, $this->pluginDIRRoot . 'languages/'); |
| 165 | } |
| 166 | |
| 167 | //writes the html and script for the bar |
| 168 | public function write_markup() { |
| 169 | if ($this->markupLoaded) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if ($this->scriptLoaded != TRUE) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if (defined('DOING_AJAX') && DOING_AJAX) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
| 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 'if(typeof wpfront_scroll_top == "function") '; |
| 190 | echo 'wpfront_scroll_top(' . json_encode(array( |
| 191 | 'scroll_offset' => $this->options->scroll_offset(), |
| 192 | 'button_width' => $this->options->button_width(), |
| 193 | 'button_height' => $this->options->button_height(), |
| 194 | 'button_opacity' => $this->options->button_opacity() / 100, |
| 195 | 'button_fade_duration' => $this->options->button_fade_duration(), |
| 196 | 'scroll_duration' => $this->options->scroll_duration(), |
| 197 | 'location' => $this->options->location(), |
| 198 | 'marginX' => $this->options->marginX(), |
| 199 | 'marginY' => $this->options->marginY(), |
| 200 | )) . ');'; |
| 201 | echo '</script>'; |
| 202 | } |
| 203 | |
| 204 | $this->markupLoaded = TRUE; |
| 205 | } |
| 206 | |
| 207 | //returns localized string |
| 208 | public function __($key) { |
| 209 | return __($key, self::PLUGIN_SLUG); |
| 210 | } |
| 211 | |
| 212 | //for compatibility |
| 213 | private function submit_button() { |
| 214 | if (function_exists('submit_button')) { |
| 215 | submit_button(); |
| 216 | } else { |
| 217 | echo '<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="' . $this->__('Save Changes') . '" /></p>'; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | private function enabled() { |
| 222 | return $this->options->enabled(); |
| 223 | } |
| 224 | |
| 225 | private function image() { |
| 226 | if($this->options->image() == 'custom') |
| 227 | return $this->options->custom_url(); |
| 228 | return $this->iconsURL . $this->options->image(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | } |