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