public static function ajax_wpak_edit_component() { $answer = array('ok' => 0, 'message' => '', 'type' => 'error', 'html' => '', 'component' => array()); if (empty($_POST['post_id']) || empty($_POST['nonce']) || !check_admin_referer('wpak-component-data-' . $_POST['post_id'], 'nonce')) { exit('bad nonce'); } $action = $_POST['wpak_action']; $data = $_POST['data']; WpakAddons::require_app_addons_php_files(intval($_POST['post_id'])); if ($action == 'add_or_update') { // Unslash POST data before manipulating DB $data = wp_unslash($data); $post_id = $data['component_post_id']; if (empty($post_id)) { $answer['message'] = __("Application not found.", WpAppKit::i18n_domain); self::exit_sending_json($answer); } $edit = !empty($data['component_id']); $edit_id = $edit ? intval($data['component_id']) : 0; $component_label = trim($data['component_label']); $component_type = $data['component_type']; if (empty($component_label)) { $answer['message'] = __('You must provide a label for the component!', WpAppKit::i18n_domain); self::exit_sending_json($answer); } if (is_numeric($component_label)) { $answer['message'] = __("The component label can't be numeric.", WpAppKit::i18n_domain); self::exit_sending_json($answer); } $component_slug = $edit ? trim($data['component_slug']) : $component_label; $component_slug = sanitize_title_with_dashes(remove_accents($component_slug)); if (empty($component_slug)) { $answer['message'] = __("You must provide a slug for the component.", WpAppKit::i18n_domain); self::exit_sending_json($answer); } if (is_numeric($component_slug)) { $answer['message'] = __("The component slug can't be numeric.", WpAppKit::i18n_domain); self::exit_sending_json($answer); } if (WpakComponentsStorage::component_exists($post_id, $component_slug, $edit_id)) { $i = 0; do { $component_index = intval(preg_replace('/.*-(\\d+)$/', '$1', $component_slug)); $component_index++; $component_slug = preg_replace('/-(\\d+)$/', '', $component_slug) . '-' . $component_index; if ($i++ > 100) { break; } } while (WpakComponentsStorage::component_exists($post_id, $component_slug, $edit_id)); } $component_options = WpakComponentsTypes::get_component_type_options_from_posted_form($component_type, $data); $component = new WpakComponent($component_slug, $component_label, $component_type, $component_options); $component_id = WpakComponentsStorage::add_or_update_component($post_id, $component, $edit_id); $answer['component'] = array('id' => $component_id, 'slug' => $component_slug, 'label' => $component_label); $answer['html'] = self::get_component_row($post_id, WpakComponentsStorage::get_nb_components($post_id), $component_id, $component); if ($edit) { $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['message'] = sprintf(__('Component "%s" updated successfuly', WpAppKit::i18n_domain), $component_label); } else { $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['message'] = sprintf(__('Component "%s" created successfuly', WpAppKit::i18n_domain), $component_label); } self::exit_sending_json($answer); } elseif ($action == 'delete') { $id = $data['component_id']; $post_id = $data['post_id']; if (is_numeric($id) && is_numeric($post_id)) { if ($component_id = WpakComponentsStorage::component_exists($post_id, $id)) { if (WpakNavigationItemsStorage::navigation_item_exists_by_component($post_id, $component_id)) { $answer['message'] = __('The component to delete is in the app navigation. Please remove the component from app navigation before deleting it.', WpAppKit::i18n_domain); } else { if (!WpakComponentsStorage::delete_component($post_id, $id)) { $answer['message'] = __('Could not delete component', WpAppKit::i18n_domain); } else { $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['message'] = __('Component deleted successfuly', WpAppKit::i18n_domain); } } } else { $answer['message'] = __('Component to delete not found', WpAppKit::i18n_domain); } } self::exit_sending_json($answer); } //We should not arrive here, but just in case : self::exit_sending_json($answer); }