textdomain . '/v1', '/home', array( 'methods' => 'GET', 'callback' => array(&$this, 'home'), 'permission_callback' => '__return_true' )); register_rest_route($this->textdomain . '/v1', '/posts/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', array( 'methods' => 'GET', 'callback' => array(&$this, 'posts'), 'permission_callback' => '__return_true' )); register_rest_route($this->textdomain . '/v1', '/menus/(?P[a-zA-Z0-9_-]+)', array( 'methods' => 'GET', 'callback' => array(&$this, 'wp_api_v2_menus_get_all_menus'), 'permission_callback' => '__return_true' )); register_rest_route($this->textdomain . '/v1', '/locations/(?P[a-zA-Z0-9_-]+)', array( 'methods' => 'GET', 'callback' => array(&$this, 'wp_api_v2_locations_get_menu_data'), 'permission_callback' => '__return_true' )); register_rest_route($this->textdomain . '/v1', '/post/(?P[a-zA-Z0-9_-]+)', array( 'methods' => 'GET,POST', 'callback' => array(&$this, 'getPostBySlug'), 'permission_callback' => '__return_true' )); register_rest_route($this->textdomain . '/v1', '/post', array( 'methods' => 'POST', 'callback' => array(&$this, 'getPostBySlug'), 'permission_callback' => '__return_true' )); register_rest_route($this->textdomain . '/v1', '/archive/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', array( 'methods' => 'POST', 'callback' => array(&$this, 'getPostsByCat'), 'permission_callback' => '__return_true' )); register_rest_route($this->textdomain . '/v1', '/theme-options/(?P[a-zA-Z0-9_-]+)', array( 'methods' => 'GET', 'callback' => array(&$this, 'themeOptions'), 'permission_callback' => '__return_true' )); } function home($data) { $page_on_front = get_option('page_on_front'); // header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, token'); // $args = array( //// 'offset' => $data['offset'], // 'ID' =>$page_on_front, //// 'posts_per_page' => $data['limit'], //// 'post_type' => 'post', //// 'post_status' => 'publish' // ); // print_r(get_option( 'page_on_front' )); // die(); $my_posts = get_post($page_on_front); if ($my_posts) { $blocks = parse_blocks($my_posts->post_content); $html = ''; foreach ($blocks as $block) { $html .= render_block($block); } return [ 'success' => true, 'result' => [ 'ID' => $my_posts->ID, 'post_author' => $my_posts->post_author, 'post_content' => $html, 'post_content_main' => $my_posts->post_content, '$blocks' => $blocks, 'post_date' => $my_posts->post_date, 'post_excerpt' => $my_posts->post_excerpt, 'post_title' => $my_posts->post_title, 'post_status' => $my_posts->post_status, 'post_name' => $my_posts->post_name, ] ]; } else { return [ 'success' => false, 'result' => $this->getPosts([ 'offset'=>0, 'limit'=>10 ]) ]; } } function getPosts($data) { $args = array( 'offset' => $data['offset'], 'posts_per_page' => $data['limit'], // 'post_type' => 'post', 'post_status' => 'publish' ); // print_r(get_option( 'page_on_front' )); // die(); // $my_posts = get_posts($args); $my_posts = new WP_Query($args); // 'post_type' => ['post'], // )); $ar = []; if ($my_posts->have_posts()) : while ($my_posts->have_posts()) : $my_posts->the_post(); global $post; $blocks = parse_blocks($post->post_content); $html = ''; foreach ($blocks as $block) { $html .= render_block($block); } $html = do_shortcode($html, true); $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'featured'); array_push($ar, [ 'ID' => $post->ID, 'post_author' => $post->post_author, 'post_content' => $html, 'post_date' => $post->post_date, 'post_excerpt' => $post->post_excerpt, 'post_title' => $post->post_title, 'post_status' => $post->post_status, 'post_name' => $post->post_name, 'post_image_featured' => $image, // 'slug' => $post->post_name, // 'taxonomy' => $parameters['taxonomy'] ]); endwhile; endif; // if ($my_posts) { return $ar; // if ($my_posts) { // // return $my_posts; // } else { // return false; // } } function posts($data) { $args = array( 'offset' => $data['offset'], 'posts_per_page' => $data['limit'], // 'post_type' => 'post', 'post_status' => 'publish' ); // print_r(get_option( 'page_on_front' )); // die(); $my_posts = get_posts($args); if ($my_posts) { return [ 'success' => true, 'result' => [ 'ID' => $my_posts[0]->ID, 'post_author' => $my_posts[0]->post_author, 'post_content' => parse_blocks($my_posts[0]->post_content), 'post_date' => $my_posts[0]->post_date, 'post_excerpt' => $my_posts[0]->post_excerpt, 'post_title' => $my_posts[0]->post_title, 'post_status' => $my_posts[0]->post_status, 'post_name' => $my_posts[0]->post_name, ] ]; } else { return [ 'success' => false, 'result' => [] ]; } } function getPostBySlug(WP_REST_Request $req) { // $body = $req->get_body(); $parameters = $req->get_params(); $args = array( 'name' => $parameters['slug'], 'post_type' => ['post', 'page'], 'post_status' => 'publish', 'numberposts' => 1 ); $p = $this->getPostSimple($args); if (!$p) { $args = array( 'ID' => $parameters['slug'], 'post_type' => ['post', 'page'], 'post_status' => 'publish', 'numberposts' => 1 ); $p = $this->getPostSimple($args); } return $p; } function getPostSimple($args) { $my_posts = get_posts($args); if ($my_posts) { $blocks = parse_blocks($my_posts[0]->post_content); $html = ''; foreach ($blocks as $block) { $html .= render_block($block); } $html = do_shortcode($html, true); return [ 'ID' => $my_posts[0]->ID, 'post_author' => $my_posts[0]->post_author, 'post_content' => $html, 'post_date' => $my_posts[0]->post_date, 'post_excerpt' => $my_posts[0]->post_excerpt, 'post_title' => $my_posts[0]->post_title, 'post_status' => $my_posts[0]->post_status, 'post_name' => $my_posts[0]->post_name, ]; } else { return $my_posts; } } function getPostsByCat(WP_REST_Request $req) { $parameters = $req->get_params(); $args = array( 'post_type' => ['post'], 'post_status' => 'publish' ); if ($parameters['limit']) { $args['posts_per_page'] = $parameters['limit']; } if ($parameters['offset']) { $args['offset'] = $parameters['offset']; } if ($parameters['params']['slug'] && $parameters['taxonomy']) { $args['tax_query'] = array( array( 'taxonomy' => $parameters['taxonomy'], 'field' => 'slug', 'terms' => $parameters['params']['slug'] ) ); } // $my_posts = get_posts($args); $my_posts = new WP_Query($args); // 'post_type' => ['post'], // )); $ar = []; if ($my_posts->have_posts()) : while ($my_posts->have_posts()) : $my_posts->the_post(); global $post; $blocks = parse_blocks($post->post_content); $html = ''; foreach ($blocks as $block) { $html .= render_block($block); } $html = do_shortcode($html, true); $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'featured'); array_push($ar, [ 'ID' => $post->ID, 'post_author' => $post->post_author, 'post_content' => $html, 'post_date' => $post->post_date, 'post_excerpt' => $post->post_excerpt, 'post_title' => $post->post_title, 'post_status' => $post->post_status, 'post_name' => $post->post_name, 'post_image_featured' => $image, 'slug' => $parameters['params']['slug'], 'taxonomy' => $parameters['taxonomy'] ]); endwhile; endif; // if ($my_posts) { return $ar; // } else { // return $my_posts; // } } function return_widgets() { // return true; // Calling the footer sidebar if it exists. dynamic_sidebar('akbar_right_sidebar'); } function themeOptions($data) { // $option_name = ''; // // // Gets option name as defined in the theme // if (function_exists('optionsframework_option_name')) { // $option_name = optionsframework_option_name(); // } // // // Fallback option name // if ('' == $option_name) { // $option_name = get_option('stylesheet'); // $option_name = preg_replace("/\W/", "_", strtolower($option_name)); // } // // // Get option settings from database // $options = get_option($option_name); // $home_url = home_url(); // //// $tblm = $this->wp_api_v2_locations_get_menu_data(['id' => 'tblm']); // $footerm = $this->wp_api_v2_locations_get_menu_data(['id' => 'footerm']); // $headerm = $this->wp_api_v2_locations_get_menu_data(['id' => 'headerm']); //// $tbrm = $this->wp_api_v2_locations_get_menu_data(['id' => 'tbrm']); // $mainmenu = $this->wp_api_v2_locations_get_menu_data(['id' => 'mainmenu']); // if ($mainmenu && $mainmenu->items) // foreach ($mainmenu->items as $key => $m) { // $mainmenu->items[$key]->url = str_replace($home_url, "", $m->url); // } // $arr = [ //// 'site_url' => site_url(), // 'home_url' => home_url(), // 'theme_uri' => get_template_directory_uri(), // 'theme_options' => $options, // 'menu' => [ //// 'tblm' => $tblm, //// 'tbrm' => $tbrm, // 'headerm' => $headerm, // 'footerm' => $footerm, // 'mainmenu' => $mainmenu // ] // ]; $arr = akbar_theme_options_generation_array(); $string = "window.globalTS=" . json_encode($arr); // $this->createSettingsJs($string); // akbar_theme_options_generation($string); return $arr; } /** * Get all registered menus * @return array List of menus with slug and description */ function wp_api_v2_menus_get_all_menus() { $menus = get_terms('nav_menu', array('hide_empty' => true)); foreach ($menus as $key => $menu) { // check if there is acf installed if (class_exists('acf')) { $fields = get_fields($menu); if (!empty($fields)) { foreach ($fields as $field_key => $item) { // add all acf custom fields $menus[$key]->$field_key = $item; } } } } return $menus; } /** * Get all locations * @return array List of locations **/ function wp_api_v2_menu_get_all_locations() { $nav_menu_locations = get_nav_menu_locations(); $locations = new stdClass; foreach ($nav_menu_locations as $location_slug => $menu_id) { if (get_term($location_slug) !== null) { $locations->{$location_slug} = get_term($location_slug); } else { $locations->{$location_slug} = new stdClass; } $locations->{$location_slug}->slug = $location_slug; $locations->{$location_slug}->menu = get_term($menu_id); } return $locations; } /** * Get menu's data from his id * * @param array $data WP REST API data variable * * @return object Menu's data with his items */ function wp_api_v2_locations_get_menu_data($data) { // Create default empty object $menu = new stdClass; // this could be replaced with `if (has_nav_menu($data['id']))` if (($locations = get_nav_menu_locations()) && isset($locations[$data['id']])) { // Replace default empty object with the location object $menu = get_term($locations[$data['id']]); $menu->items = $this->wp_api_v2_menus_get_menu_items($locations[$data['id']]); } else { // return new WP_Error('not_found', 'No location has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing location ID or location slug.', array('status' => 404)); return []; } // check if there is acf installed if (class_exists('acf')) { $fields = get_fields($menu); if (!empty($fields)) { foreach ($fields as $field_key => $item) { // add all acf custom fields $menu->$field_key = $item; } } } //print_r($menu); // die(); return $menu; } /** * Check if a menu item is child of one of the menu's element passed as reference * * @param $parents Menu's items * @param $child Menu's item to check * * @return bool True if the parent is found, false otherwise */ function wp_api_v2_menus_dna_test(&$parents, $child) { foreach ($parents as $key => $item) { if ($child->menu_item_parent == $item->ID) { if (!$item->child_items) { $item->child_items = []; } array_push($item->child_items, $child); return true; } // // if ($item->child_items) { // if (wp_api_v2_menus_dna_test($item->child_items, $child)) { // return true; // } // } } return false; } /** * Retrieve items for a specific menu * * @param $id Menu id * * @return array List of menu items */ function wp_api_v2_menus_get_menu_items($id) { $menu_items = wp_get_nav_menu_items($id); // check if there is acf installed if (class_exists('acf')) { foreach ($menu_items as $menu_key => $menu_item) { $fields = get_fields($menu_item->ID); if (!empty($fields)) { foreach ($fields as $field_key => $item) { // add all acf custom fields $menu_items[$menu_key]->$field_key = $item; } } } } // WordPress does not group child menu items with parent menu items $child_items = []; if (!$menu_items) { $menu_items = []; } // pull all child menu items into separate object foreach ($menu_items as $key => $item) { if ($item->type == 'post_type') { // add slug to menu items $slug = basename(get_permalink($item->object_id)); $item->slug = $slug; } else if ($item->type == 'taxonomy') { $cat = get_term($item->object_id); $item->slug = $cat->slug; } else if ($item->type == 'post_type_archive') { $post_type_data = get_post_type_object($item->object); if ($post_type_data->has_archive) { $item->slug = $post_type_data->rewrite['slug']; } } if (isset($item->thumbnail_id) && $item->thumbnail_id) { $item->thumbnail_src = wp_get_attachment_image_url(intval($item->thumbnail_id), 'post-thumbnail'); } if (isset($item->thumbnail_hover_id) && $item->thumbnail_hover_id) { $item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail'); } if ($item->menu_item_parent) { array_push($child_items, $item); unset($menu_items[$key]); } } // push child items into their parent item in the original object do { foreach ($child_items as $key => $child_item) { if ($this->wp_api_v2_menus_dna_test($menu_items, $child_item)) { unset($child_items[$key]); } } } while (count($child_items)); return array_values($menu_items); } /** * Get menu's data from his id. * It ensures compatibility for previous versions when this endpoint * was allowing locations id in place of menus id) * * @param array $data WP REST API data variable * * @return object Menu's data with his items */ function wp_api_v2_menus_get_menu_data($data) { // This ensure retro compatibility with versions `<= 0.5` when this endpoint // was allowing locations id in place of menus id if (has_nav_menu($data['id'])) { $menu = wp_api_v2_locations_get_menu_data($data); } else if (is_nav_menu($data['id'])) { if (is_int($data['id'])) { $id = $data['id']; } else { $id = wp_get_nav_menu_object($data['id']); } $menu = get_term($id); $menu->items = wp_api_v2_menus_get_menu_items($id); } else { return new WP_Error('not_found', 'No menu has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing menu ID, menu slug, location ID or location slug.', array('status' => 404)); } // check if there is acf installed if (class_exists('acf')) { $fields = get_fields($menu); if (!empty($fields)) { foreach ($fields as $field_key => $item) { // add all acf custom fields $menu->$field_key = []; } } } return $menu; } } $AKBAR_API = new akbar_apis();