| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 2.02.12 |
| 8 |
*/ |
| 9 |
class FrmShortcodeHelper { |
| 10 |
|
| 11 |
/** |
| 12 |
* Get the shortcode attributes in key/value pairs from a string |
| 13 |
* |
| 14 |
* @since 2.02.12 |
| 15 |
* |
| 16 |
* @param string $text |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
public static function get_shortcode_attribute_array( $text ) { |
| 21 |
$atts = array(); |
| 22 |
if ( $text !== '' ) { |
| 23 |
$atts = shortcode_parse_atts( $text ); |
| 24 |
} |
| 25 |
|
| 26 |
if ( ! is_array( $atts ) ) { |
| 27 |
$atts = array(); |
| 28 |
} |
| 29 |
|
| 30 |
return $atts; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns shortcodes that are shown/hidden based on the context. |
| 35 |
* |
| 36 |
* @since 6.16.3 |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
public static function get_contextual_shortcodes() { |
| 40 |
return array( |
| 41 |
'address' => array( |
| 42 |
'admin_email' => __( 'Admin email', 'formidable' ), |
| 43 |
'default-from-email' => __( 'Default from email', 'formidable' ), |
| 44 |
'default-email' => __( 'Default email', 'formidable' ), |
| 45 |
), |
| 46 |
'body' => array( |
| 47 |
'default-message' => __( 'Default Msg', 'formidable' ), |
| 48 |
'default-html' => __( 'Default HTML', 'formidable' ), |
| 49 |
'default-plain' => __( 'Default Plain', 'formidable' ), |
| 50 |
), |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get contextual shortcodes. |
| 56 |
* |
| 57 |
* @since 6.16.3 |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public static function get_contextual_shortcode_values() { |
| 62 |
$contextual_shortcodes = self::get_contextual_shortcodes(); |
| 63 |
return array_merge( $contextual_shortcodes['address'], $contextual_shortcodes['body'] ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get flattened format of contextual shortcodes. |
| 68 |
* |
| 69 |
* @since 6.16.3 |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public static function get_contextual_codes() { |
| 74 |
$contextual_shortcodes = self::get_contextual_shortcodes(); |
| 75 |
$result = array(); |
| 76 |
foreach ( $contextual_shortcodes as $type => $shortcodes ) { |
| 77 |
$result[ $type ] = array_keys( $shortcodes ); |
| 78 |
} |
| 79 |
return $result; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get the name of the shortcode from the regEx |
| 84 |
* |
| 85 |
* @since 3.0 |
| 86 |
* |
| 87 |
* @param array $shortcodes |
| 88 |
* @param int $short_key The position in the shortcodes array. |
| 89 |
* @param array $args |
| 90 |
* |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
public static function get_shortcode_tag( $shortcodes, $short_key, $args = array() ) { |
| 94 |
$args = wp_parse_args( |
| 95 |
$args, |
| 96 |
array( |
| 97 |
'conditional' => false, |
| 98 |
'conditional_check' => false, |
| 99 |
'foreach' => false, |
| 100 |
) |
| 101 |
); |
| 102 |
if ( ( $args['conditional'] || $args['foreach'] ) && ! $args['conditional_check'] ) { |
| 103 |
$args['conditional_check'] = true; |
| 104 |
} |
| 105 |
|
| 106 |
$prefix = ''; |
| 107 |
if ( $args['conditional_check'] ) { |
| 108 |
if ( $args['conditional'] ) { |
| 109 |
$prefix = 'if '; |
| 110 |
} elseif ( $args['foreach'] ) { |
| 111 |
$prefix = 'foreach '; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
$with_tags = $args['conditional_check'] ? 3 : 2; |
| 116 |
if ( ! empty( $shortcodes[ $with_tags ][ $short_key ] ) ) { |
| 117 |
$tag = str_replace( '[' . $prefix, '', $shortcodes[0][ $short_key ] ); |
| 118 |
$tag = str_replace( ']', '', $tag ); |
| 119 |
$tag = str_replace( chr( 194 ) . chr( 160 ), ' ', $tag ); |
| 120 |
$tags = preg_split( '/\s+/', $tag, 2 ); |
| 121 |
if ( is_array( $tags ) ) { |
| 122 |
$tag = $tags[0]; |
| 123 |
} |
| 124 |
} else { |
| 125 |
$tag = $shortcodes[ $with_tags - 1 ][ $short_key ]; |
| 126 |
} |
| 127 |
|
| 128 |
return $tag; |
| 129 |
} |
| 130 |
|
| 131 |
public static function remove_inline_conditions( $no_vars, $code, $replace_with, &$html ) { |
| 132 |
if ( $no_vars ) { |
| 133 |
$html = str_replace( '[if ' . $code . ']', '', $html ); |
| 134 |
$html = str_replace( '[/if ' . $code . ']', '', $html ); |
| 135 |
} else { |
| 136 |
$html = preg_replace( '/(\[if\s+' . $code . '\])(.*?)(\[\/if\s+' . $code . '\])/mis', '', $html ); |
| 137 |
} |
| 138 |
|
| 139 |
$html = str_replace( '[' . $code . ']', $replace_with, $html ); |
| 140 |
} |
| 141 |
} |
| 142 |
|