| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor Widgets |
| 4 |
* |
| 5 |
* @package GamiPress\Elementor_Forms\Widgets\Elementor_Widgets |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* GamiPress Elementor widgets setup |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
function gamipress_elementor_register_widgets_setup() { |
| 21 |
|
| 22 |
if ( ! class_exists( '\Elementor\Widget_Base' ) ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
if ( ! class_exists( 'Widget_GamiPress_Elementor' ) ) { |
| 27 |
|
| 28 |
/** |
| 29 |
* Elementor class for GamiPress widgets |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
class Widget_GamiPress_Elementor extends \Elementor\Widget_Base { |
| 34 |
|
| 35 |
private $_widget_name = null; |
| 36 |
private $_widget_instance = null; |
| 37 |
|
| 38 |
public function hide_on_search() { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
public function get_name() { |
| 43 |
// Add prefix to avoid conflict with widgets in WordPress category |
| 44 |
return 'gamipress-elementor-' . $this->get_widget_instance()->id_base; |
| 45 |
} |
| 46 |
|
| 47 |
public function get_title() { |
| 48 |
return $this->get_widget_instance()->name; |
| 49 |
} |
| 50 |
|
| 51 |
public function get_categories() { |
| 52 |
// GamiPress category |
| 53 |
return [ 'gamipress-elementor' ]; |
| 54 |
} |
| 55 |
|
| 56 |
public function get_icon() { |
| 57 |
$widget_id = $this->get_widget_instance()->id_base; |
| 58 |
|
| 59 |
$icons = array( |
| 60 |
'gamipress_achievement_widget' => 'dashicons dashicons-awards', |
| 61 |
'gamipress_achievements_widget' => 'dashicons dashicons-awards', |
| 62 |
'gamipress_last_achievements_earned_widget' => 'dashicons dashicons-awards', |
| 63 |
'gamipress_earnings_widget' => 'dashicons dashicons-gamipress', |
| 64 |
'gamipress_logs_widget' => 'dashicons dashicons-editor-ul', |
| 65 |
'gamipress_user_points_widget' => 'dashicons dashicons-star-filled', |
| 66 |
'gamipress_site_points_widget' => 'dashicons dashicons-star-filled', |
| 67 |
'gamipress_points_widget' => 'dashicons dashicons-star-filled', |
| 68 |
'gamipress_points_types_widget' => 'dashicons dashicons-star-filled', |
| 69 |
'gamipress_rank_widget' => 'dashicons dashicons-rank', |
| 70 |
'gamipress_ranks_widget' => 'dashicons dashicons-rank', |
| 71 |
'gamipress_user_rank_widget' => 'dashicons dashicons-rank', |
| 72 |
'gamipress_email_settings_widget' => 'dashicons dashicons-email', |
| 73 |
'gamipress_inline_achievement_widget' => 'dashicons dashicons-awards', |
| 74 |
'gamipress_inline_last_achievements_earned_widget' => 'dashicons dashicons-awards', |
| 75 |
'gamipress_inline_rank_widget' => 'dashicons dashicons-rank', |
| 76 |
'gamipress_inline_user_rank_widget' => 'dashicons dashicons-rank', |
| 77 |
'gamipress_vimeo_widget' => 'dashicons dashicons-gamipress', |
| 78 |
'gamipress_youtube_widget' => 'dashicons dashicons-gamipress', |
| 79 |
); |
| 80 |
|
| 81 |
return isset( $icons[ $widget_id ] ) ? $icons[ $widget_id ] : 'eicon-wordpress'; |
| 82 |
} |
| 83 |
|
| 84 |
public function get_keywords() { |
| 85 |
return [ 'gamipress', 'widget', 'custom' ]; |
| 86 |
} |
| 87 |
|
| 88 |
public function get_style_depends(): array { |
| 89 |
return [ 'e-swiper' ]; |
| 90 |
} |
| 91 |
|
| 92 |
public function get_script_depends(): array { |
| 93 |
return [ 'swiper' ]; |
| 94 |
} |
| 95 |
|
| 96 |
public function is_reload_preview_required() { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
public function get_form() { |
| 101 |
$instance = $this->get_widget_instance(); |
| 102 |
ob_start(); |
| 103 |
echo '<div class="widget-inside media-widget-control"><div class="form wp-core-ui">'; |
| 104 |
echo '<input type="hidden" class="id_base" value="' . esc_attr( $instance->id_base ) . '" />'; |
| 105 |
echo '<input type="hidden" class="widget-id" value="widget-' . esc_attr( $this->get_id() ) . '" />'; |
| 106 |
echo '<div class="widget-content">'; |
| 107 |
$widget_data = $this->get_settings( 'wp' ); |
| 108 |
$instance->form( $widget_data ); |
| 109 |
do_action( 'in_widget_form', $instance, null, $widget_data ); |
| 110 |
echo '</div></div></div>'; |
| 111 |
return ob_get_clean(); |
| 112 |
} |
| 113 |
|
| 114 |
public function get_widget_instance() { |
| 115 |
if ( is_null( $this->_widget_instance ) ) { |
| 116 |
global $wp_widget_factory; |
| 117 |
if ( isset( $wp_widget_factory->widgets[ $this->_widget_name ] ) ) { |
| 118 |
$this->_widget_instance = $wp_widget_factory->widgets[ $this->_widget_name ]; |
| 119 |
$this->_widget_instance->_set( 'REPLACE_TO_ID' ); |
| 120 |
} elseif ( class_exists( $this->_widget_name ) ) { |
| 121 |
$this->_widget_instance = new $this->_widget_name(); |
| 122 |
$this->_widget_instance->_set( 'REPLACE_TO_ID' ); |
| 123 |
} |
| 124 |
} |
| 125 |
return $this->_widget_instance; |
| 126 |
} |
| 127 |
|
| 128 |
protected function get_init_settings() { |
| 129 |
$settings = parent::get_init_settings(); |
| 130 |
if ( ! empty( $settings['wp'] ) ) { |
| 131 |
$widget = $this->get_widget_instance(); |
| 132 |
$instance = $widget->update( $settings['wp'], [] ); |
| 133 |
$settings['wp'] = apply_filters( 'widget_update_callback', $instance, $settings['wp'], [], $widget ); |
| 134 |
} |
| 135 |
return $settings; |
| 136 |
} |
| 137 |
|
| 138 |
protected function register_controls() { |
| 139 |
$this->add_control( |
| 140 |
'wp', |
| 141 |
[ |
| 142 |
'label' => esc_html__( 'Form', 'elementor' ), |
| 143 |
'type' => \Elementor\Controls_Manager::WP_WIDGET, |
| 144 |
'widget' => $this->get_name(), |
| 145 |
'id_base' => $this->get_widget_instance()->id_base, |
| 146 |
] |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
protected function render() { |
| 151 |
$default_widget_args = [ |
| 152 |
'widget_id' => $this->get_name(), |
| 153 |
'before_widget' => '', |
| 154 |
'after_widget' => '', |
| 155 |
'before_title' => '<h5>', |
| 156 |
'after_title' => '</h5>', |
| 157 |
]; |
| 158 |
$default_widget_args = apply_filters( 'elementor/widgets/wordpress/widget_args', $default_widget_args, $this ); |
| 159 |
$this->get_widget_instance()->widget( $default_widget_args, $this->get_settings( 'wp' ) ); |
| 160 |
} |
| 161 |
|
| 162 |
public function __construct( $data = [], $args = null ) { |
| 163 |
$this->_widget_name = $args['widget_name']; |
| 164 |
parent::__construct( $data, $args ); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Register GamiPress category |
| 170 |
* |
| 171 |
* @since 1.0.0 |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
function gamipress_elementor_register_category( $elements_manager ) { |
| 175 |
|
| 176 |
$elements_manager->add_category( |
| 177 |
'gamipress-elementor', |
| 178 |
[ |
| 179 |
'title' => __( 'GamiPress', 'gamipress' ), |
| 180 |
'icon' => 'fa fa-trophy', |
| 181 |
] |
| 182 |
); |
| 183 |
} |
| 184 |
add_action( 'elementor/elements/categories_registered', 'gamipress_elementor_register_category' ); |
| 185 |
|
| 186 |
/** |
| 187 |
* Register the GamiPress widgets |
| 188 |
* |
| 189 |
* @since 1.0.0 |
| 190 |
*/ |
| 191 |
function gamipress_elementor_register_widgets ( $widgets_manager ) { |
| 192 |
|
| 193 |
global $wp_widget_factory; |
| 194 |
|
| 195 |
// We go through all the widgets registered in WordPress |
| 196 |
foreach ( $wp_widget_factory->widgets as $widget_class => $widget_obj ) { |
| 197 |
|
| 198 |
// To get GamiPress widgets |
| 199 |
if ( strpos( $widget_class, 'gamipress' ) === false ) continue; |
| 200 |
|
| 201 |
$widgets_manager->register( |
| 202 |
new Widget_GamiPress_Elementor( [], [ 'widget_name' => $widget_class ] ) |
| 203 |
); |
| 204 |
} |
| 205 |
} |
| 206 |
add_action( 'elementor/widgets/register', 'gamipress_elementor_register_widgets', 100 ); |
| 207 |
} |
| 208 |
} |
| 209 |
add_action( 'elementor/init', 'gamipress_elementor_register_widgets_setup' ); |
| 210 |
|
| 211 |
/** |
| 212 |
* Enqueue GamiPress CSS |
| 213 |
* |
| 214 |
* @since 1.0.0 |
| 215 |
*/ |
| 216 |
/** |
| 217 |
* Enqueue GamiPress CSS |
| 218 |
* |
| 219 |
* @since 1.0.0 |
| 220 |
*/ |
| 221 |
function gamipress_elementor_enqueue_css_script () { |
| 222 |
// Use minified libraries if SCRIPT_DEBUG is turned off |
| 223 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 224 |
|
| 225 |
wp_enqueue_style( 'gamipress-admin', GAMIPRESS_URL . 'assets/css/gamipress-admin' . $suffix . '.css' ); |
| 226 |
|
| 227 |
// Custom CSS for descriptions and switch |
| 228 |
$custom_css = " |
| 229 |
.cmb-tooltip-desc.cmb-tooltip-left { |
| 230 |
display: block; |
| 231 |
font-weight: normal; |
| 232 |
line-height: normal; |
| 233 |
opacity: 0.8; |
| 234 |
} |
| 235 |
.gamipress-switch input{ |
| 236 |
border: none !important; |
| 237 |
} |
| 238 |
.gamipress-switch input + label:after { |
| 239 |
margin-top: 2px; |
| 240 |
} |
| 241 |
.gamipress-switch input:checked:after { |
| 242 |
margin-top: 7px; |
| 243 |
} |
| 244 |
|
| 245 |
"; |
| 246 |
wp_add_inline_style( 'gamipress-admin', $custom_css ); |
| 247 |
|
| 248 |
} |
| 249 |
add_action( 'elementor/editor/after_enqueue_scripts', 'gamipress_elementor_enqueue_css_script' ); |
| 250 |
|