Skip to content

Commit

Permalink
Merge pull request #56 from WebberZone/variable-location
Browse files Browse the repository at this point in the history
Insert after nth paragraph
  • Loading branch information
ajaydsouza committed Sep 27, 2016
2 parents 14014eb + 98974ef commit 5c4292e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
1 change: 1 addition & 0 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function crp_options() {
$crp_settings['add_to_archives'] = ( isset( $_POST['add_to_archives'] ) ? true : false );

$crp_settings['content_filter_priority'] = absint( $_POST['content_filter_priority'] );
$crp_settings['insert_after_paragraph'] = ( -1 === $_POST['insert_after_paragraph'] || '' === $_POST['insert_after_paragraph'] ) ? -1 : intval( $_POST['insert_after_paragraph'] );
$crp_settings['show_metabox'] = ( isset( $_POST['show_metabox'] ) ? true : false );
$crp_settings['show_metabox_admins'] = ( isset( $_POST['show_metabox_admins'] ) ? true : false );
$crp_settings['show_credit'] = ( isset( $_POST['show_credit'] ) ? true : false );
Expand Down
9 changes: 8 additions & 1 deletion admin/main-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,19 @@

<tr><th scope="row"><label for="content_filter_priority"><?php esc_html_e( 'Display location priority:', 'contextual-related-posts' ); ?></label></th>
<td>
<input type="textbox" name="content_filter_priority" id="content_filter_priority" value="<?php echo esc_attr( stripslashes( $crp_settings['content_filter_priority'] ) ); ?>" />
<input type="textbox" name="content_filter_priority" id="content_filter_priority" value="<?php echo esc_attr( $crp_settings['content_filter_priority'] ); ?>" />
<p class="description"><?php esc_html_e( 'If you select to automatically add the related posts, CRP will hook into the Content Filter at a priority as specified in this option.', 'contextual-related-posts' ); ?></p>
<p class="description"><?php esc_html_e( 'A higher number will cause the related posts to be processed later and move their display further down after the post content. Any number below 10 is not recommended.', 'contextual-related-posts' ); ?></p>
</td>
</tr>

<tr><th scope="row"><label for="insert_after_paragraph"><?php esc_html_e( 'Insert after paragraph number', 'contextual-related-posts' ); ?>:</label></th>
<td>
<input type="textbox" name="insert_after_paragraph" id="insert_after_paragraph" value="<?php echo esc_attr( $crp_settings['insert_after_paragraph'] ); ?>" />
<p class="description"><?php esc_html_e( 'Enter 0 to display the related posts before the post content, -1 to display this at the end or a number to insert it after that paragraph number. If your post has less paragraphs, related posts will be displayed at the end.', 'contextual-related-posts' ); ?></p>
</td>
</tr>

<tr><th scope="row"><label for="show_metabox"><?php esc_html_e( 'Show metabox:', 'contextual-related-posts' ); ?></label></th>
<td>
<input type="checkbox" name="show_metabox" id="show_metabox" <?php checked( true, $crp_settings['show_metabox'] ); ?> />
Expand Down
82 changes: 69 additions & 13 deletions contextual-related-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,27 +630,82 @@ function crp_content_filter( $content ) {
$crp_disable_here = 0;
}

if ( $crp_disable_here ) { return $content; }
if ( $crp_disable_here ) {
return $content;
}

// Else add the content.
if ( ( is_single() ) && ( $crp_settings['add_to_content'] ) ) {
return $content . get_crp( 'is_widget=0' );
} elseif ( ( is_page() ) && ( $crp_settings['add_to_page'] ) ) {
return $content . get_crp( 'is_widget=0' );
} elseif ( ( is_home() ) && ( $crp_settings['add_to_home'] ) ) {
return $content . get_crp( 'is_widget=0' );
} elseif ( ( is_category() ) && ( $crp_settings['add_to_category_archives'] ) ) {
return $content . get_crp( 'is_widget=0' );
} elseif ( ( is_tag() ) && ( $crp_settings['add_to_tag_archives'] ) ) {
return $content . get_crp( 'is_widget=0' );
} elseif ( ( ( is_tax() ) || ( is_author() ) || ( is_date() ) ) && ( $crp_settings['add_to_archives'] ) ) {
return $content . get_crp( 'is_widget=0' );
if ( ( ( is_single() ) && ( $crp_settings['add_to_content'] ) ) ||
( ( is_page() ) && ( $crp_settings['add_to_page'] ) ) ||
( ( is_home() ) && ( $crp_settings['add_to_home'] ) ) ||
( ( is_category() ) && ( $crp_settings['add_to_category_archives'] ) ) ||
( ( is_tag() ) && ( $crp_settings['add_to_tag_archives'] ) ) ||
( ( ( is_tax() ) || ( is_author() ) || ( is_date() ) ) && ( $crp_settings['add_to_archives'] ) ) ) {

$crp_code = get_crp( 'is_widget=0' );

return crp_generate_content( $content, $crp_code );

} else {
return $content;
}
}


/**
* Helper for inserting crp code into or alongside content
*
* @since 2.3.0
*
* @param string $content Post content.
* @param string $crp_code CRP generated code.
* @return string After the filter has been processed
*/
function crp_generate_content( $content, $crp_code ) {
global $crp_settings;

if ( -1 === (int) $crp_settings['insert_after_paragraph'] || ! is_numeric( $crp_settings['insert_after_paragraph'] ) ) {
return $content . $crp_code;
} elseif ( 0 === (int) $crp_settings['insert_after_paragraph'] ) {
return $crp_code . $content;
} else {
return crp_insert_after_paragraph( $content, $crp_code, $crp_settings['insert_after_paragraph'] );
}

}

/**
* Helper for inserting code after a closing paragraph tag
*
* @since 2.3.0
*
* @param string $content Post content.
* @param string $crp_code CRP generated code.
* @param string $paragraph_id Paragraph number to insert after.
* @return string After the filter has been processed
*/
function crp_insert_after_paragraph( $content, $crp_code, $paragraph_id ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );

if ( count( $paragraphs ) >= $paragraph_id ) {
foreach ( $paragraphs as $index => $paragraph ) {

if ( trim( $paragraph ) ) {
$paragraphs[ $index ] .= $closing_p;
}

if ( (int) $paragraph_id === $index + 1 ) {
$paragraphs[ $index ] .= $crp_code;
}
}

return implode( '', $paragraphs );
}

return $content . $crp_code;
}

/**
* Filter to add related posts to feeds.
*
Expand Down Expand Up @@ -767,6 +822,7 @@ function crp_default_options() {
'add_to_archives' => false, // Add related posts to other archives.

'content_filter_priority' => 10, // Content priority.
'insert_after_paragraph' => -1, // Insert after paragraph number.
'show_metabox' => true, // Show metabox to admins.
'show_metabox_admins' => false, // Limit to admins as well.

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ In addition to the above, the shortcode takes every option that the plugin suppo
* Features:
* Shortcode and the widget now have an added parameter for 'offset'. This is useful if you would like to display different widgets/shortcodes but not always start from the first post
* New option in metabox: "Exclude this post from the related posts list"
* New option: Insert after nth paragraph

* Enhancements:
* The generated HTML code uses a single `a href` tag rather than two separate ones per item which is usually better for SEO. If you're not using the Rounded Thumbnail style and using your own custom style, then you might need to reconfigure this
Expand Down

0 comments on commit 5c4292e

Please sign in to comment.