| 1 |
<?php |
| 2 |
//ini_set('log_errors', true); |
| 3 |
//ini_set('error_log', dirname(__FILE__).'/awac_errors.log'); |
| 4 |
/** |
| 5 |
* |
| 6 |
* @package Add Widget After Content |
| 7 |
* @author Arelthia Phillips |
| 8 |
* @license GPL-3.0+ |
| 9 |
* @link https://arelthiaphillips.com/products/add-widget-after-content/ |
| 10 |
* @copyright Copyright (C) 2014-2020 Arelthia Phillips |
| 11 |
* |
| 12 |
* Plugin Name: Add Widget After Content |
| 13 |
* Description: This plugin adds a widget area after post content before the comments. You can also tell it not to display on a specific post or post format. |
| 14 |
* Plugin URI: https://wordpress.org/plugins/add-widget-after-content/ |
| 15 |
* Author: Arelthia Phillips |
| 16 |
* Author URI: https://arelthiaphillips.com |
| 17 |
* Version: 2.5.4 |
| 18 |
* License: GPL-3.0+ |
| 19 |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html |
| 20 |
* Text Domain: add-widget-after-content |
| 21 |
*/ |
| 22 |
// If this file is called directly, abort. |
| 23 |
if ( ! defined( 'WPINC' ) ) { |
| 24 |
die; |
| 25 |
} |
| 26 |
define( 'AWAC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
| 27 |
define( 'AWAC_PLUGIN_FILE', __FILE__ ); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
require_once(AWAC_PLUGIN_DIR . 'add-widget-after-content-admin.php'); |
| 32 |
|
| 33 |
if ( !class_exists( 'AddWidgetAfterContent' ) ) { |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* @package AddWidgetAfterContent |
| 38 |
* @author Arelthia Phillips |
| 39 |
*/ |
| 40 |
class AddWidgetAfterContent { |
| 41 |
/** |
| 42 |
* Unique identifier for your plugin. |
| 43 |
* |
| 44 |
* The variable name is used as the text domain when internationalizing strings |
| 45 |
* of text. |
| 46 |
* |
| 47 |
* @since 2.0.1 |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
protected $plugin_slug = 'add-widget-after-content'; |
| 52 |
protected $plugin_version = '2.5.4'; |
| 53 |
protected $settings; |
| 54 |
/** |
| 55 |
* Initialize the plugin |
| 56 |
* @access public |
| 57 |
* @return AddWidgetAfterContent |
| 58 |
*/ |
| 59 |
function __construct() { |
| 60 |
|
| 61 |
add_action( 'init', array( $this, 'load_textdomain' ) ); |
| 62 |
add_action( 'widgets_init', array( $this,'register_sidebar')); |
| 63 |
add_action( 'add_meta_boxes', array( $this,'after_content_create_metabox') ); |
| 64 |
add_action( 'save_post', array( $this,'after_content_save_meta') ); |
| 65 |
add_filter( 'the_content', array( $this,'insert_after_content'), $this->get_content_filter_priority()); |
| 66 |
$this->settings = new AddWidgetAfterContentAdmin($this->plugin_slug, $this->plugin_version ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Fired when the plugin is activated |
| 71 |
* |
| 72 |
*/ |
| 73 |
public static function activate() { |
| 74 |
|
| 75 |
if (get_option('awac_priority') === false) { |
| 76 |
update_option('awac_priority', '10'); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Get the priority to use when filtering the content |
| 84 |
* |
| 85 |
* @return int |
| 86 |
*/ |
| 87 |
public static function get_content_filter_priority() { |
| 88 |
$priority = get_option( 'awac_priority' ); |
| 89 |
|
| 90 |
if ( empty( $priority ) || ! is_numeric( $priority ) ) { |
| 91 |
// Set the default priority if it's not already set. |
| 92 |
$priority = 10; |
| 93 |
update_option( 'awac_priority', $priority ); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
// Ensure that the priority is an integer. |
| 98 |
return (int) $priority; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Register the widget area/sidebar that will go after the content |
| 103 |
* |
| 104 |
*/ |
| 105 |
public function register_sidebar() { |
| 106 |
$args = array( |
| 107 |
'id' => 'add-widget-after-content', |
| 108 |
'name' => 'After Content' , |
| 109 |
'description' => 'This widget section shows after the content, but before comments on single post pages', |
| 110 |
'before_widget' => '<div class="awac-wrapper"><div class="awac widget %1$s">', |
| 111 |
'after_widget' => '</div></div>', |
| 112 |
'before_title' => '<h4 class="widget-title">', |
| 113 |
'after_title' => '</h4>' |
| 114 |
); |
| 115 |
|
| 116 |
register_sidebar( apply_filters( 'awac_sidebar_arguments', $args ) ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Return the plugin slug. |
| 121 |
*/ |
| 122 |
public function get_plugin_slug() { |
| 123 |
return $this->plugin_slug; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Determines if the widget area should show on the current post/page |
| 129 |
* @param int $post_id The id of the current post |
| 130 |
* @return bool false if should not show or true if it should show |
| 131 |
*/ |
| 132 |
public static function show_awac($post_id){ |
| 133 |
$exclude_format = (array)get_option('all_post_formats'); |
| 134 |
$exclude_type = (array)get_option('all_post_types'); |
| 135 |
$exclude_category = (array)get_option('all_post_categories'); |
| 136 |
$ps_type = get_post_type( $post_id ); |
| 137 |
$ps_format = get_post_format(); |
| 138 |
$ps_category = get_the_category(); |
| 139 |
|
| 140 |
if(!is_singular()){ |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
//should the widget be shown after the content |
| 145 |
$ps_hide_widget = get_post_meta( get_the_ID(), '_awac_hide_widget', true ); |
| 146 |
if( $ps_hide_widget ){ |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
if ( false === $ps_format ) { |
| 151 |
$ps_format = 'standard'; |
| 152 |
} |
| 153 |
|
| 154 |
foreach ($ps_category as $categories) { |
| 155 |
$cat = $categories->name; |
| 156 |
if(isset($exclude_category[$cat]) == 1){ |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
if(isset($exclude_type[$ps_type]) == 1 || isset($exclude_format[$ps_format]) == 1){ |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
return true; |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* Add the widget after the post content if the widget is not set to be hide |
| 173 |
* @param string $content content of the current post |
| 174 |
* @return $content the post content plus the widget area content |
| 175 |
*/ |
| 176 |
public function insert_after_content( $content ) { |
| 177 |
if ( is_null( $content ) || ! is_string( $content ) ) { |
| 178 |
return $content; |
| 179 |
} |
| 180 |
|
| 181 |
if ( $this->show_awac( get_the_ID() ) ) { |
| 182 |
try { |
| 183 |
$awac_content = $this->get_after_content(); |
| 184 |
$content .= apply_filters( 'awac_content', $awac_content ); |
| 185 |
} catch ( Exception $e ) { |
| 186 |
$content .= apply_filters( 'awac_content', '' ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $content; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Get what ever is to be in the widget area, but don't display it yet |
| 196 |
* @return string the content of the add-widget-after-content sidebar/widget |
| 197 |
*/ |
| 198 |
public function get_after_content() { |
| 199 |
ob_start(); |
| 200 |
dynamic_sidebar( 'add-widget-after-content' ); |
| 201 |
$sidebar = ob_get_contents(); |
| 202 |
ob_end_clean(); |
| 203 |
return $sidebar; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Add a meta box to admin pages that are not excluded |
| 208 |
*/ |
| 209 |
public function after_content_create_metabox( $post_type ) { |
| 210 |
|
| 211 |
$exclude_type = (array) get_option('all_post_types', array()); |
| 212 |
|
| 213 |
$exclude_format = (array) get_option('all_post_formats', array()); |
| 214 |
|
| 215 |
$format = get_post_format() ?: 'standard'; |
| 216 |
|
| 217 |
// If not an excluded post type and not an excluded post format |
| 218 |
if ( ! isset( $exclude_type[$post_type] ) && ! isset( $exclude_format[$format] ) ) { |
| 219 |
add_meta_box( 'ps-meta', 'Widget After Content', array( $this, 'after_content_metabox' ), $post_type , 'normal', 'high' ); |
| 220 |
} else { |
| 221 |
remove_meta_box( 'ps-meta', $post_type, 'normal' ); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Fills the Widget After Content metabox with its content |
| 227 |
* @param object $post the post object for the post |
| 228 |
*/ |
| 229 |
public function after_content_metabox( $post ) { |
| 230 |
|
| 231 |
wp_nonce_field( 'awac_save_meta', 'awac_meta_metabox_nonce' ); |
| 232 |
$ps_hide_widget = get_post_meta( $post->ID, '_awac_hide_widget', true ); |
| 233 |
$status = checked( $ps_hide_widget, 1, false ); |
| 234 |
$html = esc_html__( 'Remove widget after content for this post.', 'add-widget-after-content' ); |
| 235 |
$html .= '<p>' . esc_html__( 'Yes', 'add-widget-after-content' ) . ': <input type="checkbox" name="ps_hide_widget"'; |
| 236 |
$html .= ' ' . esc_attr( $status) . ' /> ' ; |
| 237 |
$html .= '</p>'; |
| 238 |
|
| 239 |
echo apply_filters( 'awac_metabox_content', $html ); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
/** |
| 244 |
* Saves _awac_hide_widget when the post is saved |
| 245 |
* @param int $post_id the id of the current post being saved |
| 246 |
* @throws Exception if $_POST is not set |
| 247 |
*/ |
| 248 |
public function after_content_save_meta( $post_id) { |
| 249 |
|
| 250 |
if ( ! isset( $_POST ) ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
$nonce = isset( $_POST['awac_meta_metabox_nonce'] ) ? stripslashes( $_POST['awac_meta_metabox_nonce'] ) : ''; |
| 255 |
|
| 256 |
if ( ! wp_verify_nonce( $nonce, 'awac_save_meta' ) ) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
$value = isset( $_POST['ps_hide_widget'] ) |
| 261 |
? (bool) $_POST['ps_hide_widget'] |
| 262 |
: false; |
| 263 |
|
| 264 |
update_post_meta( $post_id, '_awac_hide_widget', $value ); |
| 265 |
do_action( 'awac_after_save_meta'); |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Load the plugin text domain for translation. |
| 271 |
* |
| 272 |
*/ |
| 273 |
public function load_textdomain() { |
| 274 |
|
| 275 |
$domain = $this->plugin_slug; |
| 276 |
if ( empty( $domain ) ) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
$locale = apply_filters( 'plugin_locale', get_locale(), $domain ); |
| 281 |
|
| 282 |
$plugin_dir = plugin_dir_path( dirname( __FILE__ ) ); |
| 283 |
if ( ! is_dir( $plugin_dir ) || ! is_readable( $plugin_dir ) ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' ); |
| 288 |
load_plugin_textdomain( $domain ); |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
}//end class AddWidgetAfterContent |
| 296 |
|
| 297 |
}//end check for class |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
if( class_exists( 'AddWidgetAfterContent' ) ) { |
| 302 |
|
| 303 |
/** |
| 304 |
* Register callback to be fired when plugin is activated |
| 305 |
*/ |
| 306 |
register_activation_hook( __FILE__, array( 'AddWidgetAfterContent', 'activate' ) ); |
| 307 |
|
| 308 |
/** |
| 309 |
* Register callback to be fired when plugin is uninstalled |
| 310 |
*/ |
| 311 |
//register_uninstall_hook( __FILE__, array( 'AddWidgetAfterContent','uninstall' ) ); |
| 312 |
|
| 313 |
/** |
| 314 |
* instantiate the plugin class |
| 315 |
*/ |
| 316 |
$AWAC = new AddWidgetAfterContent(); |
| 317 |
} |