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.6 4.8.7 4.8.8 4.8.9 4.9.0 4.9.1 4.9.10 4.9.11 4.9.12 4.9.13 4.9.14 4.9.15 4.9.16 4.9.17 4.9.18 4.9.19 4.9.2 4.9.20 4.9.21 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 trunk 1.1.5 1.2.1 1.2.3 1.2.4 1.2.5 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8
mailchimp-for-wp / includes / forms / class-asset-manager.php
mailchimp-for-wp / includes / forms Last commit date
views 10 years ago admin-functions.php 10 years ago class-admin.php 10 years ago class-asset-manager.php 10 years ago class-form-element.php.php 10 years ago class-form-listener.php 10 years ago class-form-manager.php 10 years ago class-form-message.php 10 years ago class-form-previewer.php 10 years ago class-form-tags.php 10 years ago class-form.php 10 years ago class-output-manager.php 10 years ago class-widget.php 10 years ago functions.php 10 years ago
class-asset-manager.php
251 lines
1 <?php
2
3 /**
4 * This class takes care of all form related functionality
5 *
6 * @access private
7 * @ignore
8 */
9 class MC4WP_Form_Asset_Manager {
10
11 /**
12 * @var MC4WP_Form_Output_Manager
13 */
14 protected $output_manager;
15
16 /**
17 * @var bool
18 */
19 protected $scripts_loaded = false;
20
21 /**
22 * @var string
23 */
24 public $filename_suffix;
25
26 /**
27 * Constructor
28 *
29 * @param MC4WP_Form_Output_Manager $output_manager
30 */
31 public function __construct( MC4WP_Form_Output_Manager $output_manager ) {
32 $this->output_manager = $output_manager;
33 $this->filename_suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
34 }
35
36 /**
37 * Init all form related functionality
38 */
39 public function initialize() {
40 $this->register_assets();
41 $this->add_hooks();
42 }
43
44 /**
45 * Add hooks
46 */
47 public function add_hooks() {
48 // load checkbox css if necessary
49 add_action( 'wp_enqueue_scripts', array( $this, 'load_stylesheets' ) );
50 add_action( 'mc4wp_output_form', array( $this, 'load_scripts' ) );
51 add_action( 'wp_footer', array( $this, 'print_javascript' ), 999 );
52 }
53
54 /**
55 * Register the various JS files used by the plugin
56 */
57 public function register_assets() {
58 global $wp_scripts;
59
60 $suffix = $this->filename_suffix;
61
62 // register client-side API script
63 wp_register_script( 'mc4wp-forms-api', MC4WP_PLUGIN_URL . 'assets/js/forms-api'. $suffix .'.js', array(), MC4WP_VERSION, true );
64
65 // register placeholder script, which will later be enqueued for IE only
66 wp_register_script( 'mc4wp-placeholders', MC4WP_PLUGIN_URL . 'assets/js/third-party/placeholders.min.js', array(), MC4WP_VERSION, true );
67 $wp_scripts->add_data( 'mc4wp-placeholders', 'conditional', 'lte IE 9' );
68
69 // register stylesheets
70 $stylesheets = array(
71 'basic',
72 'themes'
73 );
74 foreach( $stylesheets as $stylesheet ) {
75 wp_register_style( 'mc4wp-form-' . $stylesheet, MC4WP_PLUGIN_URL . 'assets/css/form-' . $stylesheet .$suffix . '.css', array(), MC4WP_VERSION );
76 }
77
78 /**
79 * Runs right after all assets (scripts & stylesheets) for forms have been registered
80 *
81 * @since 3.0
82 *
83 * @param string $suffix The suffix to add to the filename, before the file extension. Is usually set to ".min".
84 * @ignore
85 */
86 do_action( 'mc4wp_register_form_assets', $suffix );
87 }
88
89 /**
90 * Load the various stylesheets
91 */
92 public function load_stylesheets( ) {
93
94 $stylesheets = (array) get_option( 'mc4wp_form_stylesheets', array() );
95
96 /**
97 * Filters the stylesheets to be loaded
98 *
99 * Should be an array of stylesheet handles previously registered using `wp_register_style`.
100 * Each value is prefixed with `mc4wp-form-` to get the handle.
101 *
102 * Return an empty array if you want to disable the loading of all stylesheets.
103 *
104 * @since 3.0
105 * @param array $stylesheets Array of valid stylesheet handles
106 */
107 $stylesheets = (array) apply_filters( 'mc4wp_form_stylesheets', $stylesheets );
108
109 foreach( $stylesheets as $stylesheet ) {
110 $handle = 'mc4wp-form-' . $stylesheet;
111 // TODO: check if stylesheet handle is registered?
112 wp_enqueue_style( $handle );
113 }
114
115 /**
116 * @ignore
117 */
118 do_action( 'mc4wp_load_form_stylesheets', $stylesheets );
119
120 return true;
121 }
122
123 /**
124 * Get configuration object for client-side use.
125 *
126 * @return array
127 */
128 public function get_javascript_config() {
129
130 $submitted_form = mc4wp_get_submitted_form();
131
132 if( ! $submitted_form ) {
133 return array();
134 }
135
136 $config = array(
137 'submitted_form' => array(
138 'id' => $submitted_form->ID,
139 'data' => $submitted_form->data,
140 'action' => $submitted_form->config['action'],
141 'element_id' => $submitted_form->config['element_id'],
142 )
143 );
144
145 if( $submitted_form->has_errors() ) {
146 $config['submitted_form']['errors'] = $submitted_form->errors;
147 }
148
149 $auto_scroll = 'default';
150
151 /**
152 * Filters the `auto_scroll` setting for when a form is submitted.
153 *
154 * Accepts the following values:
155 *
156 * - false
157 * - "default"
158 * - "animated"
159 *
160 * @param boolean|string $auto_scroll
161 * @since 3.0
162 */
163 $config['auto_scroll'] = apply_filters( 'mc4wp_form_auto_scroll', $auto_scroll );
164
165 return $config;
166 }
167
168 /**
169 * Load JavaScript files
170 * @return bool
171 */
172 public function load_scripts() {
173
174 if( $this->scripts_loaded ) {
175 return false;
176 }
177
178 // print dummy JS
179 $this->print_dummy_javascript();
180
181 // load API script
182 wp_localize_script( 'mc4wp-forms-api', 'mc4wp_forms_config', $this->get_javascript_config() );
183 wp_enqueue_script( 'mc4wp-forms-api' );
184
185 // load placeholder polyfill if browser is Internet Explorer
186 if( ! empty( $GLOBALS['is_IE'] ) ) {
187 wp_enqueue_script( 'mc4wp-placeholders' );
188 }
189
190 $this->scripts_loaded = true;
191 return true;
192 }
193
194 /**
195 * Prints dummy JavaScript which allows people to call `mc4wp.forms.on()` before the JS is loaded.
196 */
197 public function print_dummy_javascript() {
198 $file = MC4WP_PLUGIN_DIR . "assets/js/forms-dummy-api{$this->filename_suffix}.js";
199 echo '<script type="text/javascript">';
200 include $file;
201 echo '</script>';
202 }
203
204 /**
205 * Returns the MailChimp for WP form mark-up
206 *
207 * @return string
208 */
209 public function print_javascript() {
210
211 // don't print any scripts if this page has no forms
212 if( empty( $this->output_manager->printed_forms ) ) {
213 return false;
214 }
215
216 // make sure scripts are loaded
217 $this->load_scripts();
218
219 // print inline scripts depending on printed fields
220 echo '<script type="text/javascript">';
221 echo '(function() {';
222
223 // include general form enhancements
224 include dirname( __FILE__ ) . '/views/js/general-form-enhancements.js';
225
226 // include url fix
227 if( in_array( 'url', $this->output_manager->printed_field_types ) ) {
228 include dirname( __FILE__ ) . '/views/js/url-fields.js';
229 }
230
231 // include date polyfill?
232 if( in_array( 'date', $this->output_manager->printed_field_types ) ) {
233 include dirname( __FILE__ ) . '/views/js/date-fields.js';
234 }
235
236 echo '})();';
237 echo '</script>';
238
239 /**
240 * Runs right after inline JavaScript is printed, just before the closing </body> tag.
241 *
242 * This function will only run if the current page contains at least one form.
243 *
244 * @ignore
245 */
246 do_action( 'mc4wp_print_forms_javascript' );
247 }
248
249
250 }
251