| @@ -1,52 +1,97 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Plugin Name: Easy Footnotes |
| 4 | - * Plugin URI: http://jasonyingling.me/easy-footnotes-wordpress/ | |
| 4 | + * Plugin URI: https://jasonyingling.me/easy-footnotes-wordpress/ | |
| 5 | 5 | * Description: Easily add footnotes to your posts with a simple shortcode. |
| 6 | - * Version: 1.0.2 | |
| 6 | + * Version: 1.1.1 | |
| 7 | 7 | * Author: Jason Yingling |
| 8 | - * Author URI: http://jasonyingling.me | |
| 8 | + * Author URI: https://jasonyingling.me | |
| 9 | 9 | * License: GPL2 |
| 10 | + * | |
| 11 | + * @package easy-footnotes | |
| 10 | 12 | */ |
| 11 | - | |
| 12 | - /* Copyright 2014 Jason Yingling (email : yingling017@gmail.com) | |
| 13 | 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. | |
| 14 | +/* | |
| 15 | + Copyright 2018 Jason Yingling (email : yingling017@gmail.com) | |
| 17 | 16 | |
| 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. | |
| 17 | + This program is free software; you can redistribute it and/or modify | |
| 18 | + it under the terms of the GNU General Public License, version 2, as | |
| 19 | + published by the Free Software Foundation. | |
| 22 | 20 | |
| 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 | |
| 21 | + This program is distributed in the hope that it will be useful, | |
| 22 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 23 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 24 | + GNU General Public License for more details. | |
| 25 | + | |
| 26 | + You should have received a copy of the GNU General Public License | |
| 27 | + along with this program; if not, write to the Free Software | |
| 28 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
| 26 | 29 | */ |
| 30 | +/** | |
| 31 | + * The Easy Footnotes class. In camel case. Because I didn't follow WPCS way back when. | |
| 32 | + */ | |
| 33 | +class easyFootnotes { | |
| 27 | 34 | |
| 28 | -class easyFootnotes { | |
| 29 | - | |
| 30 | - // Add label option using add_option if it does not already exist | |
| 31 | - public $footnotes = array(); | |
| 35 | + /** | |
| 36 | + * Add label option using add_option if it does not already exist. | |
| 37 | + * | |
| 38 | + * @var array $footnotes An array for the footnotes to be stored. | |
| 39 | + */ | |
| 40 | + public $footnotes = array(); | |
| 32 | 41 | public $footnoteCount = 0; |
| 33 | - | |
| 42 | + public $prevPost; | |
| 43 | + public $footnoteOptions; | |
| 44 | + | |
| 45 | + private $footnoteSettings; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Constructing the initial plugin options, shortcodes, and hooks. | |
| 49 | + */ | |
| 34 | 50 | public function __construct() { |
| 35 | - add_shortcode( 'note', array($this, 'easy_footnote_shortcode') ); | |
| 36 | - add_filter('the_content', array($this, 'easy_footnote_after_content'), 999); | |
| 37 | - add_action('wp_enqueue_scripts', array($this, 'register_qtip_scripts')); | |
| 51 | + $footnoteSettings = array( | |
| 52 | + 'footnoteLabel' => __( 'Footnotes', 'easy-footnotes' ), | |
| 53 | + 'useLabel' => false, | |
| 54 | + 'hide_easy_footnote_after_posts' => false, | |
| 55 | + 'show_easy_footnote_on_front' => false, | |
| 56 | + ); | |
| 57 | + | |
| 58 | + add_option( 'easy_footnotes_options', $this->footnoteSettings ); | |
| 59 | + add_shortcode( 'note', array( $this, 'easy_footnote_shortcode' ) ); | |
| 60 | + add_shortcode( 'efn_note', array( $this, 'easy_footnote_shortcode' ) ); | |
| 61 | + add_filter( 'the_content', array( $this, 'easy_footnote_after_content' ), 20 ); | |
| 62 | + add_filter( 'the_content', array( $this, 'easy_footnote_reset' ), 999 ); | |
| 63 | + add_action( 'wp_enqueue_scripts', array( $this, 'register_qtip_scripts' ) ); | |
| 64 | + add_action( 'admin_menu', array( $this, 'easy_footnotes_admin_actions' ) ); | |
| 65 | + add_action( 'admin_enqueue_scripts', array( $this, 'easy_footnotes_admin_scripts' ) ); | |
| 66 | + | |
| 67 | + $this->footnoteOptions = get_option( 'easy_footnotes_options' ); | |
| 38 | 68 | } |
| 39 | - | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Registering the scripts and styles used by jQuery qTip. | |
| 72 | + */ | |
| 40 | 73 | public function register_qtip_scripts() { |
| 41 | - wp_register_script( 'imagesloaded', plugins_url( '/assets/qtip/imagesloaded.pkgd.min.js' , __FILE__ ), null, false, true ); | |
| 42 | - wp_register_script( 'qtip', plugins_url( '/assets/qtip/jquery.qtip.min.js' , __FILE__ ), array('jquery', 'imagesloaded'), false, true ); | |
| 43 | - wp_register_script( 'qtipcall', plugins_url( '/assets/qtip/jquery.qtipcall.js' , __FILE__ ), array('jquery', 'qtip'), false, true ); | |
| 44 | - wp_register_style( 'qtipstyles', plugins_url( '/assets/qtip/jquery.qtip.min.css' , __FILE__ ), null, false, false ); | |
| 45 | - wp_register_style( 'easyfootnotescss', plugins_url( '/assets/easy-footnotes.css' , __FILE__ ), null, false, false ); | |
| 74 | + wp_register_script( 'imagesloaded', plugins_url( '/assets/qtip/imagesloaded.pkgd.min.js', __FILE__ ), array(), '3.1.8', true ); | |
| 75 | + wp_register_script( 'qtip', plugins_url( '/assets/qtip/jquery.qtip.min.js', __FILE__ ), array( 'jquery', 'imagesloaded' ), '3.0.3', true ); | |
| 76 | + wp_register_script( 'qtipcall', plugins_url( '/assets/qtip/jquery.qtipcall.js', __FILE__ ), array( 'jquery', 'qtip' ), '1.1.0', true ); | |
| 77 | + wp_register_style( 'qtipstyles', plugins_url( '/assets/qtip/jquery.qtip.min.css', __FILE__ ), array(), '3.0.3', false ); | |
| 78 | + wp_register_style( 'easyfootnotescss', plugins_url( '/assets/easy-footnotes.css', __FILE__ ), array(), '1.1.0', false ); | |
| 46 | 79 | } |
| 47 | - | |
| 48 | - public function easy_footnote_shortcode($atts, $content = null) { | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Create the Easy Footnotes shortcode. | |
| 83 | + * | |
| 84 | + * @param array $atts Shortcode attributes. | |
| 85 | + * @param string $content Content within the shortcode. | |
| 86 | + */ | |
| 87 | + public function easy_footnote_shortcode( $atts, $content = null ) { | |
| 88 | + if ( isset( $this->footnoteOptions['show_easy_footnote_on_front'] ) && $this->footnoteOptions['show_easy_footnote_on_front'] ) { | |
| 89 | + $efn_show_on_front = is_front_page(); | |
| 90 | + } else { | |
| 91 | + $efn_show_on_front = false; | |
| 92 | + } | |
| 93 | + | |
| 49 | 94 | wp_enqueue_style( 'qtipstyles' ); |
| 50 | 95 | wp_enqueue_style( 'easyfootnotescss' ); |
| 51 | 96 | wp_enqueue_script( 'imagesloaded' ); |
| 52 | 97 | wp_enqueue_script( 'qtip' ); |
| @@ -51,50 +96,155 @@ | ||
| 51 | 96 | wp_enqueue_script( 'imagesloaded' ); |
| 52 | 97 | wp_enqueue_script( 'qtip' ); |
| 53 | 98 | wp_enqueue_script( 'qtipcall' ); |
| 54 | 99 | wp_enqueue_style( 'dashicons' ); |
| 55 | - | |
| 56 | - extract (shortcode_atts(array( | |
| 57 | - ), $atts)); | |
| 58 | - | |
| 59 | - $this->easy_footnote_count($this->footnoteCount); | |
| 60 | - $this->easy_footnote_content($content); | |
| 61 | - | |
| 62 | - $footnoteContent = "<span id='easy-footnote-".$this->footnoteCount."' class='easy-footnote-margin-adjust'></span><span class='easy-footnote'><a href='#easy-footnote-bottom-".$this->footnoteCount."' title='$content'><sup>$this->footnoteCount</sup></a></span>"; | |
| 63 | - | |
| 64 | - | |
| 100 | + | |
| 101 | + $atts = shortcode_atts( | |
| 102 | + array( | |
| 103 | + // Future home of shortcode atts. | |
| 104 | + ), | |
| 105 | + $atts | |
| 106 | + ); | |
| 107 | + | |
| 108 | + $post_id = get_the_ID(); | |
| 109 | + | |
| 110 | + $content = do_shortcode( $content ); | |
| 111 | + | |
| 112 | + $count = $this->footnoteCount; | |
| 113 | + | |
| 114 | + // Increment the counter. | |
| 115 | + $count++; | |
| 116 | + | |
| 117 | + // Set the footnoteCount (This whole process needs reworked). | |
| 118 | + $this->footnoteCount = $count; | |
| 119 | + | |
| 120 | + $this->easy_footnote_content( $content ); | |
| 121 | + | |
| 122 | + if ( ( is_singular() || $efn_show_on_front ) && is_main_query() ) { | |
| 123 | + $footnoteLink = '#easy-footnote-bottom-' . $this->footnoteCount . '-' . $post_id; | |
| 124 | + } else { | |
| 125 | + $footnoteLink = get_permalink( get_the_ID() ) . '#easy-footnote-bottom-' . $this->footnoteCount . '-' . $post_id; | |
| 126 | + } | |
| 127 | + | |
| 128 | + $footnoteContent = "<span id='easy-footnote-" . esc_attr( $this->footnoteCount ) . '-' . $post_id . "' class='easy-footnote-margin-adjust'></span><span class='easy-footnote'><a href='" . esc_url( $footnoteLink ) . "' title='" . htmlspecialchars( $content, ENT_QUOTES ) . "'><sup>" . esc_html( $this->footnoteCount ) . "</sup></a></span>"; | |
| 129 | + | |
| 65 | 130 | return $footnoteContent; |
| 66 | 131 | } |
| 67 | - | |
| 68 | - public function easy_footnote_content($content) { | |
| 69 | - $this->footnotes[$this->footnoteCount] = $content; | |
| 70 | 132 | |
| 133 | + /** | |
| 134 | + * The content of a particular footnote. | |
| 135 | + * | |
| 136 | + * @param string $content The content from a particular call of the shortcode. | |
| 137 | + */ | |
| 138 | + public function easy_footnote_content( $content ) { | |
| 139 | + $this->footnotes[ $this->footnoteCount ] = $content; | |
| 140 | + | |
| 71 | 141 | return $this->footnotes; |
| 72 | 142 | } |
| 73 | - | |
| 74 | - public function easy_footnote_count($count) { | |
| 75 | - $count++; | |
| 76 | - | |
| 77 | - $this->footnoteCount = $count; | |
| 78 | - | |
| 79 | - return $this->footnoteCount; | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Display the list of footnotes after the post content. | |
| 146 | + * | |
| 147 | + * @param string $content The content of the current post. | |
| 148 | + */ | |
| 149 | + public function easy_footnote_after_content( $content ) { | |
| 150 | + if ( isset( $this->footnoteOptions['hide_easy_footnote_after_posts'] ) && $this->footnoteOptions['hide_easy_footnote_after_posts'] ) { | |
| 151 | + return $content; | |
| 152 | + } | |
| 153 | + | |
| 154 | + if ( isset( $this->footnoteOptions['show_easy_footnote_on_front'] ) && $this->footnoteOptions['show_easy_footnote_on_front'] ) { | |
| 155 | + $efn_show_on_front = is_front_page(); | |
| 156 | + } else { | |
| 157 | + $efn_show_on_front = false; | |
| 158 | + } | |
| 159 | + | |
| 160 | + if ( ( is_singular() || $efn_show_on_front ) && is_main_query() ) { | |
| 161 | + $footnotesInsert = $this->footnotes; | |
| 162 | + | |
| 163 | + $footnoteCopy = ''; | |
| 164 | + | |
| 165 | + $useLabel = $this->footnoteOptions['useLabel']; | |
| 166 | + $efLabel = $this->footnoteOptions['footnoteLabel']; | |
| 167 | + | |
| 168 | + $post_id = get_the_ID(); | |
| 169 | + | |
| 170 | + foreach ( $footnotesInsert as $count => $footnote ) { | |
| 171 | + $footnoteCopy .= '<li class="easy-footnote-single"><span id="easy-footnote-bottom-' .esc_attr( $count ) . '-' . $post_id . '" class="easy-footnote-margin-adjust"></span>' . wp_kses_post( $footnote ) . '<a class="easy-footnote-to-top" href="' . esc_url( '#easy-footnote-' . $count . '-' . $post_id ) . '"></a></li>'; | |
| 172 | + } | |
| 173 | + if ( ! empty( $footnotesInsert ) ) { | |
| 174 | + if ( true === $useLabel ) { | |
| 175 | + $footnote_label = '<h4>' . esc_html( $efLabel ) . '</h4>'; | |
| 176 | + // Filter for editing footnote label markup and output. | |
| 177 | + $footnote_label = apply_filters( 'efn_footnote_label', $footnote_label, $efLabel ); | |
| 178 | + | |
| 179 | + $efn_output = sprintf( | |
| 180 | + '<div class="easy-footnote-title"> | |
| 181 | + %s | |
| 182 | + </div>', | |
| 183 | + $footnote_label | |
| 184 | + ); | |
| 185 | + } | |
| 186 | + | |
| 187 | + $footnote_content = ''; | |
| 188 | + | |
| 189 | + // Add filter before footnote list. | |
| 190 | + $footnote_content = apply_filters( 'before_footnote', $footnote_content ); | |
| 191 | + $footnote_content .= '<ol class="easy-footnotes-wrapper">' . $footnoteCopy . '</ol>'; | |
| 192 | + | |
| 193 | + // Add filter after footnote list. | |
| 194 | + $footnote_content = apply_filters( 'after_footnote', $footnote_content ); | |
| 195 | + | |
| 196 | + $efn_output .= $footnote_content; | |
| 197 | + | |
| 198 | + // Add filters for the entire footnote list output. | |
| 199 | + $efn_output = apply_filters( 'efn_footnote_list_output', $efn_output ); | |
| 200 | + | |
| 201 | + $content .= do_shortcode( $efn_output ); | |
| 202 | + } | |
| 203 | + } | |
| 204 | + | |
| 205 | + return $content; | |
| 80 | 206 | } |
| 81 | - | |
| 82 | - // Calculate reading time by running it through the_content | |
| 83 | - public function easy_footnote_after_content($content) { | |
| 84 | - $footnotesInsert = $this->footnotes; | |
| 85 | - global $footnoteCopy; | |
| 86 | - | |
| 87 | - foreach ($footnotesInsert as $count => $footnote) { | |
| 88 | - $footnoteCopy .= '<li class="easy-footnote-single"><span id="easy-footnote-bottom-'.$count.'" class="easy-footnote-margin-adjust"></span>'.$footnote.'<a class="easy-footnote-to-top" href="#easy-footnote-'.$count.'"></a></li>'; | |
| 89 | - } | |
| 90 | - | |
| 91 | - $content .= '<ol class="easy-footnotes-wrapper">'.$footnoteCopy.'</ol>'; | |
| 92 | - | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Reset the footnote count and footnote array each time the_content has been run. | |
| 210 | + * | |
| 211 | + * @param string $content The content of the post from the_content filter. | |
| 212 | + */ | |
| 213 | + public function easy_footnote_reset( $content ) { | |
| 214 | + $this->footnoteCount = 0; | |
| 215 | + | |
| 216 | + $this->footnotes = array(); | |
| 217 | + | |
| 93 | 218 | return $content; |
| 94 | 219 | } |
| 95 | - | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * Include the easy-footnotes-admin.php file. | |
| 223 | + */ | |
| 224 | + public function easy_footnotes_admin() { | |
| 225 | + include 'easy-footnotes-admin.php'; | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * Function to add options page for Easy Footnote Settings. | |
| 230 | + */ | |
| 231 | + public function easy_footnotes_admin_actions() { | |
| 232 | + add_options_page( | |
| 233 | + __( 'Easy Footnotes Settings', 'easy-footnotes' ), | |
| 234 | + __( 'Easy Footnotes', 'easy-footnotes' ), | |
| 235 | + 'manage_options', | |
| 236 | + 'easy-footnotes-settings', | |
| 237 | + array( $this, 'easy_footnotes_admin' ) | |
| 238 | + ); | |
| 239 | + } | |
| 240 | + | |
| 241 | + /** | |
| 242 | + * Function for enqueuring EAsy Footnotes admin scripts. | |
| 243 | + */ | |
| 244 | + public function easy_footnotes_admin_scripts() { | |
| 245 | + wp_enqueue_style( 'easy-footnotes-admin-styles', plugins_url( '/assets/easy-footnotes-admin.css', __FILE__ ), '', '1.0.13' ); | |
| 246 | + wp_enqueue_script( 'easy-footnotes-admin-scripts', plugins_url( '/assets/js/easy-footnotes-admin.js', __FILE__ ), array( 'jquery' ), '1.0.1', true ); | |
| 247 | + } | |
| 96 | 248 | } |
| 97 | 249 | |
| 98 | 250 | $easyFootnotes = new easyFootnotes(); |
| 99 | - | |
| 100 | -?> | |