| 1 |
<?php |
| 2 |
namespace LearnPress\ExternalPlugin\Elementor; |
| 3 |
|
| 4 |
use Elementor\Controls_Manager; |
| 5 |
use Elementor\Repeater; |
| 6 |
use Elementor\Widget_Base; |
| 7 |
use Throwable; |
| 8 |
|
| 9 |
class LPElementorWidgetBase extends Widget_Base { |
| 10 |
/** |
| 11 |
* @var string $title |
| 12 |
*/ |
| 13 |
public $title = ''; |
| 14 |
/** |
| 15 |
* @var string $prefix_name; |
| 16 |
*/ |
| 17 |
private $prefix_name = 'learnpress_'; |
| 18 |
/** |
| 19 |
* @var string $name; |
| 20 |
*/ |
| 21 |
public $name = ''; |
| 22 |
/** |
| 23 |
* @var string $icon |
| 24 |
*/ |
| 25 |
public $icon; |
| 26 |
/** |
| 27 |
* @var string[] key search widget |
| 28 |
*/ |
| 29 |
public $keywords = array(); |
| 30 |
/** |
| 31 |
* @var array Controls |
| 32 |
*/ |
| 33 |
public $controls = array(); |
| 34 |
|
| 35 |
public function __construct( $data = [], $args = null ) { |
| 36 |
parent::__construct( $data, $args ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
public function get_title() { |
| 41 |
return $this->title; |
| 42 |
} |
| 43 |
|
| 44 |
public function get_name() { |
| 45 |
return $this->prefix_name . $this->name; |
| 46 |
} |
| 47 |
|
| 48 |
public function get_icon() { |
| 49 |
return $this->icon ?? 'eicon-site-logo'; |
| 50 |
} |
| 51 |
|
| 52 |
public function get_keywords() { |
| 53 |
return array_push( $this->keywords, 'learnpress' ); |
| 54 |
} |
| 55 |
|
| 56 |
public function get_categories() { |
| 57 |
return array( 'learnpress' ); |
| 58 |
} |
| 59 |
|
| 60 |
public function get_help_url() { |
| 61 |
return ''; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Register controls. |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
protected function register_controls() { |
| 70 |
if ( ! is_array( $this->controls ) ) { |
| 71 |
error_log( __METHOD__ . ': ' . json_encode( $this->controls ) ); |
| 72 |
return; |
| 73 |
} |
| 74 |
$this->print_fields( $this->controls ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Register controls. |
| 79 |
* |
| 80 |
* @param array $fields |
| 81 |
* |
| 82 |
* @since 4.2.3 |
| 83 |
* @version 1.0.0 |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
protected function print_fields( array $fields ) { |
| 87 |
try { |
| 88 |
foreach ( $fields as $id => $field ) { |
| 89 |
if ( isset( $field['method'] ) && is_callable( [ $this, $field['method'] ] ) && is_array( $field ) ) { |
| 90 |
$params = []; |
| 91 |
foreach ( $field as $key => $value ) { |
| 92 |
if ( 'method' === $key ) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
$params[] = $value; |
| 96 |
} |
| 97 |
|
| 98 |
// Register control type Repeater |
| 99 |
if ( isset( $params[1]['type'] ) && Controls_Manager::REPEATER === $params[1]['type'] ) { |
| 100 |
$repeater = new Repeater(); |
| 101 |
|
| 102 |
foreach ( $params[1]['fields'] as $key => $value ) { |
| 103 |
// For call method add_responsive_control, and another method |
| 104 |
if ( isset( $value['method'] ) ) { |
| 105 |
$prms = $value; |
| 106 |
unset( $prms['method'] ); |
| 107 |
|
| 108 |
if ( 'add_group_control' === $value['method'] ) { |
| 109 |
$args = [ $value['type'] ?? '', $prms ]; |
| 110 |
} else { |
| 111 |
unset( $prms['name'] ); |
| 112 |
$args = [ $value['name'] ?? '', $prms ]; |
| 113 |
} |
| 114 |
|
| 115 |
call_user_func_array( [ $repeater, $value['method'] ], $args ); |
| 116 |
} else { |
| 117 |
$repeater->add_control( $value['name'], $value ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$params[1]['fields'] = $repeater->get_controls(); |
| 122 |
$params[1]['title_field'] = $params[1]['title_field'] ?? ''; |
| 123 |
$this->add_control( |
| 124 |
$params[0], // string id of control |
| 125 |
$params[1] // array args of control |
| 126 |
); |
| 127 |
} else { |
| 128 |
call_user_func_array( [ $this, $field['method'] ], $params ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} catch ( Throwable $e ) { |
| 133 |
error_log( $e->getMessage() ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|