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