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