Skip to content

Commit 1de3a6d

Browse files
authored
Merge pull request #9 from SolidDigital/feature/display-conditions
add display conditions
2 parents afe08f7 + 3da93d4 commit 1de3a6d

5 files changed

Lines changed: 116 additions & 3 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Solid;
4+
5+
class DisplayConditionSolidDynamicsMacro extends \ElementorPro\Modules\DisplayConditions\Conditions\Base\Condition_Base {
6+
public function get_name() {
7+
return 'solid_dynamics_macro';
8+
}
9+
10+
public function get_label() {
11+
return esc_html__( 'Solid Dynamics Macro', 'solid-dynamics' );
12+
}
13+
14+
public function get_group() {
15+
return 'other';
16+
}
17+
18+
public function check( $args ) : bool {
19+
$value = solid_dynamics_macro($args['macro']);
20+
return boolval($value);
21+
}
22+
23+
public function get_options() {
24+
$this->add_control(
25+
'macro',
26+
[
27+
'label' => __( 'Macro', 'solid-dynamics' ),
28+
'type' => \Elementor\Controls_Manager::TEXT,
29+
]
30+
);
31+
}
32+
}
33+
34+
function solid_dynamics_macro($macro) {
35+
$items = explode('|', $macro);
36+
$type = $items[0] ?? '';
37+
$field = $items[1] ?? '';
38+
39+
// TODO: add index for repeater fields.
40+
// $index = $items[2];
41+
42+
if (empty($type) || empty($field)) {
43+
return;
44+
}
45+
46+
switch ($type) {
47+
case 'post':
48+
global $post;
49+
50+
if ($post->$field !== null) {
51+
return $post->$field;
52+
}
53+
54+
return get_post_meta($post->ID, $field, true);
55+
case 'user':
56+
$user = wp_get_current_user();
57+
58+
if ($user->$field !== null) {
59+
return $user->$field;
60+
}
61+
62+
return get_user_meta($user->ID, $field, true);
63+
64+
case 'function':
65+
if (is_callable($field)) {
66+
return $field();
67+
};
68+
69+
return;
70+
// TODO: add option
71+
// case 'option':
72+
// $option = get_option($field);
73+
// return $option;
74+
// TODO: add author
75+
// case 'author':
76+
// $author = get_queried_object();
77+
// return $author->name;
78+
// TODO: add term
79+
// case 'term':
80+
// $term = get_queried_object();
81+
// return $term->name;
82+
}
83+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace Solid;
3+
4+
class ElementorDisplayConditions {
5+
function __construct() {
6+
add_action( 'elementor/display_conditions/register', [$this, 'elementor_display_conditions_register_conditions'] );
7+
}
8+
9+
function elementor_display_conditions_register_conditions($conditions_manager) {
10+
require_once( __DIR__ . "/display-condition-solid-dynamics-macro.php" );
11+
12+
$conditions_manager->register_condition_instance( new DisplayConditionSolidDynamicsMacro() );
13+
}
14+
}

classes/post-field.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function _register_controls() {
2626
$this->add_control(
2727
'fieldname',
2828
[
29-
'label' => __( 'Callback', 'solid-dynamics' ),
29+
'label' => __( 'Post Field', 'solid-dynamics' ),
3030
'type' => \Elementor\Controls_Manager::TEXT,
3131
]
3232
);

readme.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ Helpful utilities for Elementor, Jet Engine, and beyond.
1010

1111
== Description ==
1212

13-
This plugin provides an Admin Page called, "Widget Usage" under the "Solid Dynamics" menu that shows the individual posts in which a widget is used. Currently, the Elementor Element Manager only shows the total number of usages of a widget. We're always wondering where those widgets are being used, and "Widget Usage" is the answer to that question.
13+
Elementor Widget Usage
14+
15+
Solid Dynamics provides an Admin Page called, "Widget Usage" under the "Solid Dynamics" menu that shows the individual posts in which a widget is used. Currently, the Elementor Element Manager only shows the total number of usages of a widget. We're always wondering where those widgets are being used, and "Widget Usage" is the answer to that question.
16+
17+
Elementor Dynamic Tags
1418

1519
Solid Dynamics also provides several dynamic tags under the "Solid Dynamics" section in Elementor:
1620

@@ -20,7 +24,15 @@ Solid Dynamics also provides several dynamic tags under the "Solid Dynamics" sec
2024
- `List Pluck`: Pluck `field` off each item in `list` (`src` meta or option), and join with `sep`.
2125
- `Post Field`: Retrieves custom post field by name.
2226

23-
This plugin also provides several general use and Elementor specific settings under the menu Solid Dynamics. All settings have to be opted in to. Activating the plugin does not activate any of the settings. Activating the plugin does automatically make the dynamic tags listed above available.
27+
Elementor Display Conditions
28+
29+
Solid Dynamics also provides several display conditions for Elementor:
30+
31+
- `Solid Dynamics Macro`: access post or user data, e.g. `post|post_content`, `user|user_email`, or call any function `function|get_current_user_id`. The result is passed to boolval to show the widget.
32+
33+
Settings
34+
35+
Solid Dynamics also provides several general use and Elementor specific settings under the menu Solid Dynamics. All settings have to be opted in to. Activating the plugin does not activate any of the settings. Activating the plugin does automatically make the dynamic tags listed above available.
2436

2537
General Settings:
2638

solid-dynamics.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
die;
1919
}
2020

21+
require_once( __DIR__ . "/classes/elementor-display-conditions.php" );
22+
23+
new ElementorDisplayConditions();
24+
2125
require_once( __DIR__ . "/classes/elementor-dynamic-tags.php" );
2226

2327
new ElementorDynamicTags();

0 commit comments

Comments
 (0)