1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
<?php
/** * Class MC4WP_Form_Output_Manager * * @ignore * @access private */ class MC4WP_Form_Output_Manager {
/** * @var int The # of forms outputted */ public $count = 0;
/** * @const string */ const SHORTCODE = 'mc4wp_form';
/** * Add hooks */ public function add_hooks() { // enable shortcodes in form content add_filter( 'mc4wp_form_content', 'do_shortcode' ); add_action( 'init', array( $this, 'register_shortcode' ) ); }
/** * Registers the [mc4wp_form] shortcode */ public function register_shortcode() { add_shortcode( self::SHORTCODE, array( $this, 'shortcode' ) ); }
/** * @param array $attributes * @param string $content * @return string */ public function shortcode( $attributes = array(), $content = '' ) { $default_attributes = array( 'id' => '', 'lists' => '', 'email_type' => '', 'element_id' => '', 'element_class' => '', );
$attributes = shortcode_atts( $default_attributes, $attributes, self::SHORTCODE );
$config = array( 'element_id' => $attributes['element_id'], 'lists' => $attributes['lists'], 'email_type' => $attributes['email_type'], 'element_class' => $attributes['element_class'], );
return $this->output_form( $attributes['id'], $config, false ); }
/** * @param int $id * @param array $config * @param bool $echo * * @return string */ public function output_form( $id = 0, $config = array(), $echo = true ) { try { $form = mc4wp_get_form( $id ); } catch ( Exception $e ) { if ( current_user_can( 'manage_options' ) ) { return sprintf( '<strong>Mailchimp for WordPress error:</strong> %s', $e->getMessage() ); }
return ''; }
$this->count++;
// set a default element_id if none is given if ( empty( $config['element_id'] ) ) { $config['element_id'] = 'mc4wp-form-' . $this->count; }
$form_html = $form->get_html( $config['element_id'], $config );
try { // start new output buffer ob_start();
/** * Runs just before a form element is outputted. * * @since 3.0 * * @param MC4WP_Form $form */ do_action( 'mc4wp_output_form', $form );
// output the form (in output buffer) echo $form_html;
// grab all contents in current output buffer & then clean + end it. $html = ob_get_clean(); } catch ( Error $e ) { $html = $form_html; }
// echo content if necessary if ( $echo ) { echo $html; }
return $html; } }
|