| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 2.04 |
| 8 |
*/ |
| 9 |
class FrmEntryShortcodeFormatter { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var int |
| 13 |
* @since 2.04 |
| 14 |
*/ |
| 15 |
protected $form_id = 0; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var array |
| 19 |
* @since 2.04 |
| 20 |
*/ |
| 21 |
protected $skip_fields = array( 'captcha', 'html' ); |
| 22 |
|
| 23 |
/** |
| 24 |
* @var array |
| 25 |
* @since 3.0 |
| 26 |
*/ |
| 27 |
protected $single_cell_fields = array( 'html' ); |
| 28 |
|
| 29 |
/** |
| 30 |
* @var array |
| 31 |
* @since 2.04 |
| 32 |
*/ |
| 33 |
protected $fields = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* @var bool |
| 37 |
* @since 2.05 |
| 38 |
*/ |
| 39 |
protected $is_plain_text = false; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var string |
| 43 |
* @since 2.04 |
| 44 |
*/ |
| 45 |
protected $format = 'text'; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var FrmTableHTMLGenerator |
| 49 |
* @since 2.04 |
| 50 |
*/ |
| 51 |
protected $table_generator = null; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var array |
| 55 |
* @since 2.04 |
| 56 |
*/ |
| 57 |
protected $array_content = array(); |
| 58 |
|
| 59 |
/** |
| 60 |
* FrmEntryShortcodeFormatter constructor |
| 61 |
* |
| 62 |
* @param int|string $form_id |
| 63 |
* @param array $atts |
| 64 |
*/ |
| 65 |
public function __construct( $form_id, $atts ) { |
| 66 |
if ( ! $form_id ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$this->init_form_id( $form_id ); |
| 71 |
$this->init_fields(); |
| 72 |
|
| 73 |
if ( empty( $this->fields ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$this->init_plain_text( $atts ); |
| 78 |
$this->init_format( $atts ); |
| 79 |
|
| 80 |
if ( $this->is_table_format() ) { |
| 81 |
$this->init_table_generator(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Initialize the form_id property |
| 87 |
* |
| 88 |
* @since 2.04 |
| 89 |
* |
| 90 |
* @param $form_id |
| 91 |
*/ |
| 92 |
protected function init_form_id( $form_id ) { |
| 93 |
$this->form_id = (int) $form_id; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Initialize the fields property |
| 98 |
* |
| 99 |
* @since 2.04 |
| 100 |
*/ |
| 101 |
protected function init_fields() { |
| 102 |
$this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Initialize the is_plain_text property |
| 107 |
* |
| 108 |
* @since 2.05 |
| 109 |
* |
| 110 |
* @param array $atts |
| 111 |
*/ |
| 112 |
protected function init_plain_text( $atts ) { |
| 113 |
if ( isset( $atts['plain_text'] ) && $atts['plain_text'] ) { |
| 114 |
$this->is_plain_text = true; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Initialize the format property |
| 120 |
* |
| 121 |
* @since 2.04 |
| 122 |
* |
| 123 |
* @param array $atts |
| 124 |
*/ |
| 125 |
protected function init_format( $atts ) { |
| 126 |
if ( isset( $atts['format'] ) && is_string( $atts['format'] ) && $atts['format'] !== '' ) { |
| 127 |
$this->format = $atts['format']; |
| 128 |
} else { |
| 129 |
$this->format = 'text'; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Initialize the table_generator property |
| 135 |
* |
| 136 |
* @since 2.04 |
| 137 |
*/ |
| 138 |
protected function init_table_generator() { |
| 139 |
$this->table_generator = new FrmTableHTMLGenerator( 'shortcode' ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Return the default HTML for an entry |
| 144 |
* |
| 145 |
* @since 2.04 |
| 146 |
*/ |
| 147 |
public function content() { |
| 148 |
if ( ! $this->form_id || empty( $this->fields ) ) { |
| 149 |
return ''; |
| 150 |
} |
| 151 |
|
| 152 |
if ( $this->format == 'array' ) { |
| 153 |
$content = $this->get_array(); |
| 154 |
} elseif ( $this->is_plain_text_format() ) { |
| 155 |
$content = $this->get_plain_text(); |
| 156 |
} else { |
| 157 |
$content = $this->get_table(); |
| 158 |
} |
| 159 |
|
| 160 |
return $content; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Return the default HTML array |
| 165 |
* |
| 166 |
* @since 2.04 |
| 167 |
*/ |
| 168 |
protected function get_array() { |
| 169 |
foreach ( $this->fields as $field ) { |
| 170 |
$this->add_field_array( $field ); |
| 171 |
} |
| 172 |
|
| 173 |
return $this->array_content; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Return the default plain text for an email message |
| 178 |
* |
| 179 |
* @since 2.04 |
| 180 |
*/ |
| 181 |
protected function get_plain_text() { |
| 182 |
return $this->generate_content_for_all_fields(); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Return the default HTML for an email message |
| 187 |
* |
| 188 |
* @since 2.04 |
| 189 |
*/ |
| 190 |
protected function get_table() { |
| 191 |
$content = $this->table_generator->generate_table_header(); |
| 192 |
$content .= $this->generate_content_for_all_fields(); |
| 193 |
$content .= $this->table_generator->generate_table_footer(); |
| 194 |
|
| 195 |
return $content; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Generate the content for all fields |
| 200 |
* |
| 201 |
* @since 2.05 |
| 202 |
* |
| 203 |
* @return string |
| 204 |
*/ |
| 205 |
protected function generate_content_for_all_fields() { |
| 206 |
$content = ''; |
| 207 |
|
| 208 |
foreach ( $this->fields as $field ) { |
| 209 |
$content .= $this->generate_field_content( $field ); |
| 210 |
} |
| 211 |
|
| 212 |
return $content; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Generate a field's HTML or plain text shortcodes |
| 217 |
* |
| 218 |
* @since 2.04 |
| 219 |
* |
| 220 |
* @param stdClass $field |
| 221 |
* |
| 222 |
* @return string |
| 223 |
*/ |
| 224 |
protected function generate_field_content( $field ) { |
| 225 |
if ( in_array( $field->type, $this->skip_fields ) ) { |
| 226 |
return ''; |
| 227 |
} |
| 228 |
|
| 229 |
$row = $this->generate_two_cell_shortcode_row( $field ); |
| 230 |
|
| 231 |
return $row; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Generate a two cell row of shortcodes for an HTML or plain text table |
| 236 |
* |
| 237 |
* @since 2.04 |
| 238 |
* |
| 239 |
* @param stdClass $field |
| 240 |
* @param mixed $value |
| 241 |
* |
| 242 |
* @return string |
| 243 |
*/ |
| 244 |
protected function generate_two_cell_shortcode_row( $field, $value = null ) { |
| 245 |
$row = '[if ' . $field->id . ']'; |
| 246 |
|
| 247 |
$label = '[' . $field->id . ' show=field_label]'; |
| 248 |
|
| 249 |
if ( $value === null ) { |
| 250 |
$value = '[' . $field->id . ']'; |
| 251 |
} |
| 252 |
|
| 253 |
if ( $this->is_plain_text_format() ) { |
| 254 |
$row .= $label . ': ' . $value . "\r\n"; |
| 255 |
} else { |
| 256 |
$row .= $this->table_generator->generate_two_cell_table_row( $label, $value ); |
| 257 |
} |
| 258 |
|
| 259 |
$row .= '[/if ' . $field->id . ']'; |
| 260 |
|
| 261 |
if ( $this->is_table_format() ) { |
| 262 |
$row .= "\r\n"; |
| 263 |
} |
| 264 |
|
| 265 |
return $row; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Generate a field's array for the default HTML array |
| 270 |
* |
| 271 |
* @since 2.04 |
| 272 |
* |
| 273 |
* @param stdClass $field |
| 274 |
*/ |
| 275 |
protected function add_field_array( $field ) { |
| 276 |
if ( in_array( $field->type, $this->skip_fields ) ) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
$this->add_single_field_array( $field, $field->id ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Generate a single field's array |
| 285 |
* |
| 286 |
* @since 2.04 |
| 287 |
* |
| 288 |
* @param stdClass $field |
| 289 |
* @param string $value |
| 290 |
*/ |
| 291 |
protected function add_single_field_array( $field, $value ) { |
| 292 |
$array = array( |
| 293 |
'label' => '[' . $field->id . ' show=field_label]', |
| 294 |
'val' => '[' . $value . ']', |
| 295 |
'type' => $field->type, |
| 296 |
); |
| 297 |
|
| 298 |
$this->array_content[ $field->id ] = apply_filters( 'frm_field_shortcodes_for_default_html_email', $array, $field ); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Check if the format is default plain text |
| 303 |
* |
| 304 |
* @since 2.05 |
| 305 |
* |
| 306 |
* @return bool |
| 307 |
*/ |
| 308 |
protected function is_plain_text_format() { |
| 309 |
return ( $this->format === 'text' && $this->is_plain_text === true ); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Check if the format is default HTML |
| 314 |
* |
| 315 |
* @since 2.05 |
| 316 |
* |
| 317 |
* @return bool |
| 318 |
*/ |
| 319 |
protected function is_table_format() { |
| 320 |
return ( $this->format === 'text' && $this->is_plain_text === false ); |
| 321 |
} |
| 322 |
} |
| 323 |
|