/** * Add a new module * * @since 1.0 */ function dslc_ajax_add_module($atts) { // Allowed to do this? if (is_user_logged_in() && current_user_can(DS_LIVE_COMPOSER_CAPABILITY)) { // The array we'll pass back to the AJAX call $response = array(); // The ID of the module to add $module_id = $_POST['dslc_module_id']; $post_id = $_POST['dslc_post_id']; if (isset($_POST['dslc_preload_preset']) && $_POST['dslc_preload_preset'] == 'enabled') { $preload_preset = 'enabled'; } else { $preload_preset = 'disabled'; } // If post ID is not numberic stop execution if (!is_numeric($post_id)) { return; } /** * The instance ID for this specific module */ // If it is not a new module ( already has ID ) if (isset($_POST['dslc_module_instance_id'])) { $module_instance_id = $_POST['dslc_module_instance_id']; // If it is a new module ( no ID ) } else { // Get current count $module_id_count = get_option('dslc_module_id_count'); // If not the first one if ($module_id_count) { // Increment by one $module_instance_id = $module_id_count + 1; // Update the count update_option('dslc_module_id_count', $module_instance_id); // If it is the first one } else { // Set 1 as the ID $module_instance_id = 1; // Update the count update_option('dslc_module_id_count', $module_instance_id); } } // If module instance ID not numeric stop execution if (!is_numeric($module_instance_id)) { return; } // Instanciate the module class $module_instance = new $module_id(); // Generate settings $all_opts = $module_instance->options(); $module_settings = dslc_module_settings($all_opts); // Append ID to settings $module_settings['module_instance_id'] = $module_instance_id; // Append post ID to settings $module_settings['post_id'] = $post_id; // Start output fetching ob_start(); // Load preset if there was no preset before if ($preload_preset == 'enabled') { $module_settings = apply_filters('dslc_filter_settings', $module_settings); } // Transform image ID to URL global $dslc_var_image_option_bckp; $dslc_var_image_option_bckp = array(); foreach ($all_opts as $all_opt) { if ($all_opt['type'] == 'image') { if (isset($module_settings[$all_opt['id']]) && !empty($module_settings[$all_opt['id']]) && is_numeric($module_settings[$all_opt['id']])) { $dslc_var_image_option_bckp[$all_opt['id']] = $module_settings[$all_opt['id']]; $image_info = wp_get_attachment_image_src($module_settings[$all_opt['id']], 'full'); $module_settings[$all_opt['id']] = $image_info[0]; } } } // Module size if (isset($_POST['dslc_m_size'])) { $module_settings['dslc_m_size'] = $_POST['dslc_m_size']; } else { $module_settings['dslc_m_size'] = '12'; } // Output $module_instance->output($module_settings); // Get the output and stop fetching $output = ob_get_contents(); ob_end_clean(); // Set the output $response['output'] = $output; // Encode response $response_json = json_encode($response); // Send the response header("Content-Type: application/json"); echo $response_json; // Good night exit; } }
/** * Add a new module * * @since 1.0 */ function dslc_ajax_add_module($atts) { // Allowed to do this? if (is_user_logged_in() && current_user_can(DS_LIVE_COMPOSER_CAPABILITY)) { // The array we'll pass back to the AJAX call. $response = array(); // The ID of the module to add. $module_id = $_POST['dslc_module_id']; $post_id = $_POST['dslc_post_id']; if (isset($_POST['dslc_preload_preset']) && 'enabled' === $_POST['dslc_preload_preset']) { $preload_preset = 'enabled'; } else { $preload_preset = 'disabled'; } /** * The instance ID for this specific module */ // If it is not a new module ( already has ID )? if (isset($_POST['dslc_module_instance_id'])) { $module_instance_id = esc_attr($_POST['dslc_module_instance_id']); // If it is a new module ( no ID )? } else { // Get current count. $module_id_count = get_option('dslc_module_id_count'); // If not the first one? if ($module_id_count) { // Increment by one. $module_instance_id = $module_id_count + 1; // Update the count. update_option('dslc_module_id_count', $module_instance_id); // If it is the first one? } else { // Set 1 as the ID. $module_instance_id = 1; // Update the count. update_option('dslc_module_id_count', $module_instance_id); } } // Instanciate the module class. $module_instance = new $module_id(); // Generate settings. // Array $all_opts - has a structure of the module setting (not actual data). $all_opts = $module_instance->options(); /** * Array $module_settings - has all the module settings (actual data). * Ex.: [css_bg_color] => rgb(184, 61, 61). * * Function dslc_module_settings form the module settings array * based on default settings + current settings. * * Function dslc_module_settings get current module settings * form $_POST[ $option['id'] ]. */ $module_settings = dslc_module_settings($all_opts); // Append ID to settings. $module_settings['module_instance_id'] = $module_instance_id; // Append post ID to settings. $module_settings['post_id'] = $post_id; // Start output fetching. ob_start(); // Load preset if there was no preset before. if ('enabled' === $preload_preset) { $module_settings = apply_filters('dslc_filter_settings', $module_settings); } // Transform image ID to URL. global $dslc_var_image_option_bckp; $dslc_var_image_option_bckp = array(); foreach ($all_opts as $all_opt) { if ('image' === $all_opt['type']) { if (isset($module_settings[$all_opt['id']]) && !empty($module_settings[$all_opt['id']]) && is_numeric($module_settings[$all_opt['id']])) { $dslc_var_image_option_bckp[$all_opt['id']] = $module_settings[$all_opt['id']]; $image_info = wp_get_attachment_image_src($module_settings[$all_opt['id']], 'full'); $module_settings[$all_opt['id']] = $image_info[0]; } } } // Module size. if (isset($_POST['dslc_m_size'])) { $module_settings['dslc_m_size'] = $_POST['dslc_m_size']; } else { $module_settings['dslc_m_size'] = '12'; } // Output. $module_instance->output($module_settings); // Get the output and stop fetching. $output = ob_get_contents(); ob_end_clean(); // Set the output. $response['output'] = $output; // Encode response. $response_json = wp_json_encode($response); // Send the response. header('Content-Type: application/json'); echo $response_json; // Good night. exit; } }