server = $server; $this->options = get_option("pw-mobile-app"); $this->accountOptions = get_option("androapp_account_settings"); } /** * Register the user-related routes * * @param array $routes Existing routes * @return array Modified routes */ public function register_routes( $routes ) { $user_routes = array( // /users/me is an alias, and simply redirects to /users/ '/pwmenu' => array( array( array( $this, 'get_menu' ), WP_JSON_Server::READABLE ), ), '/androappconfig' => array( array( array( $this, 'get_config' ), WP_JSON_Server::READABLE ), ), '/androappauthcheck' => array( array( array( $this, 'check_auth' ), WP_JSON_Server::READABLE ), ), ); return array_merge( $routes, $user_routes ); } public function check_auth( $key, $context = 'view' ) { $buildOptions = get_option("pw-mobile-build-options"); if(!empty($key) && $buildOptions['authentication_key'] == $key){ return "valid"; } return "Key not valid"; } public function get_config( $context = 'view' ) { $config = array(); $config['menulist'] = $this->get_menu(); $config['monetise'] = $this->get_monetise(); return $config; } private function get_monetise(){ return array( "topAdUnitId" => $this->accountOptions[pw_mobile_app_settings::$topAdUnitKey], "bottomAdUnitId" => $this->accountOptions[pw_mobile_app_settings::$bottomAdUnitKey], "listViewAdUnitId" => $this->accountOptions[pw_mobile_app_settings::$listViewAdUnitKey], "listViewAdFreq" => $this->accountOptions[pw_mobile_app_settings::$listViewAdUnitFreqKey], "listViewAdType" => $this->accountOptions[pw_mobile_app_settings::$listViewAdUnitTypeKey], "interstitialAdUnitId" => $this->accountOptions[pw_mobile_app_settings::$interstitialAdUnitKey], "interstitialAdFreq" => $this->accountOptions[pw_mobile_app_settings::$interstitialAdUnitFreqKey] ); } /* reference: https://github.com/mcnasby/wp-json-api-menu-controller/blob/master/api-controllers/menus.php */ public function get_menu( $context = 'view' ) { $id = $this->options['app_menu']; // Make sure we have key/value query vars if ( $id ) { function getJsonLink($objectType, $objectId, $url){ if($objectType =="category"){ return json_url("/taxonomies/category/terms/".$objectId); }else if($objectType =="page"){ return json_url("/pages/".$objectId); }else if($objectType =="pose"){ return json_url("/posts/".$objectId); } else { return $url; } } function get_nav_items($id) { global $json_api; $menu_output = wp_get_nav_menu_items( $id ); //print_r($menu_output); if($menu_output) { $filtered_items = array_map(function($el){ $filter = array( "id"=>$el->ID, "parent_id"=>$el->menu_item_parent, "menu_order"=>$el->menu_order, "name"=>$el->title, "object_type"=>$el->object, "object_id"=>($el->object == "custom" ? "" : $el->object_id ), "link"=> getJsonLink($el->object,$el->object_id, $el->url) ); return $filter; }, $menu_output); $output = ( false ? $menu_output : $filtered_items); $count = count($output); } else{ $count = 0; } if ($count == "0") { return array( "output" => "Empty Menu", "count" => $count ); } else { return array( "output" => $output, "count" => $count ); } } if ($id) { $menuid = $id; $menuloc = ""; $menu_items = get_nav_items($menuid); } if($menu_items["count"] == 0){ return new WP_Error( 'json_invalid_menu_id', __( 'Invalid or Empty Menu.' ), array( 'status' => 400 ) ); } return $menu_items["output"]; /*array( "count" => $menu_items["count"], "menu_location" => $menuloc, "menu_id" => $menuid, "menu" => $menu_items["output"] );*/ } else { return new WP_Error('json_invalid_menu_id', "Include the parameter 'menu_id' with an appropriate string value.", array( 'status' => 400 )); } } } ?>