| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. |
| 2 |
/** |
| 3 |
* |
| 4 |
* Shortcoder Class |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
* |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'ADMINIFY_Shortcoder' ) ) { |
| 11 |
class ADMINIFY_Shortcoder extends ADMINIFY_Abstract{ |
| 12 |
|
| 13 |
// constans |
| 14 |
public $unique = ''; |
| 15 |
public $abstract = 'shortcoder'; |
| 16 |
public $blocks = array(); |
| 17 |
public $sections = array(); |
| 18 |
public $pre_tabs = array(); |
| 19 |
public $pre_sections = array(); |
| 20 |
public $args = array( |
| 21 |
'button_title' => 'Add Shortcode', |
| 22 |
'select_title' => 'Select a shortcode', |
| 23 |
'insert_title' => 'Insert Shortcode', |
| 24 |
'show_in_editor' => true, |
| 25 |
'show_in_custom' => false, |
| 26 |
'defaults' => array(), |
| 27 |
'class' => '', |
| 28 |
'gutenberg' => array( |
| 29 |
'title' => 'ADMINIFY Shortcodes', |
| 30 |
'description' => 'ADMINIFY Shortcode Block', |
| 31 |
'icon' => 'screenoptions', |
| 32 |
'category' => 'widgets', |
| 33 |
'keywords' => array( 'shortcode', 'adminify', 'insert' ), |
| 34 |
'placeholder' => 'Write shortcode here...', |
| 35 |
), |
| 36 |
); |
| 37 |
|
| 38 |
// run shortcode construct |
| 39 |
public function __construct( $key, $params = array() ) { |
| 40 |
|
| 41 |
$this->unique = $key; |
| 42 |
$this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this ); |
| 43 |
$this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this ); |
| 44 |
$this->pre_tabs = $this->pre_tabs( $this->sections ); |
| 45 |
$this->pre_sections = $this->pre_sections( $this->sections ); |
| 46 |
|
| 47 |
add_action( 'admin_footer', array( $this, 'add_footer_modal_shortcode' ) ); |
| 48 |
add_action( 'customize_controls_print_footer_scripts', array( $this, 'add_footer_modal_shortcode' ) ); |
| 49 |
add_action( 'wp_ajax_adminify-get-shortcode-'. $this->unique, array( $this, 'get_shortcode' ) ); |
| 50 |
|
| 51 |
if ( ! empty( $this->args['show_in_editor'] ) ) { |
| 52 |
|
| 53 |
ADMINIFY::$shortcode_instances[$this->unique] = wp_parse_args( array( 'hash' => md5( $key ), 'modal_id' => $this->unique ), $this->args ); |
| 54 |
|
| 55 |
// elementor editor support |
| 56 |
if ( ADMINIFY::is_active_plugin( 'elementor/elementor.php' ) ) { |
| 57 |
add_action( 'elementor/editor/before_enqueue_scripts', array( 'ADMINIFY', 'add_admin_enqueue_scripts' ) ); |
| 58 |
add_action( 'elementor/editor/footer', array( 'ADMINIFY_Field_icon', 'add_footer_modal_icon' ) ); |
| 59 |
add_action( 'elementor/editor/footer', array( $this, 'add_footer_modal_shortcode' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
// instance |
| 67 |
public static function instance( $key, $params = array() ) { |
| 68 |
return new self( $key, $params ); |
| 69 |
} |
| 70 |
|
| 71 |
public function pre_tabs( $sections ) { |
| 72 |
|
| 73 |
$result = array(); |
| 74 |
$parents = array(); |
| 75 |
$count = 100; |
| 76 |
|
| 77 |
foreach ( $sections as $key => $section ) { |
| 78 |
if ( ! empty( $section['parent'] ) ) { |
| 79 |
$section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count; |
| 80 |
$parents[$section['parent']][] = $section; |
| 81 |
unset( $sections[$key] ); |
| 82 |
} |
| 83 |
$count++; |
| 84 |
} |
| 85 |
|
| 86 |
foreach ( $sections as $key => $section ) { |
| 87 |
$section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count; |
| 88 |
if ( ! empty( $section['id'] ) && ! empty( $parents[$section['id']] ) ) { |
| 89 |
$section['subs'] = wp_list_sort( $parents[$section['id']], array( 'priority' => 'ASC' ), 'ASC', true ); |
| 90 |
} |
| 91 |
$result[] = $section; |
| 92 |
$count++; |
| 93 |
} |
| 94 |
|
| 95 |
return wp_list_sort( $result, array( 'priority' => 'ASC' ), 'ASC', true ); |
| 96 |
} |
| 97 |
|
| 98 |
public function pre_sections( $sections ) { |
| 99 |
|
| 100 |
$result = array(); |
| 101 |
|
| 102 |
foreach ( $this->pre_tabs as $tab ) { |
| 103 |
if ( ! empty( $tab['subs'] ) ) { |
| 104 |
foreach ( $tab['subs'] as $sub ) { |
| 105 |
$result[] = $sub; |
| 106 |
} |
| 107 |
} |
| 108 |
if ( empty( $tab['subs'] ) ) { |
| 109 |
$result[] = $tab; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return $result; |
| 114 |
} |
| 115 |
|
| 116 |
// get default value |
| 117 |
public function get_default( $field ) { |
| 118 |
|
| 119 |
$default = ( isset( $field['default'] ) ) ? $field['default'] : ''; |
| 120 |
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default; |
| 121 |
|
| 122 |
return $default; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
public function add_footer_modal_shortcode() { |
| 127 |
|
| 128 |
if( ! wp_script_is( 'adminify' ) ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
$class = ( $this->args['class'] ) ? ' '. esc_attr( $this->args['class'] ) : ''; |
| 133 |
$has_select = ( count( $this->pre_tabs ) > 1 ) ? true : false; |
| 134 |
$single_usage = ( ! $has_select ) ? ' adminify-shortcode-single' : ''; |
| 135 |
$hide_header = ( ! $has_select ) ? ' hidden' : ''; |
| 136 |
|
| 137 |
?> |
| 138 |
<div id="adminify-modal-<?php echo esc_attr( $this->unique ); ?>" class="wp-core-ui adminify-modal adminify-shortcode hidden<?php echo esc_attr( $single_usage . $class ); ?>" data-modal-id="<?php echo esc_attr( $this->unique ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'adminify_shortcode_nonce' ) ); ?>"> |
| 139 |
<div class="adminify-modal-table"> |
| 140 |
<div class="adminify-modal-table-cell"> |
| 141 |
<div class="adminify-modal-overlay"></div> |
| 142 |
<div class="adminify-modal-inner"> |
| 143 |
<div class="adminify-modal-title"> |
| 144 |
<?php echo $this->args['button_title']; ?> |
| 145 |
<div class="adminify-modal-close"></div> |
| 146 |
</div> |
| 147 |
<?php |
| 148 |
|
| 149 |
echo '<div class="adminify-modal-header'. esc_attr( $hide_header ) .'">'; |
| 150 |
echo '<select>'; |
| 151 |
echo ( $has_select ) ? '<option value="">'. esc_attr( $this->args['select_title'] ) .'</option>' : ''; |
| 152 |
|
| 153 |
$tab_key = 1; |
| 154 |
|
| 155 |
foreach ( $this->pre_tabs as $tab ) { |
| 156 |
|
| 157 |
if ( ! empty( $tab['subs'] ) ) { |
| 158 |
|
| 159 |
echo '<optgroup label="'. esc_attr( $tab['title'] ) .'">'; |
| 160 |
|
| 161 |
foreach ( $tab['subs'] as $sub ) { |
| 162 |
|
| 163 |
$view = ( ! empty( $sub['view'] ) ) ? ' data-view="'. esc_attr( $sub['view'] ) .'"' : ''; |
| 164 |
$shortcode = ( ! empty( $sub['shortcode'] ) ) ? ' data-shortcode="'. esc_attr( $sub['shortcode'] ) .'"' : ''; |
| 165 |
$group = ( ! empty( $sub['group_shortcode'] ) ) ? ' data-group="'. esc_attr( $sub['group_shortcode'] ) .'"' : ''; |
| 166 |
|
| 167 |
echo '<option value="'. esc_attr( $tab_key ) .'"'. $view . $shortcode . $group .'>'. esc_attr( $sub['title'] ) .'</option>'; |
| 168 |
|
| 169 |
$tab_key++; |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
echo '</optgroup>' ; |
| 174 |
|
| 175 |
} else { |
| 176 |
|
| 177 |
$view = ( ! empty( $tab['view'] ) ) ? ' data-view="'. esc_attr( $tab['view'] ) .'"' : ''; |
| 178 |
$shortcode = ( ! empty( $tab['shortcode'] ) ) ? ' data-shortcode="'. esc_attr( $tab['shortcode'] ) .'"' : ''; |
| 179 |
$group = ( ! empty( $tab['group_shortcode'] ) ) ? ' data-group="'. esc_attr( $tab['group_shortcode'] ) .'"' : ''; |
| 180 |
|
| 181 |
echo '<option value="'. esc_attr( $tab_key ) .'"'. $view . $shortcode . $group .'>'. esc_attr( $tab['title'] ) .'</option>'; |
| 182 |
|
| 183 |
$tab_key++; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
echo '</select>'; |
| 190 |
echo '</div>'; |
| 191 |
|
| 192 |
?> |
| 193 |
<div class="adminify-modal-content"> |
| 194 |
<div class="adminify-modal-loading"><div class="adminify-loading"></div></div> |
| 195 |
<div class="adminify-modal-load"></div> |
| 196 |
</div> |
| 197 |
<div class="adminify-modal-insert-wrapper hidden"><a href="#" class="button button-primary adminify-modal-insert"><?php echo $this->args['insert_title']; ?></a></div> |
| 198 |
</div> |
| 199 |
</div> |
| 200 |
</div> |
| 201 |
</div> |
| 202 |
<?php |
| 203 |
} |
| 204 |
|
| 205 |
public function get_shortcode() { |
| 206 |
|
| 207 |
ob_start(); |
| 208 |
|
| 209 |
$nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : ''; |
| 210 |
$shortcode_key = ( ! empty( $_POST[ 'shortcode_key' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'shortcode_key' ] ) ) : ''; |
| 211 |
|
| 212 |
if ( ! empty( $shortcode_key ) && wp_verify_nonce( $nonce, 'adminify_shortcode_nonce' ) ) { |
| 213 |
|
| 214 |
$unallows = array( 'group', 'repeater', 'sorter' ); |
| 215 |
$section = $this->pre_sections[$shortcode_key-1]; |
| 216 |
$shortcode = ( ! empty( $section['shortcode'] ) ) ? $section['shortcode'] : ''; |
| 217 |
$view = ( ! empty( $section['view'] ) ) ? $section['view'] : 'normal'; |
| 218 |
|
| 219 |
if ( ! empty( $section ) ) { |
| 220 |
|
| 221 |
// |
| 222 |
// View: normal |
| 223 |
if ( ! empty( $section['fields'] ) && $view !== 'repeater' ) { |
| 224 |
|
| 225 |
echo '<div class="adminify-fields">'; |
| 226 |
|
| 227 |
echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. $section['description'] .'</div>' : ''; |
| 228 |
|
| 229 |
foreach ( $section['fields'] as $field ) { |
| 230 |
|
| 231 |
if ( in_array( $field['type'], $unallows ) ) { $field['_notice'] = true; } |
| 232 |
|
| 233 |
// Extra tag improves for spesific fields (border, spacing, dimensions etc...) |
| 234 |
$field['tag_prefix'] = ( ! empty( $field['tag_prefix'] ) ) ? $field['tag_prefix'] .'_' : ''; |
| 235 |
|
| 236 |
$field_default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; |
| 237 |
|
| 238 |
ADMINIFY::field( $field, $field_default, $shortcode, 'shortcode' ); |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
echo '</div>'; |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
// |
| 247 |
// View: group and repeater fields |
| 248 |
$repeatable_fields = ( $view === 'repeater' && ! empty( $section['fields'] ) ) ? $section['fields'] : array(); |
| 249 |
$repeatable_fields = ( $view === 'group' && ! empty( $section['group_fields'] ) ) ? $section['group_fields'] : $repeatable_fields; |
| 250 |
|
| 251 |
if ( ! empty( $repeatable_fields ) ) { |
| 252 |
|
| 253 |
$button_title = ( ! empty( $section['button_title'] ) ) ? ' '. $section['button_title'] : esc_html__( 'Add New', 'adminify' ); |
| 254 |
$inner_shortcode = ( ! empty( $section['group_shortcode'] ) ) ? $section['group_shortcode'] : $shortcode; |
| 255 |
|
| 256 |
echo '<div class="adminify--repeatable">'; |
| 257 |
|
| 258 |
echo '<div class="adminify--repeat-shortcode">'; |
| 259 |
|
| 260 |
echo '<div class="adminify-repeat-remove fas fa-times"></div>'; |
| 261 |
|
| 262 |
echo '<div class="adminify-fields">'; |
| 263 |
|
| 264 |
foreach ( $repeatable_fields as $field ) { |
| 265 |
|
| 266 |
if ( in_array( $field['type'], $unallows ) ) { $field['_notice'] = true; } |
| 267 |
|
| 268 |
// Extra tag improves for spesific fields (border, spacing, dimensions etc...) |
| 269 |
$field['tag_prefix'] = ( ! empty( $field['tag_prefix'] ) ) ? $field['tag_prefix'] .'_' : ''; |
| 270 |
|
| 271 |
$field_default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; |
| 272 |
|
| 273 |
ADMINIFY::field( $field, $field_default, $inner_shortcode.'[0]', 'shortcode' ); |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
echo '</div>'; |
| 278 |
|
| 279 |
echo '</div>'; |
| 280 |
|
| 281 |
echo '</div>'; |
| 282 |
|
| 283 |
echo '<div class="adminify--repeat-button-block"><a class="button adminify--repeat-button" href="#"><i class="fas fa-plus-circle"></i> '. $button_title .'</a></div>'; |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
} else { |
| 290 |
echo '<div class="adminify-field adminify-error-text">'. esc_html__( 'Error: Invalid nonce verification.', 'adminify' ) .'</div>'; |
| 291 |
} |
| 292 |
|
| 293 |
wp_send_json_success( array( 'content' => ob_get_clean() ) ); |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
// Once editor setup for gutenberg and media buttons |
| 298 |
public static function once_editor_setup() { |
| 299 |
|
| 300 |
if ( function_exists( 'register_block_type' ) ) { |
| 301 |
add_action( 'enqueue_block_editor_assets', array( 'ADMINIFY_Shortcoder', 'add_guteberg_blocks' ) ); |
| 302 |
} |
| 303 |
|
| 304 |
if ( adminify_wp_editor_api() ) { |
| 305 |
add_action( 'media_buttons', array( 'ADMINIFY_Shortcoder', 'add_media_buttons' ) ); |
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
// Add gutenberg blocks. |
| 311 |
public static function add_guteberg_blocks() { |
| 312 |
|
| 313 |
$depends = array( 'wp-blocks', 'wp-element', 'wp-components' ); |
| 314 |
|
| 315 |
if ( wp_script_is( 'wp-edit-widgets' ) ) { |
| 316 |
$depends[] = 'wp-edit-widgets'; |
| 317 |
} else { |
| 318 |
$depends[] = 'wp-edit-post'; |
| 319 |
} |
| 320 |
|
| 321 |
wp_enqueue_script( 'adminify-gutenberg-block', ADMINIFY::include_plugin_url( 'assets/js/gutenberg.js' ), $depends ); |
| 322 |
|
| 323 |
wp_localize_script( 'adminify-gutenberg-block', 'adminify_gutenberg_blocks', ADMINIFY::$shortcode_instances ); |
| 324 |
|
| 325 |
foreach ( ADMINIFY::$shortcode_instances as $value ) { |
| 326 |
|
| 327 |
register_block_type( 'adminify-gutenberg-block/block-'. $value['hash'], array( |
| 328 |
'editor_script' => 'adminify-gutenberg-block', |
| 329 |
) ); |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
// Add media buttons |
| 336 |
public static function add_media_buttons( $editor_id ) { |
| 337 |
|
| 338 |
foreach ( ADMINIFY::$shortcode_instances as $value ) { |
| 339 |
echo '<a href="#" class="button button-primary adminify-shortcode-button" data-editor-id="'. esc_attr( $editor_id ) .'" data-modal-id="'. esc_attr( $value['modal_id'] ) .'">'. $value['button_title'] .'</a>'; |
| 340 |
} |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
} |
| 346 |
|