public static function get_component_items($app_id, $component_slug, $items_ids, $args = array()) { $component_items = array(); if (WpakComponentsStorage::component_exists($app_id, $component_slug)) { $component = WpakComponentsStorage::get_component($app_id, $component_slug); $component_items_by_global = WpakComponentsTypes::get_component_items($component, $items_ids, $args); foreach ($component_items_by_global as $global => $items) { if (is_string($global)) { //to be sure that we did not forget to set a global as key $component_items['globals'][$global] = $items; } } } return $component_items; }
public static function get_navigation_components($post_id) { $components = array(); $navigation_items = self::get_navigation_items_raw($post_id); if (!empty($navigation_items)) { $navigation_items = self::order_items($navigation_items); foreach ($navigation_items as $nav_item_id => $nav_item) { if (WpakComponentsStorage::component_exists($post_id, $nav_item->component_id)) { $components[$nav_item->component_id] = WpakComponentsStorage::get_component($post_id, $nav_item->component_id); } } } return $components; }
public static function ajax_wpak_edit_navigation() { $answer = array('ok' => 0, 'message' => '', 'type' => 'error', 'html' => '', 'data' => ''); if (empty($_POST['post_id']) || !is_numeric($_POST['post_id']) || empty($_POST['nonce']) || !check_admin_referer('wpak-navigation-data-' . $_POST['post_id'], 'nonce')) { exit; } $post_id = $_POST['post_id']; $action = $_POST['wpak_action']; $data = $_POST['data']; if ($action == 'add_or_update') { $post_id = $data['navigation_post_id']; $nav_item_component_id = $data['component_id']; $new_item_position = WpakNavigationItemsStorage::get_nb_navigation_items($post_id) + 1; $nav_item_position = !empty($data['position']) && is_numeric($data['position']) ? $data['position'] : $new_item_position; if (empty($nav_item_component_id)) { $answer['message'] = __('You must choose a component!', WpAppKit::i18n_domain); self::exit_sending_json($answer); } if (!WpakComponentsStorage::component_exists($post_id, $nav_item_component_id)) { $answer['message'] = __("This component doesn't exist!", WpAppKit::i18n_domain); self::exit_sending_json($answer); } if (WpakNavigationItemsStorage::navigation_item_exists_by_component($post_id, $nav_item_component_id)) { $answer['message'] = __('This component is already in navigation!', WpAppKit::i18n_domain); self::exit_sending_json($answer); } $navigation_item = new WpakNavigationItem($nav_item_component_id, $nav_item_position); $navigation_item_id = WpakNavigationItemsStorage::add_or_update_navigation_item($post_id, $navigation_item); $answer['html'] = self::get_navigation_row($post_id, WpakNavigationItemsStorage::get_nb_navigation_items($post_id), $navigation_item_id, $navigation_item); $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['message'] = __('New navigation item created successfuly', WpAppKit::i18n_domain); self::exit_sending_json($answer); } elseif ($action == 'delete') { $nav_item_id = $data['navigation_item_id']; $post_id = $data['post_id']; if (WpakNavigationItemsStorage::navigation_item_exists($post_id, $nav_item_id)) { if (!WpakNavigationItemsStorage::delete_navigation_item($post_id, $nav_item_id)) { $answer['message'] = __('Could not delete navigation item', WpAppKit::i18n_domain); } else { $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['message'] = __('Navigation item deleted successfuly', WpAppKit::i18n_domain); } } else { $answer['message'] = __('Navigation item to delete not found', WpAppKit::i18n_domain); } self::exit_sending_json($answer); } elseif ($action == 'move') { if (!empty($data['positions']) && is_array($data['positions'])) { WpakNavigationItemsStorage::update_items_positions($data['post_id'], $data['positions']); $answer['message'] = __('Navigation order updated successfuly', WpAppKit::i18n_domain); $answer['ok'] = 1; $answer['type'] = 'updated'; } } elseif ($action == 'edit_option') { if (!empty($data['navigation_item_id']) && !empty($data['option'])) { $value = $data['value']; if ($data['option'] == 'icon_slug') { $value = sanitize_file_name(remove_accents($value)); } WpakNavigationItemsStorage::update_item_option($post_id, $data['navigation_item_id'], $data['option'], $value); $answer['message'] = __('Navigation item updated successfuly', WpAppKit::i18n_domain); $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['data'] = $value; } } //We should not arrive here, but just in case : self::exit_sending_json($answer); }
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); }