Skip to content

Commit 5d1ae67

Browse files
Restore previous behavior of the logical operator filter
If the logical operator is filtered, it should set the default behavior of the dropdown, etc. See #48
1 parent 3693a62 commit 5d1ae67

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

ad-code-manager.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Ad_Code_Manager
4444
public $plugin_slug = 'ad-code-manager';
4545
public $manage_ads_cap = 'manage_options';
4646
public $post_type_labels;
47-
// public $logical_operator;
47+
public $logical_operator;
4848
public $ad_tag_ids;
4949
public $providers;
5050
public $current_provider_slug;
@@ -170,7 +170,7 @@ function action_init() {
170170
*/
171171
$this->whitelisted_conditionals = apply_filters( 'acm_whitelisted_conditionals', $this->whitelisted_conditionals );
172172
// Allow users to filter default logical operator
173-
// $this->logical_operator = apply_filters( 'acm_logical_operator', 'OR' );
173+
$this->logical_operator = apply_filters( 'acm_logical_operator', 'OR' );
174174

175175
// Allow the ad management cap to be filtered if need be
176176
$this->manage_ads_cap = apply_filters( 'acm_manage_ads_cap', $this->manage_ads_cap );
@@ -225,7 +225,7 @@ function handle_admin_action() {
225225
case 'edit':
226226
$id = ( isset( $_REQUEST['id'] ) ) ? (int)$_REQUEST['id'] : 0;
227227
$priority = ( isset( $_REQUEST['priority'] ) ) ? (int)$_REQUEST['priority'] : 10;
228-
$operator = ( isset( $_REQUEST['operator'] ) && $_REQUEST['operator'] == 'AND' ) ? 'AND' : 'OR';
228+
$operator = ( isset( $_REQUEST['operator'] ) && in_array( $_REQUEST['operator'], array( 'AND', 'OR' ) ) ) ? $_REQUEST['operator'] : $this->logical_operator;
229229
$ad_code_vals = array(
230230
'priority' => $priority,
231231
'operator' => $operator,
@@ -337,7 +337,7 @@ function get_ad_codes( $query_args = array() ) {
337337
$priority = ( !empty( $priority ) ) ? intval( $priority ) : 10;
338338

339339
$operator = get_post_meta( $ad_code_cpt->ID, 'operator', true );
340-
$operator = ( !empty( $operator ) ) ? esc_html( $operator ) : 'OR';
340+
$operator = ( !empty( $operator ) ) ? esc_html( $operator ) : $this->logical_operator;
341341

342342
$ad_codes_formatted[] = array(
343343
'conditionals' => $this->get_conditionals( $ad_code_cpt->ID ),
@@ -373,8 +373,7 @@ function get_ad_code( $post_id ) {
373373
$priority = ( !empty( $priority ) ) ? intval( $priority ) : 10;
374374

375375
$operator = get_post_meta( $post_id, 'operator', true );
376-
$operator = ( !empty( $operator ) ) ? esc_html( $operator ) : 'OR';
377-
$operator = apply_filters( 'acm_logical_operator', $operator );
376+
$operator = ( !empty( $operator ) ) ? esc_html( $operator ) : $this->logical_operator;
378377

379378
$ad_code_formatted = array(
380379
'conditionals' => $this->get_conditionals( $post->ID ),
@@ -703,7 +702,7 @@ function register_scripts_and_styles() {
703702
* @param int $priority Priority of the ad code in comparison to others
704703
* @return bool|WP_Error $success Whether we were successful in registering the ad tag
705704
*/
706-
function register_ad_code( $tag, $url, $conditionals = array(), $url_vars = array(), $priority = 10, $operator = 'OR' ) {
705+
function register_ad_code( $tag, $url, $conditionals = array(), $url_vars = array(), $priority = 10, $operator = false ) {
707706

708707
// Run $url aganist a whitelist to make sure it's a safe URL
709708
if ( !$this->validate_script_url( $url ) )
@@ -719,7 +718,8 @@ function register_ad_code( $tag, $url, $conditionals = array(), $url_vars = arra
719718
$priority = 10;
720719

721720
// Make sure our operator is 'OR' or 'AND'
722-
$operator = $operator == 'AND' ? 'AND' : 'OR';
721+
if ( ! $operator || ! in_array( $operator, array( 'AND', 'OR' ) ) )
722+
$operator = $this->logical_operator;
723723

724724
// Save the ad code to our set of ad codes
725725
$this->ad_codes[$tag][] = array(
@@ -750,7 +750,7 @@ function register_ad_codes( $ad_codes = array() ) {
750750
'conditionals' => array(),
751751
'url_vars' => array(),
752752
'priority' => 10,
753-
'operator' => 'OR',
753+
'operator' => $this->logical_operator,
754754
);
755755
$ad_code = array_merge( $default, $ad_code );
756756

@@ -764,7 +764,8 @@ function register_ad_codes( $ad_codes = array() ) {
764764
$ad_code['priority'] = strlen( $ad_code['priority'] ) == 0 ? 10 : intval( $ad_code['priority'] ); //make sure priority is int, if it's unset, we set it to 10
765765

766766
// Make sure our operator is 'OR' or 'AND'
767-
$ad_code['operator'] = $ad_code['operator'] == 'AND' ? 'AND' : 'OR';
767+
if ( ! $ad_code['operator'] || ! in_array( $ad_code['operator'], array( 'AND', 'OR' ) ) )
768+
$operator = $this->logical_operator;
768769

769770
$this->register_ad_code( $default_tag['tag'], apply_filters( 'acm_default_url', $ad_code['url'] ), $ad_code['conditionals'], array_merge( $default_tag['url_vars'], $ad_code['url_vars'] ), $ad_code['priority'], $ad_code['operator'] );
770771
}

common/lib/acm-wp-list-table.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,14 @@ function single_row( $item ) {
140140
* @return string $output What will be rendered
141141
*/
142142
function column_default( $item, $column_name ) {
143+
global $ad_code_manager;
143144

144145
switch( $column_name ) {
145146
case 'priority':
146147
return esc_html( $item['priority'] );
147148
break;
149+
case 'operator':
150+
return ( ! empty( $item['operator'] ) ) ? $item['operator'] : $ad_code_manager->logical_operator;
148151
default:
149152
// Handle custom columns, if any
150153
if ( isset( $item['url_vars'][$column_name] ) )
@@ -220,8 +223,13 @@ function column_id( $item ) {
220223
$output .= '<div class="acm-operator-field">';
221224
$output .= '<h4 class="acm-section-label">' . __( 'Logical Operator', 'ad-code-manager' ) . '</h4>';
222225
$output .= '<select name="operator">';
223-
$output .= '<option ' . selected( esc_attr( $item['operator'] ), 'OR' ) . '>OR</option>';
224-
$output .= '<option ' . selected( esc_attr( $item['operator'] ), 'AND' ) . '>AND</option>';
226+
$operators = array(
227+
'OR' => __( 'OR', 'ad-code-manager' ),
228+
'AND' => __( 'AND', 'ad-code-manager' ),
229+
);
230+
foreach( $operators as $key => $label ) {
231+
$output .= '<option ' . selected( $item['operator'], $key ) . '>' . esc_attr( $label ) . '</option>';
232+
}
225233
$output .= '</select>';
226234
$output .= '</div>';
227235

@@ -238,18 +246,6 @@ function column_name( $item ) {
238246
return $output;
239247
}
240248

241-
/**
242-
* Display the logical operator for this ad code
243-
*
244-
* @since 0.2
245-
*/
246-
function column_operator( $item ) {
247-
if ( empty( $item['operator'] ) )
248-
return esc_html( 'OR' );
249-
250-
return esc_html( $item['operator'] );
251-
}
252-
253249
/**
254250
* Display the conditionals for this ad code
255251
*

0 commit comments

Comments
 (0)