'GET', 'callback' => array($this, 'getInfoPages'), )); register_rest_route( 'ponti', '/infopages-slugs', array( 'methods' => 'GET', 'callback' => array($this, 'getInfoPagesSlugs'), )); register_rest_route( 'ponti', '/defaultpages', array( 'methods' => 'GET', 'callback' => array($this, 'getDefaultPages'), )); register_rest_route( 'ponti', '/defaultpages-slugs', array( 'methods' => 'GET', 'callback' => array($this, 'getDefaultPagesSlugs'), )); // uses section (parent) and slug query strings // ie. /more/page?section=parent-slug&slug=child-slug register_rest_route( 'ponti', 'page/', array( 'methods' => 'GET', 'callback' => array($this, 'getSingleBySlug'), )); }); } public function getInfoPages() { $data = array(); $args = array( 'post_type' => 'page', 'post_parent' => 0, 'posts_per_page' => -1, 'fields' => 'ids', 'orderby' => 'menu_order', 'order' => 'ASC', // get all NON default templates 'meta_key' => '_wp_page_template', 'meta_value' => 'default', 'meta_compare' => '!=', // exclude home 'post__not_in' => array( 280 ), ); $query = new WP_Query($args); $i = 0; foreach($query->posts as $post_id) : $data[$i] = self::getSingle($post_id); // Sub Pages $subargs = array( 'post_type' => 'page', 'post_parent' => $post_id, 'fields' => 'ids', 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_key' => '_wp_page_template', 'meta_value' => 'default', 'meta_compare' => '!=' ); $subposts = get_posts( $subargs ); if(!empty($subposts)){ $j = 0; foreach($subposts as $sub_post_id){ $data[$i]['subs'][$j] = self::getSingle($sub_post_id); $j++; } } $i++; endforeach; wp_reset_postdata(); return rest_ensure_response( $data ); } public function getInfoPagesSlugs() { $data = array(); $args = array( 'post_type' => 'page', 'post_parent' => 0, 'posts_per_page' => -1, 'fields' => 'ids', 'orderby' => 'menu_order', 'order' => 'ASC', // get all NON default templates 'meta_key' => '_wp_page_template', 'meta_value' => 'default', 'meta_compare' => '!=', // exclude home 'post__not_in' => array( 280 ) ); $i = 0; $posts = get_posts( $args ); foreach($posts as $post_id){ $data[$i]['slug'] = get_post_field('post_name',$post_id); // Sub Pages $subargs = array( 'post_type' => 'page', 'post_parent' => $post_id, 'fields' => 'ids', 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => [ 'meta_key' => '_wp_page_template', 'meta_value' => 'default', 'meta_compare' => '!=' ] ); $subposts = get_posts( $subargs ); if(!empty($subposts)){ $j = 0; foreach($subposts as $sub_post_id){ $data[$i]['subs'][$j]['slug'] = get_post_field('post_name',$sub_post_id); $j++; } } $i++; } return rest_ensure_response( $data ); } public function getDefaultPages() { $data = array(); $args = array( 'post_type' => 'page', 'posts_per_page' => -1, //'post_parent' => 0, 'fields' => 'ids', 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ); $i = 0; $posts = get_posts( $args ); foreach($posts as $post_id){ $data[$i] = self::getSingle($post_id); $i++; } return rest_ensure_response( $data ); } public function getDefaultPagesSlugs() { $data = array(); $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'fields' => 'ids', 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ); $i = 0; $posts = get_posts( $args ); foreach($posts as $post_id){ $data[$i] = get_post_field('post_name',$post_id); $i++; } return rest_ensure_response( $data ); } public function getSingleBySlug(WP_REST_Request $request ){ $params = $request->get_query_params(); $path = ''; if(!empty($params['section'])){ $path .= $params['section']; } if(!empty($params['section']) && !empty($params['slug'])){ $path .= '/'; } if(!empty($params['slug'])){ $path .= $params['slug']; } $post_id = PONTI_API_Utils::getPostIDfromSlug($path); if($post_id){ $data = self::getSingle($post_id); return rest_ensure_response($data); } else { return rest_ensure_response(['success' => false]); } } public static function getSingle($post_id,$revision_parent_id=null){ if($revision_parent_id==null){ $revision_parent_id = $post_id; } $data = get_fields($post_id); $data['wordpress_id'] = $post_id; $data['title'] = get_post_field('post_title',$post_id); $data['slug'] = get_post_field('post_name',$revision_parent_id); $data['template'] = PONTI_API_Utils::getTemplateName($revision_parent_id); $data = PONTI_API_SEO::getData($data); if($data['template'] == 'home'){ $projects = PONTI_API_Projects::getDataInternal(); $data['projects'] = []; if(!empty($projects)){ foreach($projects as $project){ if($project['show_on_home']){ $data['projects'][] = $project; } } } } return $data; } } new PONTI_API_Pages();\d+)', array( 'methods' => 'GET', 'callback' => array($this, 'getData') )); }); } public function getData($request = null) { $data = array(); $id = $request['id']; if(empty($id) && !intval($id)) { return new WP_REST_Response(array('success' => false, 'message'=> 'No Post ID supplied'), 200); } $post = wp_get_post_autosave($id); if(!$post){ //get original post instead if no auto-save $post = get_post($id); } $post_type = get_post_type($id); if($post_type=='page'){ $data = PONTI_API_Pages::getSingle($post->ID,$id); $post_type = 'infopage'; if($data['template']=='default') $post_type = 'defaultpage'; } if(!empty($post_type)){ $data['postType'] = $post_type; } return rest_ensure_response( $data ); } } new PONTI_API_Preview(); 'GET', 'callback' => array($this, 'getData') )); }); } public function getData() { $data = get_fields('options'); $menu_items = array(); $gallery_items = array(); $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'fields' => 'ids', 'orderby' => 'menu_order', 'order' => 'ASC', 'post__not_in' => array( 31 ), // home ); $i = 0; $posts = get_posts( $args ); foreach($posts as $post_id){ if (get_post_field('post_name',$post_id) == 'gallery') { $menu_items[$i]['slug'] = get_post_field('post_name',$post_id); $menu_items[$i]['title'] = get_post_field('post_title',$post_id); $gallery_items = get_field('images', $post_id); } else { $menu_items[$i]['slug'] = get_post_field('post_name',$post_id); $menu_items[$i]['title'] = get_post_field('post_title',$post_id); } $i++; } $data['menu_items'] = $menu_items; $data['gallery_items'] = $gallery_items; $prefixes = ['seo_defaults']; foreach($prefixes as $prefix){ $data = PONTI_API_Utils::groupFieldsByPrefix($prefix,$data); } return rest_ensure_response( $data ); } } new PONTI_API_Global(); 'description', 'facebook_title' => 'opengraph_title', 'facebook_image' => 'opengraph_image', 'facebook_description' => 'opengraph_description', ]; foreach($fields as $field => $remapped_field){ if(!empty($data['seo'][$field])){ $data['seo'][$remapped_field] = $data['seo'][$field]; unset($data['seo'][$field]); } } // replace separator in titles $separator = get_field('seo_defaults_separator','options'); $fields = ['title', 'opengraph_title','twitter_title']; foreach($fields as $field){ $data['seo'][$field] = str_replace('',$separator,$data['seo'][$field]); } //get image urls $data['seo']['opengraph_image'] = wp_get_attachment_image_url($data['seo']['opengraph_image'],'medium2'); $data['seo']['twitter_image'] = wp_get_attachment_image_url($data['seo']['twitter_image'],'medium2'); return $data; } } new PONTI_API_SEO(); $fieldvalue){ if (strpos($fieldname, $prefix.'_') !== false) { $acf[$prefix][str_replace($prefix.'_', '',$fieldname)] = $fieldvalue; unset($acf[$fieldname]); } } return $acf; } public static function getTemplateName($post_id) { if(!$post_id) return; $data = get_post_meta( $post_id, '_wp_page_template', true ); if($data=='default') return 'default'; $data = str_replace('templates/','',$data); $data = str_replace('.php','',$data); return $data; } public static function getPostIDfromSlug($slug, $post_type = 'page') { $page = get_page_by_path($slug,OBJECT,$post_type); if ($page) { return $page->ID; } else { return null; } } //-- ACF Clone Blocks — move content out of namespaced key into root level public static function namespaceCloneBlocks($blocks) { $data = $blocks; $clone_block_types = array( 'intro_text', 'paragraph_text', 'image', 'image_double', 'image_text', 'heading', 'subheading', 'pull_quote', 'question_answer' ); $i = 0; foreach($blocks as $block){ foreach($clone_block_types as $block_type){ if(isset($block[$block_type])){ $data[$i] = $data[$i][$block_type]; $data[$i]['acf_fc_layout'] = $block_type; if(!empty($block['sidebar'])){ $data[$i]['sidebar'] = $block['sidebar']; } } } $i++; } return $data; } //-- add focal point data to image arrays public static function processImageFocalPoints($acf){ if(!empty($acf['hero_image'])){ $acf['hero_image']['focal_points'] = self::getFocalPoints($acf['hero_image']['ID']); } if(!empty($acf['banner_slider'])){ $j = 0; foreach($acf['banner_slider'] as $slide){ if($slide['use_overrides'] && !empty($slide['override_image'])){ $acf['banner_slider'][$j]['override_image']['focal_points'] = self::getFocalPoints($acf['banner_slider'][$j]['override_image']['ID']); } $j++; } } if(!empty($acf['blocks'])){ $j = 0; foreach($acf['blocks'] as $block){ if(!empty($block['image'])){ $acf['blocks'][$j]['image']['image_content']['focal_points'] = self::getFocalPoints($acf['blocks'][$j]['image']['image_content']['ID']); } if(!empty($block['image_double'])){ for($i=0;$i<1;$i++){ $acf['blocks'][$j]['image_double']['images'][$i]['image']['focal_points'] = self::getFocalPoints($acf['blocks'][$j]['image_double']['images'][$i]['image']['ID']); } } if(!empty($block['image_text'])){ $acf['blocks'][$j]['image_text']['image']['focal_points'] = self::getFocalPoints($acf['blocks'][$j]['image_text']['image']['ID']); } $j++; } } return $acf; } // -- requires WP Smart Crop plugin public static function getFocalPoints($img_id){ $data = array( 'enabled' => get_post_meta($img_id,'_wpsmartcrop_enabled',true) ); if($data['enabled']){ $data['pos'] = get_post_meta($img_id,'_wpsmartcrop_image_focus',true); } return $data; } // formats date with timezone public static function formatDate($format="r", $timestamp=false, $timezone='Australia/Melbourne'){ $userTimezone = new DateTimeZone(!empty($timezone) ? $timezone : 'GMT'); $gmtTimezone = new DateTimeZone('GMT'); $myDateTime = new DateTime(($timestamp!=false?date("r",(int)$timestamp):date("r")), $gmtTimezone); $offset = $userTimezone->getOffset($myDateTime); return date($format, ($timestamp!=false?(int)$timestamp:$myDateTime->format('U')) + $offset); } public static function requestingIPAllowed(){ if(USE_API_WHITELISTED_IPS){ //local and netlify IPs if(!in_array($_SERVER['REMOTE_ADDR'], API_WHITELISTED_IPS)){ $message = 'API ERROR: '.$_SERVER['REMOTE_ADDR']. ' tried to access API but was stopped.'; error_log(print_r($message, true)); return false; } } return true; } public static function limit_text($text, $limit) { if (str_word_count($text, 0) > $limit) { $words = str_word_count($text, 2); $pos = array_keys($words); $text = substr($text, 0, $pos[$limit]) . '...'; } return $text; } } new PONTI_API_Utils();