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_indexed_by_components_slugs($post_id, $only_nav_items_options = false) { $navigation_indexed_by_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)) { $component = WpakComponentsStorage::get_component($post_id, $nav_item->component_id); if (WpakComponentsTypes::component_type_exists($component->type)) { $navigation_indexed_by_components[$component->slug] = $only_nav_items_options ? $nav_item->options : $nav_item; } } } } return $navigation_indexed_by_components; }
$options = $component->options; $current_hook = $options['hook']; } ?> <div class="component-params"> <label><?php _e('Hook name', WpAppKit::i18n_domain); ?> : </label> <input type="text" name="hook" value="<?php echo $current_hook; ?> " /> </div> <?php } public function echo_form_javascript() { } public function get_ajax_action_html_answer($action, $params) { } public function get_options_from_posted_form($data) { $hook = !empty($data['hook']) ? $data['hook'] : ''; $options = array('hook' => $hook); return $options; } } WpakComponentsTypes::register_component_type('hooks', array('label' => __('Custom component, using hooks', WpAppKit::i18n_domain)));
} self::echo_sub_options_html($post_type_object); break; } } public function get_options_from_posted_form($data) { $post_type = !empty($data['post_type']) ? $data['post_type'] : 'page'; $page_id = !empty($data['page_id']) ? $data['page_id'] : 0; $post_type_object = get_post_type_object($post_type); if ($post_type_object == null) { $post_type = ''; $page_id = 0; } $with_subtree = !empty($data['with_subtree']); $options = array('post_type' => $post_type, 'page' => $page_id, 'with_subtree' => $with_subtree); return $options; } protected static function get_hierarchical_post_types() { $post_types = get_post_types(array('hierarchical' => true), 'objects'); return $post_types; } protected static function hierachical_post_type_exists($post_type) { $post_types = get_post_types(array('hierarchical' => true), 'names'); return in_array($post_type, $post_types); } } WpakComponentsTypes::register_component_type('page', array('label' => __('WordPress page', WpAppKit::i18n_domain)));
private static function get_navigation_row($post_id, $i, $nav_item_id, $nav_item) { $error_class = ''; $alternate_class = $i % 2 ? '' : 'alternate'; $component = WpakComponentsStorage::get_component($post_id, $nav_item->component_id); // // Component type could be unknown if an addon's component has been added to the app and the addon isn't activated anymore // An addon could be seen as deactivated either if the corresponding plugin is deactivated, or if the corresponding checkbox is unchecked for the given app // if (!WpakComponentsTypes::component_type_exists($component->type)) { $error_class = ' error'; $error_message = __('Component type doesn\'t exist, this component won\'t be included into the app', WpAppKit::i18n_domain); } ob_start(); ?> <?php if (!empty($component)) { ?> <tr class="ui-state-default <?php echo $alternate_class . $error_class; ?> navigation-item navigation-item-component-<?php echo $nav_item->component_id; ?> " data-id="<?php echo $nav_item_id; ?> " id="navigation-item-row-<?php echo $nav_item_id; ?> "> <td> <span class="label"><?php echo $component->label; ?> </span> (<span class="slug"><?php echo $component->slug; ?> </span>) <input type="hidden" id="position-<?php echo $nav_item_id; ?> " name="positions[<?php echo $nav_item_id; ?> ]" value="<?php echo $nav_item->position; ?> " /> <div class="row-actions"> <span class="trash"><a class="submitdelete delete_navigation_item" href="#" data-post-id="<?php echo $post_id; ?> " data-id="<?php echo $nav_item_id; ?> "><?php _e('Delete', WpAppKit::i18n_domain); ?> </a></span> </div> </td> <td> <?php if (empty($error_message)) { ?> <?php $icon_value = $nav_item->options['icon_slug']; ?> <div id="nav-item-value-<?php echo $nav_item_id; ?> "> <span id="span-<?php echo $nav_item_id; ?> "><?php echo !empty($icon_value) ? $icon_value : __('none', WpAppKit::i18n_domain); ?> </span> <div class="row-actions"> <a href="#" class="change-icon-slug" data-id="<?php echo $nav_item_id; ?> "><?php _e('Edit icon', WpAppKit::i18n_domain); ?> </a> </div> </div> <div id="nav-item-input-<?php echo $nav_item_id; ?> " style="display:none" > <input type="text" id="icon-<?php echo $nav_item_id; ?> " class="menu-item-icon-input" data-id="<?php echo $nav_item_id; ?> " value="<?php echo $nav_item->options['icon_slug']; ?> "/> <a href="#" id="change-icon-slug-ok-<?php echo $nav_item_id; ?> " class="change-icon-slug-ok" data-id="<?php echo $nav_item_id; ?> " data-post-id="<?php echo $post_id; ?> " ><?php _e('Ok', WpAppKit::i18n_domain); ?> </a> <a href="#" class="change-icon-slug-cancel" data-id="<?php echo $nav_item_id; ?> "><?php _e('Cancel', WpAppKit::i18n_domain); ?> </a> </div> <?php } else { ?> <div id="nav-item-value-<?php echo $nav_item_id; ?> "> <span id="span-<?php echo $nav_item_id; ?> "><?php echo $error_message; ?> </span> </div> <?php } ?> </td> </tr> <?php } ?> <?php $navigation_row_html = ob_get_contents(); ob_end_clean(); return $navigation_row_html; }
} ?> <?php } ?> <?php } else { ?> <?php echo sprintf(__('No taxonomy found for post type %s', WpAppKit::i18n_domain), $current_post_type); ?> <?php } ?> <?php } ?> <?php } public function get_options_from_posted_form($data) { $post_type = !empty($data['post-type']) ? $data['post-type'] : ''; $taxonomy = !empty($data['taxonomy']) ? $data['taxonomy'] : ''; $term = !empty($data['term']) ? $data['term'] : ''; $hook = !empty($data['hook']) ? $data['hook'] : ''; $options = array('post-type' => $post_type, 'taxonomy' => $taxonomy, 'term' => $term, 'hook' => $hook); return $options; } } WpakComponentsTypes::register_component_type('posts-list', array('label' => __('Posts list', WpAppKit::i18n_domain)));
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); }