Skip to content

Commit 9d77347

Browse files
committed
add display conditions
1 parent afe08f7 commit 9d77347

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
$meta_key = $items[2] ?? '';
39+
40+
// TODO: add index for repeater fields.
41+
// $index = $items[3];
42+
43+
if (empty($type) || empty($field)) {
44+
return;
45+
}
46+
47+
switch ($type) {
48+
case 'post':
49+
global $post;
50+
51+
if ($field === 'meta' && !empty($meta_key)) {
52+
return get_post_meta($post->ID, $meta_key, true);
53+
}
54+
55+
return $post->$field;
56+
case 'user':
57+
$user = wp_get_current_user();
58+
59+
if ($field === 'meta' && !empty($meta_key)) {
60+
return get_user_meta($user->ID, $meta_key, true);
61+
}
62+
63+
return $user->$field;
64+
case 'function':
65+
$function_name = $field;
66+
67+
if (is_callable($function_name)) {
68+
return $function_name();
69+
};
70+
71+
return;
72+
// TODO: add option
73+
// case 'option':
74+
// $option = get_option($field);
75+
// return $option;
76+
// TODO: add author
77+
// case 'author':
78+
// $author = get_queried_object();
79+
// return $author->name;
80+
// TODO: add term
81+
// case 'term':
82+
// $term = get_queried_object();
83+
// return $term->name;
84+
}
85+
}
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+
}

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)