get_content_filter_priority());
$this->settings = new AddWidgetAfterContentAdmin($this->plugin_slug, $this->plugin_version );
}
/**
* Fired when the plugin is activated
*
*/
public static function activate() {
if (get_option('awac_priority') === false) {
update_option('awac_priority', '10');
}
}
/**
* Get the priority to use when filtering the content
*
* @return int
*/
public static function get_content_filter_priority() {
$priority = get_option( 'awac_priority' );
if ( empty( $priority ) || ! is_numeric( $priority ) ) {
// Set the default priority if it's not already set.
$priority = 10;
update_option( 'awac_priority', $priority );
}
// Ensure that the priority is an integer.
return (int) $priority;
}
/**
* Register the widget area/sidebar that will go after the content
*
*/
public function register_sidebar() {
$args = array(
'id' => 'add-widget-after-content',
'name' => 'After Content' ,
'description' => 'This widget section shows after the content, but before comments on single post pages',
'before_widget' => '
',
'after_widget' => '
',
'before_title' => '
',
'after_title' => '
'
);
register_sidebar( apply_filters( 'awac_sidebar_arguments', $args ) );
}
/**
* Return the plugin slug.
*/
public function get_plugin_slug() {
return $this->plugin_slug;
}
/**
* Determines if the widget area should show on the current post/page
* @param int $post_id The id of the current post
* @return bool false if should not show or true if it should show
*/
public static function show_awac($post_id){
$exclude_format = (array)get_option('all_post_formats');
$exclude_type = (array)get_option('all_post_types');
$exclude_category = (array)get_option('all_post_categories');
$ps_type = get_post_type( $post_id );
$ps_format = get_post_format();
$ps_category = get_the_category();
if(!is_singular()){
return false;
}
//should the widget be shown after the content
$ps_hide_widget = get_post_meta( get_the_ID(), '_awac_hide_widget', true );
if( $ps_hide_widget ){
return false;
}
if ( false === $ps_format ) {
$ps_format = 'standard';
}
foreach ($ps_category as $categories) {
$cat = $categories->name;
if(isset($exclude_category[$cat]) == 1){
return false;
}
}
if(isset($exclude_type[$ps_type]) == 1 || isset($exclude_format[$ps_format]) == 1){
return false;
}
return true;
}
/**
* Add the widget after the post content if the widget is not set to be hide
* @param string $content content of the current post
* @return $content the post content plus the widget area content
*/
public function insert_after_content( $content ) {
if ( is_null( $content ) || ! is_string( $content ) ) {
return $content;
}
if ( $this->show_awac( get_the_ID() ) ) {
try {
$awac_content = $this->get_after_content();
$content .= apply_filters( 'awac_content', $awac_content );
} catch ( Exception $e ) {
$content .= apply_filters( 'awac_content', '' );
}
}
return $content;
}
/**
* Get what ever is to be in the widget area, but don't display it yet
* @return string the content of the add-widget-after-content sidebar/widget
*/
public function get_after_content() {
ob_start();
dynamic_sidebar( 'add-widget-after-content' );
$sidebar = ob_get_contents();
ob_end_clean();
return $sidebar;
}
/**
* Add a meta box to admin pages that are not excluded
*/
public function after_content_create_metabox( $post_type ) {
$exclude_type = (array) get_option('all_post_types', array());
$exclude_format = (array) get_option('all_post_formats', array());
$format = get_post_format() ?: 'standard';
// If not an excluded post type and not an excluded post format
if ( ! isset( $exclude_type[$post_type] ) && ! isset( $exclude_format[$format] ) ) {
add_meta_box( 'ps-meta', 'Widget After Content', array( $this, 'after_content_metabox' ), $post_type , 'normal', 'high' );
} else {
remove_meta_box( 'ps-meta', $post_type, 'normal' );
}
}
/**
* Fills the Widget After Content metabox with its content
* @param object $post the post object for the post
*/
public function after_content_metabox( $post ) {
wp_nonce_field( 'awac_save_meta', 'awac_meta_metabox_nonce' );
$ps_hide_widget = get_post_meta( $post->ID, '_awac_hide_widget', true );
$status = checked( $ps_hide_widget, 1, false );
$html = esc_html__( 'Remove widget after content for this post.', 'add-widget-after-content' );
$html .= '