| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Easy Footnotes |
| 4 |
* Plugin URI: http://jasonyingling.me/easy-footnotes-wordpress/ |
| 5 |
* Description: Easily add footnotes to your posts with a simple shortcode. |
| 6 |
* Version: 1.0.10 |
| 7 |
* Author: Jason Yingling |
| 8 |
* Author URI: http://jasonyingling.me |
| 9 |
* License: GPL2 |
| 10 |
*/ |
| 11 |
|
| 12 |
/* Copyright 2017 Jason Yingling (email : yingling017@gmail.com) |
| 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 |
class easyFootnotes { |
| 29 |
|
| 30 |
// Add label option using add_option if it does not already exist |
| 31 |
public $footnotes = array(); |
| 32 |
public $footnoteCount = 0; |
| 33 |
public $prevPost; |
| 34 |
|
| 35 |
public function __construct() { |
| 36 |
$footnoteSettings = array( |
| 37 |
'footnoteLabel' => 'Footnotes', |
| 38 |
'useLabel' => false, |
| 39 |
); |
| 40 |
|
| 41 |
add_option('easy_footnotes_options', $footnoteSettings); |
| 42 |
add_shortcode( 'note', array($this, 'easy_footnote_shortcode') ); |
| 43 |
add_filter('the_content', array($this, 'easy_footnote_after_content'), 20); |
| 44 |
add_action('wp_enqueue_scripts', array($this, 'register_qtip_scripts')); |
| 45 |
add_action('admin_menu', array($this, 'easy_footnotes_admin_actions')); |
| 46 |
} |
| 47 |
|
| 48 |
public function register_qtip_scripts() { |
| 49 |
wp_register_script( 'imagesloaded', plugins_url( '/assets/qtip/imagesloaded.pkgd.min.js' , __FILE__ ), null, false, true ); |
| 50 |
wp_register_script( 'qtip', plugins_url( '/assets/qtip/jquery.qtip.min.js' , __FILE__ ), array('jquery', 'imagesloaded'), false, true ); |
| 51 |
wp_register_script( 'qtipcall', plugins_url( '/assets/qtip/jquery.qtipcall.js' , __FILE__ ), array('jquery', 'qtip'), false, true ); |
| 52 |
wp_register_style( 'qtipstyles', plugins_url( '/assets/qtip/jquery.qtip.min.css' , __FILE__ ), null, false, false ); |
| 53 |
wp_register_style( 'easyfootnotescss', plugins_url( '/assets/easy-footnotes.css' , __FILE__ ), null, false, false ); |
| 54 |
} |
| 55 |
|
| 56 |
public function easy_footnote_shortcode($atts, $content = null) { |
| 57 |
wp_enqueue_style( 'qtipstyles' ); |
| 58 |
wp_enqueue_style( 'easyfootnotescss' ); |
| 59 |
wp_enqueue_script( 'imagesloaded' ); |
| 60 |
wp_enqueue_script( 'qtip' ); |
| 61 |
wp_enqueue_script( 'qtipcall' ); |
| 62 |
wp_enqueue_style( 'dashicons' ); |
| 63 |
|
| 64 |
extract (shortcode_atts(array( |
| 65 |
), $atts)); |
| 66 |
|
| 67 |
$this->easy_footnote_count($this->footnoteCount, get_the_ID()); |
| 68 |
$this->easy_footnote_content($content); |
| 69 |
|
| 70 |
if (is_singular() && is_main_query()) { |
| 71 |
$footnoteLink = '#easy-footnote-bottom-'.$this->footnoteCount; |
| 72 |
} else { |
| 73 |
$footnoteLink = get_permalink(get_the_ID()).'#easy-footnote-bottom-'.$this->footnoteCount; |
| 74 |
} |
| 75 |
|
| 76 |
$footnoteContent = "<span id='easy-footnote-".esc_attr($this->footnoteCount)."' 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>"; |
| 77 |
|
| 78 |
return $footnoteContent; |
| 79 |
} |
| 80 |
|
| 81 |
public function easy_footnote_content($content) { |
| 82 |
$this->footnotes[$this->footnoteCount] = $content; |
| 83 |
|
| 84 |
return $this->footnotes; |
| 85 |
} |
| 86 |
|
| 87 |
public function easy_footnote_count($count, $currentPost) { |
| 88 |
if ($this->prevPost != $currentPost) { |
| 89 |
$count = 0; |
| 90 |
} |
| 91 |
|
| 92 |
$this->prevPost = $currentPost; |
| 93 |
|
| 94 |
$count++; |
| 95 |
|
| 96 |
$this->footnoteCount = $count; |
| 97 |
|
| 98 |
return $this->footnoteCount; |
| 99 |
} |
| 100 |
|
| 101 |
// Calculate reading time by running it through the_content |
| 102 |
public function easy_footnote_after_content($content) { |
| 103 |
if (is_singular() && is_main_query()) { |
| 104 |
$footnotesInsert = $this->footnotes; |
| 105 |
global $footnoteCopy; |
| 106 |
|
| 107 |
$footnoteOptions = get_option('easy_footnotes_options'); |
| 108 |
$useLabel = $footnoteOptions['useLabel']; |
| 109 |
$efLabel = $footnoteOptions['footnoteLabel']; |
| 110 |
|
| 111 |
foreach ($footnotesInsert as $count => $footnote) { |
| 112 |
$footnoteCopy .= '<li class="easy-footnote-single"><span id="easy-footnote-bottom-'.esc_attr($count).'" class="easy-footnote-margin-adjust"></span>'.wp_kses_post($footnote).'<a class="easy-footnote-to-top" href="'.esc_url('#easy-footnote-'.$count).'"></a></li>'; |
| 113 |
} |
| 114 |
if (!empty($footnotesInsert)) { |
| 115 |
if ($useLabel === true) { |
| 116 |
$content .= '<div class="easy-footnote-title"><h4>'.esc_html($efLabel).'</h4></div><ol class="easy-footnotes-wrapper">'.$footnoteCopy.'</ol>'; |
| 117 |
} else { |
| 118 |
$content .= '<ol class="easy-footnotes-wrapper">'.$footnoteCopy.'</ol>'; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
} |
| 124 |
return $content; |
| 125 |
} |
| 126 |
|
| 127 |
// Functions to create Reading Time admin pages |
| 128 |
public function easy_footnotes_admin() { |
| 129 |
include('easy-footnotes-admin.php'); |
| 130 |
} |
| 131 |
|
| 132 |
public function easy_footnotes_admin_actions() { |
| 133 |
add_options_page("Easy Footnotes Settings", "Easy Footnotes", "manage_options", "easy-footnotes-settings", array($this, "easy_footnotes_admin")); |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
$easyFootnotes = new easyFootnotes(); |
| 139 |
|