Skip to content

Commit 3d70a6c

Browse files
authored
Merge pull request #101 from yellowtree/shortcode_else
Add else to show_if attribute
2 parents 38c8be6 + c2a351e commit 3d70a6c

File tree

4 files changed

+147
-12
lines changed

4 files changed

+147
-12
lines changed

readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ If you use Maxmind "Automatic download" then you need to upgrade to this plugin
167167

168168
== Changelog ==
169169

170-
= 4.0.2 =
170+
171+
= 4.1.0 =
172+
* NEW: An `else` shortcode for `geoip_detect2_show_if` and `geoip_detect2_hide_if`: `[geoip_detect2_show_if city="Berlin"]Hallo Berlin![else]Not in Berlin[/geoip_detect2_show_if]`
171173
* FIX: Revert more Maxmind libraries to fix incompatibility with WooCommerce
172174
* FIX: The JS for AJAX wasn't working for Safari browsers
173175
* FIX: Improving some edge cases of Record.get_with_locales() to be consistent with non-AJAX mode

shortcodes/show_if.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,42 @@
6262
* Show TEXT if the visitor is from Berlin OR France (since 4.0.0)
6363
* `[geoip_detect2_show_if city="Berlin" operator="or" country="France"]TEXT[/geoip_detect2_show_if]`
6464
*
65+
* Show TEXT if the visitor is from Berlin, otherwise show OTHER (since 4.1.0)
66+
* `[geoip_detect2_show_if city="Berlin"]TEXT[else]OTHER[/geoip_detect2_show_if]`
67+
*
6568
* LIMITATIONS:
6669
* - You cannot nest several of these shortcodes within one another. Instead, seperate them into several blocks of shortcodes.
6770
* - City names can be ambigous. For example, [geoip_detect2_show_if country="US,FR" not_city="Paris"] will exclude both Paris in France and Paris in Texas, US. Instead, you can find out the geoname_id or seperate the shortcode to make it more specific.
6871
* - Conditions can either be combined by AND or OR. It is not possible to write this condition within a shortcode: (city = Berlin AND country = Germany) OR country = France
6972
*/
70-
function geoip_detect2_shortcode_show_if($attr, $content = null, $shortcodeName = '') {
73+
function geoip_detect2_shortcode_show_if($attr, $content = '', $shortcodeName = '') {
7174
$shortcode_options = _geoip_detect2_shortcode_options($attr);
7275
$options = array('skipCache' => $shortcode_options['skip_cache']);
7376

7477
$showContentIfMatch = ($shortcodeName === 'geoip_detect2_show_if');
7578

7679
$attr = (array) $attr;
80+
81+
$else_seperator = apply_filters('geoip_detect2_shortcode_show_if_else_seperator', '[else]');
82+
$parts = explode($else_seperator, $content, 2);
83+
$content_if = $parts[0];
84+
$content_else = isset($parts[1]) ? $parts[1] : '';
7785

7886
$parsed = geoip_detect2_shortcode_parse_conditions_from_attributes($attr, !$showContentIfMatch);
7987

8088
if (geoip_detect2_shortcode_is_ajax_mode($attr)) {
8189
geoip_detect2_enqueue_javascript('shortcode');
90+
8291
$shortcode_options['parsed'] = $parsed;
83-
$innerHTML= do_shortcode($content);
84-
return _geoip_detect2_create_placeholder('span', [ 'class' => 'js-geoip-detect-show-if', 'style' => 'display: none !important' ], $shortcode_options, $innerHTML);
92+
$span_attributes = [ 'class' => 'js-geoip-detect-show-if', 'style' => 'display: none !important' ];
93+
$span_tagname = 'span'; // TODO: Should be 'div' under certain cirumstances, to test
94+
95+
$span_if = _geoip_detect2_create_placeholder($span_tagname, $span_attributes, $shortcode_options, do_shortcode($content_if));
96+
97+
$shortcode_options['parsed']['not'] = ($shortcode_options['parsed']['not'] === 1 ? 0 : 1); // negate
98+
$span_else = _geoip_detect2_create_placeholder($span_tagname, $span_attributes, $shortcode_options, do_shortcode($content_else));
99+
100+
return $span_if . $span_else;
85101
} else {
86102
$info = geoip_detect2_get_info_from_current_ip($shortcode_options['lang'], $options);
87103

@@ -97,11 +113,12 @@ function geoip_detect2_shortcode_show_if($attr, $content = null, $shortcodeName
97113
$info = apply_filters('geoip_detect2_shortcode_show_if_ip_info_override', $info, $attr, $showContentIfMatch);
98114

99115
$evaluated = geoip_detect2_shortcode_evaluate_conditions($parsed, $info);
100-
// All Criteria Passed?
116+
101117
if ($evaluated) {
102-
return do_shortcode($content);
118+
return do_shortcode($content_if);
119+
} else {
120+
return do_shortcode($content_else);
103121
}
104-
return '';
105122
}
106123
}
107124
add_shortcode('geoip_detect2_show_if', 'geoip_detect2_shortcode_show_if');
@@ -130,9 +147,7 @@ function geoip_detect2_shortcode_parse_conditions_from_attributes(array $attr, b
130147
$parsed = [
131148
'op' => ( !empty($attr['operator']) && strtolower($attr['operator']) === 'or' ) ? 'or' : 'and',
132149
];
133-
if ($hide_if) {
134-
$parsed['not'] = 1;
135-
}
150+
$parsed['not'] = $hide_if ? 1 : 0;
136151

137152
$conditions = [];
138153

0 commit comments

Comments
 (0)