| 1 |
<?php |
| 2 |
|
| 3 |
class WidgetContextWordCount { |
| 4 |
|
| 5 |
private $wc; |
| 6 |
|
| 7 |
private $words_on_page = 0; |
| 8 |
|
| 9 |
public function __construct( $plugin ) { |
| 10 |
$this->wc = $plugin; |
| 11 |
} |
| 12 |
|
| 13 |
public function init() { |
| 14 |
// Check the number of words on page |
| 15 |
add_action( 'wp', array( $this, 'count_words_on_page' ) ); |
| 16 |
|
| 17 |
// Define our context |
| 18 |
add_filter( 'widget_contexts', array( $this, 'add_word_count_context' ) ); |
| 19 |
|
| 20 |
add_filter( 'widget_context_control-word_count', array( $this, 'control_word_count' ), 10, 2 ); |
| 21 |
add_filter( 'widget_context_check-word_count', array( $this, 'context_check_word_count' ), 10, 2 ); |
| 22 |
} |
| 23 |
|
| 24 |
function add_word_count_context( $contexts ) { |
| 25 |
$contexts['word_count'] = array( |
| 26 |
'label' => __( 'Word Count', 'widget-context' ), |
| 27 |
'description' => __( 'Match based on the post and page word count.', 'widget-context' ), |
| 28 |
'weight' => 15, |
| 29 |
); |
| 30 |
|
| 31 |
return $contexts; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
function count_words_on_page() { |
| 36 |
global $wp_query; |
| 37 |
|
| 38 |
if ( empty( $wp_query->posts ) || is_admin() ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
foreach ( $wp_query->posts as $post_data ) { |
| 43 |
$this->words_on_page += str_word_count( strip_tags( strip_shortcodes( $post_data->post_content ) ) ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
function context_check_word_count( $check, $settings ) { |
| 49 |
$settings = wp_parse_args( |
| 50 |
$settings, |
| 51 |
array( |
| 52 |
'check_wordcount' => false, |
| 53 |
'word_count' => null, |
| 54 |
'check_wordcount_type' => null, |
| 55 |
) |
| 56 |
); |
| 57 |
|
| 58 |
// Make sure this context check was enabled |
| 59 |
if ( ! $settings['check_wordcount'] ) { |
| 60 |
return $check; |
| 61 |
} |
| 62 |
|
| 63 |
$word_count = (int) $settings['word_count']; |
| 64 |
|
| 65 |
// No word count specified, bail out |
| 66 |
if ( ! $word_count ) { |
| 67 |
return $check; |
| 68 |
} |
| 69 |
|
| 70 |
if ( 'less' === $settings['check_wordcount_type'] && $this->words_on_page < $word_count ) { |
| 71 |
return true; |
| 72 |
} elseif ( 'more' === $settings['check_wordcount_type'] && $this->words_on_page > $word_count ) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
return $check; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
function control_word_count( $control_args ) { |
| 81 |
return sprintf( |
| 82 |
'<p>%s %s %s</p>', |
| 83 |
$this->wc->make_simple_checkbox( $control_args, 'check_wordcount', __( 'Has', 'widget-context' ) ), |
| 84 |
$this->wc->make_simple_dropdown( |
| 85 |
$control_args, |
| 86 |
'check_wordcount_type', |
| 87 |
array( |
| 88 |
'less' => __( 'less', 'widget-context' ), |
| 89 |
'more' => __( |
| 90 |
'more', |
| 91 |
'widget-context' |
| 92 |
), |
| 93 |
), |
| 94 |
null, |
| 95 |
__( 'than', 'widget-context' ) |
| 96 |
), |
| 97 |
$this->wc->make_simple_textfield( $control_args, 'word_count', null, __( 'words', 'widget-context' ) ) |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
|