/wp-content/plugins/insert-headers-and-footers/ihaf.php

https://bitbucket.org/carloskikea/helpet · PHP · 224 lines · 107 code · 27 blank · 90 comment · 21 complexity · b735e43c1edd656877106a4e6caf0e36 MD5 · raw file

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