insert-headers-and-footers
Last commit date
languages
9 years ago
views
9 years ago
ihaf.php
9 years ago
readme.txt
9 years ago
ihaf.php
225 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Insert Headers and Footers |
| 4 | * Plugin URI: http://www.wpbeginner.com/ |
| 5 | * Version: 1.4.1 |
| 6 | * Author: WPBeginner |
| 7 | * Author URI: http://www.wpbeginner.com/ |
| 8 | * Description: Allows you to insert code or text in the header or footer of your WordPress blog |
| 9 | * License: GPL2 |
| 10 | */ |
| 11 | |
| 12 | /* Copyright 2014 WPBeginner |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or modify |
| 15 | it under the terms of the GNU General Public License, version 2, as |
| 16 | published by the Free Software Foundation. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Insert Headers and Footers Class |
| 30 | */ |
| 31 | class InsertHeadersAndFooters { |
| 32 | /** |
| 33 | * Constructor |
| 34 | */ |
| 35 | public function __construct() { |
| 36 | |
| 37 | // Plugin Details |
| 38 | $this->plugin = new stdClass; |
| 39 | $this->plugin->name = 'insert-headers-and-footers'; // Plugin Folder |
| 40 | $this->plugin->displayName = 'Insert Headers and Footers'; // Plugin Name |
| 41 | $this->plugin->version = '1.4.1'; |
| 42 | $this->plugin->folder = plugin_dir_path( __FILE__ ); |
| 43 | $this->plugin->url = plugin_dir_url( __FILE__ ); |
| 44 | $this->plugin->db_welcome_dismissed_key = $this->plugin->name . '_welcome_dismissed_key'; |
| 45 | |
| 46 | // Check if the global wpb_feed_append variable exists. If not, set it. |
| 47 | if ( ! array_key_exists( 'wpb_feed_append', $GLOBALS ) ) { |
| 48 | $GLOBALS['wpb_feed_append'] = false; |
| 49 | } |
| 50 | |
| 51 | // Hooks |
| 52 | add_action( 'admin_init', array( &$this, 'registerSettings' ) ); |
| 53 | add_action( 'admin_menu', array( &$this, 'adminPanelsAndMetaBoxes' ) ); |
| 54 | add_action( 'wp_feed_options', array( &$this, 'dashBoardRss' ), 10, 2 ); |
| 55 | add_action( 'admin_notices', array( &$this, 'dashboardNotices' ) ); |
| 56 | add_action( 'wp_ajax_' . $this->plugin->name . '_dismiss_dashboard_notices', array( &$this, 'dismissDashboardNotices' ) ); |
| 57 | |
| 58 | // Frontend Hooks |
| 59 | add_action( 'wp_head', array( &$this, 'frontendHeader' ) ); |
| 60 | add_action( 'wp_footer', array( &$this, 'frontendFooter' ) ); |
| 61 | |
| 62 | // Filters |
| 63 | add_filter( 'dashboard_secondary_items', array( &$this, 'dashboardSecondaryItems' ) ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Number of Secondary feed items to show |
| 68 | */ |
| 69 | function dashboardSecondaryItems() { |
| 70 | return 6; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Update the planet feed to add the WPB feed |
| 75 | */ |
| 76 | function dashboardRss( $feed, $url ) { |
| 77 | // Return early if not on the right page. |
| 78 | global $pagenow; |
| 79 | if ( 'admin-ajax.php' !== $pagenow ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Return early if not on the right feed. |
| 84 | if ( strpos( $url, 'planet.wordpress.org' ) === false ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | // Only move forward if this action hasn't been done already. |
| 89 | if ( ! $GLOBALS['wpb_feed_append'] ) { |
| 90 | $GLOBALS['wpb_feed_append'] = true; |
| 91 | $urls = array( 'http://www.wpbeginner.com/feed/', $url ); |
| 92 | $feed->set_feed_url( $urls ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Show relevant notices for the plugin |
| 98 | */ |
| 99 | function dashboardNotices() { |
| 100 | global $pagenow; |
| 101 | |
| 102 | if ( !get_option( $this->plugin->db_welcome_dismissed_key ) ) { |
| 103 | if ( ! ( $pagenow == 'options-general.php' && isset( $_GET['page'] ) && $_GET['page'] == 'insert-headers-and-footers' ) ) { |
| 104 | $setting_page = admin_url( 'options-general.php?page=' . $this->plugin->name ); |
| 105 | // load the notices view |
| 106 | include_once( $this->plugin->folder . '/views/dashboard-notices.php' ); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Dismiss the welcome notice for the plugin |
| 113 | */ |
| 114 | function dismissDashboardNotices() { |
| 115 | check_ajax_referer( $this->plugin->name . '-nonce', 'nonce' ); |
| 116 | // user has dismissed the welcome notice |
| 117 | update_option( $this->plugin->db_welcome_dismissed_key, 1 ); |
| 118 | exit; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Register Settings |
| 123 | */ |
| 124 | function registerSettings() { |
| 125 | register_setting( $this->plugin->name, 'ihaf_insert_header', 'trim' ); |
| 126 | register_setting( $this->plugin->name, 'ihaf_insert_footer', 'trim' ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Register the plugin settings panel |
| 131 | */ |
| 132 | function adminPanelsAndMetaBoxes() { |
| 133 | add_submenu_page( 'options-general.php', $this->plugin->displayName, $this->plugin->displayName, 'manage_options', $this->plugin->name, array( &$this, 'adminPanel' ) ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Output the Administration Panel |
| 138 | * Save POSTed data from the Administration Panel into a WordPress option |
| 139 | */ |
| 140 | function adminPanel() { |
| 141 | // only admin user can access this page |
| 142 | if ( !current_user_can( 'administrator' ) ) { |
| 143 | echo '<p>' . __( 'Sorry, you are not allowed to access this page.', $this->plugin->name ) . '</p>'; |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | // Save Settings |
| 148 | if ( isset( $_REQUEST['submit'] ) ) { |
| 149 | // Check nonce |
| 150 | if ( !isset( $_REQUEST[$this->plugin->name.'_nonce'] ) ) { |
| 151 | // Missing nonce |
| 152 | $this->errorMessage = __( 'nonce field is missing. Settings NOT saved.', $this->plugin->name ); |
| 153 | } elseif ( !wp_verify_nonce( $_REQUEST[$this->plugin->name.'_nonce'], $this->plugin->name ) ) { |
| 154 | // Invalid nonce |
| 155 | $this->errorMessage = __( 'Invalid nonce specified. Settings NOT saved.', $this->plugin->name ); |
| 156 | } else { |
| 157 | // Save |
| 158 | // $_REQUEST has already been slashed by wp_magic_quotes in wp-settings |
| 159 | // so do nothing before saving |
| 160 | update_option( 'ihaf_insert_header', $_REQUEST['ihaf_insert_header'] ); |
| 161 | update_option( 'ihaf_insert_footer', $_REQUEST['ihaf_insert_footer'] ); |
| 162 | update_option( $this->plugin->db_welcome_dismissed_key, 1 ); |
| 163 | $this->message = __( 'Settings Saved.', $this->plugin->name ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Get latest settings |
| 168 | $this->settings = array( |
| 169 | 'ihaf_insert_header' => esc_html( wp_unslash( get_option( 'ihaf_insert_header' ) ) ), |
| 170 | 'ihaf_insert_footer' => esc_html( wp_unslash( get_option( 'ihaf_insert_footer' ) ) ), |
| 171 | ); |
| 172 | |
| 173 | // Load Settings Form |
| 174 | include_once( WP_PLUGIN_DIR . '/' . $this->plugin->name . '/views/settings.php' ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Loads plugin textdomain |
| 179 | */ |
| 180 | function loadLanguageFiles() { |
| 181 | load_plugin_textdomain( $this->plugin->name, false, $this->plugin->name . '/languages/' ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Outputs script / CSS to the frontend header |
| 186 | */ |
| 187 | function frontendHeader() { |
| 188 | $this->output( 'ihaf_insert_header' ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Outputs script / CSS to the frontend footer |
| 193 | */ |
| 194 | function frontendFooter() { |
| 195 | $this->output( 'ihaf_insert_footer' ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Outputs the given setting, if conditions are met |
| 200 | * |
| 201 | * @param string $setting Setting Name |
| 202 | * @return output |
| 203 | */ |
| 204 | function output( $setting ) { |
| 205 | // Ignore admin, feed, robots or trackbacks |
| 206 | if ( is_admin() || is_feed() || is_robots() || is_trackback() ) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | // Get meta |
| 211 | $meta = get_option( $setting ); |
| 212 | if ( empty( $meta ) ) { |
| 213 | return; |
| 214 | } |
| 215 | if ( trim( $meta ) == '' ) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | // Output |
| 220 | echo wp_unslash( $meta ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | $ihaf = new InsertHeadersAndFooters(); |
| 225 | ?> |