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 |