public function get_related($id = '', $filter = array(), $context = 'view') { $option = get_option('sirp_options'); $num = !empty($filter['num']) ? (int) $filter['num'] : (int) $option['display_num']; $ids = sirp_get_related_posts_id_api($num, $id); $posts_list = array(); foreach ($ids as $id) { $posts_list[] = get_post($id['ID']); } $response = new WP_JSON_Response(); if (!$posts_list) { $response->set_data(array()); return $response; } $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); if (!$this->check_read_permission($post)) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, $context); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
/** * Retrieve ranking * * Overrides the $type to set to 'post', then passes through to the post * endpoints. * * @see WP_JSON_Posts::get_posts() */ public function get_ranking($filter = array(), $context = 'view') { $ids = sga_ranking_get_date($filter); $posts_list = array(); foreach ($ids as $id) { $posts_list[] = get_post($id); } $response = new WP_JSON_Response(); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (!$this->check_read_permission($post)) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, $context); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
function angularScripts() { wp_enqueue_style('bootstrap', plugin_dir_url(__FILE__) . 'css/bootstrap.min.css'); wp_enqueue_style('rjs-styles', plugin_dir_url(__FILE__) . 'css/styles.css'); // Angular Core wp_enqueue_script('angular-core', plugin_dir_url(__FILE__) . 'js/angular.min.js', array('jquery'), NULL, false); wp_enqueue_script('angular-resource', plugin_dir_url(__FILE__) . 'js/angular-resource.min.js', array('angular-core'), NULL, false); wp_enqueue_script('angular-bootstrap-tables', plugin_dir_url(__FILE__) . 'js/angular-smart-table.min.js', array('angular-core'), NULL, false); wp_enqueue_script('angular-sanitize', plugin_dir_url(__FILE__) . 'js/angular-sanitize.min.js', array('jquery'), NULL, false); wp_enqueue_script('angular-route', plugin_dir_url(__FILE__) . 'js/angular-route.min.js', array('jquery'), NULL, false); wp_enqueue_script('html-janitor', plugin_dir_url(__FILE__) . 'js/html-janitor.js', array('jquery'), NULL, false); wp_enqueue_script('angular-app', plugin_dir_url(__FILE__) . 'js/angular-app.js', array('html-janitor'), NULL, false); wp_enqueue_script('bootstrap-main', plugin_dir_url(__FILE__) . 'js/bootstrap.min.js', array('jquery'), NULL, false); wp_enqueue_script('rjs-core-js', plugin_dir_url(__FILE__) . 'js/rjs.jquery.fn.js', array('jquery'), NULL, false); wp_enqueue_script('bootstrap-angularjs', plugin_dir_url(__FILE__) . 'js/ui-bootstrap-angular.min.js', array('bootstrap-main'), NULL, false); // Angular Factories /*wp_enqueue_script( 'angular-factories', plugin_dir_url( __FILE__ ). 'js/angular-factories.js', array('angular-app'), NULL, false );*/ // Angular Directives /*wp_enqueue_script( 'angular-post-directives', plugin_dir_url( __FILE__ ). 'js/angular-posts-directives.js', array('angular-factories'), NULL, false );*/ // Template Directory $template_directory = array('post_load' => plugin_dir_url(__FILE__) . 'partials/post-load.html', 'post_truck' => plugin_dir_url(__FILE__) . 'partials/post-truck.html', 'post_archive' => plugin_dir_url(__FILE__) . 'partials/archive-load.html', 'favorite_postings' => plugin_dir_url(__FILE__) . 'partials/favorite-postings.html', 'archive_truck' => plugin_dir_url(__FILE__) . 'partials/archive-truck.html', 'rjs_pagination' => plugin_dir_url(__FILE__) . 'partials/rjs.pagination.html'); // Localize Variables wp_localize_script('angular-core', 'wfcLocalized', array('site' => get_bloginfo('wpurl'), 'base' => json_url(), 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('wp_json'), 'template_directory' => $template_directory, 'today_date' => date("Y-m-d", strtotime("today midnight")), 'plugin_path' => plugin_dir_url(__FILE__))); }
function angularScripts() { // Angular Core wp_enqueue_script('angular-core', plugin_dir_url(__FILE__) . 'js/angular.min.js', array('jquery'), null, false); wp_enqueue_script('angular-app', plugin_dir_url(__FILE__) . 'js/angular-app.js', array('jquery'), null, false); // Angular Factories wp_enqueue_script('angular-factories', plugin_dir_url(__FILE__) . 'js/angular-factories.js', array('angular-app'), null, false); // Angular Directives wp_enqueue_script('angular-post-directives', plugin_dir_url(__FILE__) . 'js/angular-posts-directives.js', array('angular-factories'), null, false); // Template Directory $template_directory = array('list_detail' => plugin_dir_url(__FILE__) . 'angularjs-templates/list-detail.html', 'single_detail' => plugin_dir_url(__FILE__) . 'angularjs-templates/single-detail.html', 'new_post' => plugin_dir_url(__FILE__) . 'angularjs-templates/new-post.html', 'post_content' => plugin_dir_url(__FILE__) . 'angularjs-templates/post-content.html'); // TEMPLATE OVERRIDES if (file_exists(get_template_directory() . '/angularjs-templates/list-detail.html')) { $template_directory['list_detail'] = get_bloginfo('template_directory') . '/angularjs-templates/list-detail.html'; } if (file_exists(get_template_directory() . '/angularjs-templates/single-detail.html')) { $template_directory['list_detail'] = get_bloginfo('template_directory') . '/angularjs-templates/single-detail.html'; } if (file_exists(get_template_directory() . '/angularjs-templates/new-post.html')) { $template_directory['new_post'] = get_bloginfo('template_directory') . '/angularjs-templates/new-post.html'; } if (file_exists(get_template_directory() . '/angularjs-templates/post-content.html')) { $template_directory['post_content'] = get_bloginfo('template_directory') . '/angularjs-templates/post-content.html'; } // Localize Variables wp_localize_script('angular-core', 'wpAngularVars', array('site' => get_bloginfo('wpurl'), 'base' => json_url(), 'nonce' => wp_create_nonce('wp_json'), 'template_directory' => $template_directory)); }
/** * Prepare post data * * @param array $post The unprepared post data * @param array $fields The subset of post type fields to return * @return array The prepared post data */ protected function prepare_post($post, $context = 'view') { $_post = parent::prepare_post($post, $context); // Override entity meta keys with the correct links $_post['meta']['links']['self'] = json_url($this->base . '/' . get_page_uri($post['ID'])); if (!empty($post['post_parent'])) { $_post['meta']['links']['up'] = json_url($this->base . '/' . get_page_uri((int) $post['post_parent'])); } return apply_filters('json_prepare_page', $_post, $post, $context); }
/** * Get all registered menus. * * @return WP_Error|WP_JSON_ResponseInterface */ public function get_menus() { $menus = get_registered_nav_menus(); if ($menus) { $response = json_ensure_response($menus); $response->set_status(201); $response->header('Location', json_url('jpwp/menus/')); return $response; } else { return new WP_Error('jwp_api_error' . __FUNCTION__, __('Menus could not be returned.', 'jpwp-api')); } }
/** * Add meta to a post. * * Ensures that the correct location header is sent with the response. * * @param int $id Post ID * @param array $data { * @type string|null $key Meta key * @type string|null $key Meta value * } * @return bool|WP_Error */ public function add_meta($id, $data) { $response = parent::add_meta($id, $data); if (is_wp_error($response)) { return $response; } $data = (object) $response->get_data(); $response = new WP_JSON_Response(); $response->header('Location', json_url('/posts/' . $id . '/meta/' . $data->ID)); $response->set_data($data); $response = json_ensure_response($response); return $response; }
function my_scripts() { wp_register_script('angularjs', get_template_directory_uri() . '/bower_components/angular/angular.min.js', array(), null, false); wp_register_script('angular-ui-router', get_template_directory_uri() . '/bower_components/angular-ui-router/release/angular-ui-router.min.js', array(), null, false); wp_register_script('angular-sanitize', get_template_directory_uri() . '/bower_components/angular-sanitize/angular-sanitize.min.js', array(), null, false); wp_register_script('angular-smooth-scroll', get_template_directory_uri() . '/bower_components/ngSmoothScroll/angular-smooth-scroll.min.js', array(), null, false); wp_register_script('angular-animate', get_template_directory_uri() . '/bower_components/angular-animate/angular-animate.min.js', array(), null, false); wp_register_script('angular-spinkit', get_template_directory_uri() . '/bower_components/angular-spinkit/build/angular-spinkit.min.js', array(), null, false); wp_register_script('angular-strap', get_template_directory_uri() . '/bower_components/angular-strap/dist/angular-strap.min.js', array(), null, false); wp_register_script('angular-strap-tpl', get_template_directory_uri() . '/bower_components/lodash/lodash.min.js', array(), null, false); wp_register_script('lodash', get_template_directory_uri() . '/bower_components/angular-strap/dist/angular-strap.tpl.min.js', array(), null, false); wp_register_script('reward-style', 'http://widgets.rewardstyle.com/js/shopthepost.js', array(), null, false); wp_enqueue_script('my-scripts', get_template_directory_uri() . '/js/app.js', array('angularjs', 'angular-ui-router', 'angular-sanitize', 'angular-smooth-scroll', 'angular-animate', 'angular-spinkit', 'angular-strap', 'angular-strap-tpl', 'lodash', 'reward-style')); wp_enqueue_style('flaticon.css', get_template_directory_uri() . '/assets/icons/flaticon.css'); wp_enqueue_style('angular-motion.css', get_template_directory_uri() . '/bower_components/angular-motion/dist/angular-motion.min.css'); wp_enqueue_style('angular-spinkit.css', get_template_directory_uri() . '/bower_components/angular-spinkit/build/angular-spinkit.min.css'); wp_enqueue_style('style.css', get_stylesheet_uri()); wp_localize_script('my-scripts', 'WPAPI', array('apiUrl' => json_url(), 'apiNonce' => wp_create_nonce('wp_json'))); }
function baldrick_pods_endpoint() { if (defined('PODS_VERSION') && defined('PODS_JSON_API_VERSION')) { //add the basic pods endpoints $endpoints['pods'] = json_url('pods'); $endpoints['pods-api'] = json_url('pods-api'); //get name of all registered Pods $pods = pods_api()->load_pods(array('names' => true)); //add end point foreach if there are registered Pods if (is_array($pods) && !empty($pods)) { $endpoint_types = array('pods', 'pods_api'); foreach ($pods as $pod) { foreach ($endpoint_types as $type) { $endpoints = array_merge($endpoints, array("{$type}/{$pod}" => json_url("{$type}/{$pod}"))); } } } return $endpoints; } }
function MyScripts() { $query = new WP_Query(); $JSON = $query->get_posts(); //MODERNIZR wp_enqueue_script('modernizr', get_bloginfo('template_directory') . '/js/modernizr-1.5.min.js', array('jquery'), null, false); wp_enqueue_script('modernizr'); //LESS CSS JS wp_enqueue_script('less', get_bloginfo('template_directory') . '/js/less-1.3.1.min.js', array('jquery'), null, false); wp_enqueue_script('less'); //TINYMCE wp_enqueue_script('tinymce', get_bloginfo('template_directory') . '/js/tinymce.min.js', array('jquery'), null, false); wp_enqueue_script('tinymce-second', get_bloginfo('template_directory') . '/js/jquery.tinymce.min.js', array('tinymce'), null, false); //ANGULAR wp_enqueue_script('angular-core', '//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.js', array('jquery'), null, false); wp_enqueue_script('angular-route', '//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-route.min.js', array('angular-core'), null, false); wp_enqueue_script('angular-resource', '//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-resource.min.js', array('angular-route'), null, false); wp_enqueue_script('angular-app', get_bloginfo('template_directory') . '/js/angular-app.js', array('angular-core'), null, false); //ANGULAR SMART NAV wp_enqueue_script('angular-route', get_bloginfo('template_directory') . '/js/angular-route.js', array('angular-core'), null, false); //ANGULAR UI wp_enqueue_script('angular-ie', get_bloginfo('template_directory') . '/js/angular-ui-ieshiv.min.js', array('angular-core'), null, false); wp_enqueue_script('angular-tinymce', get_bloginfo('template_directory') . '/js/ui-tinymce.js', array('angular-core'), null, false); wp_enqueue_style('angular-ui-css', get_bloginfo('template_directory') . '/js/angular-ui.css', false, '1.0', 'all'); wp_enqueue_script('angular-bs-ui', get_bloginfo('template_directory') . '/js/ui-bootstrap-tpls-0.10.0.min.js', array('angular-core'), null, false); //BOOTSTRAP wp_enqueue_style('bootstrap-core', get_bloginfo('template_directory') . '/css/bootstrap.min.css', false, '1.0', 'all'); wp_enqueue_script('boostrap-js', get_bloginfo('template_directory') . '/js/bootstrap.min.js', array('jquery'), null, false); //LOCALIZE wp_localize_script('angular-core', 'MyAjax', array('ajaxurl' => admin_url('admin-ajax.php'), 'resturl' => get_bloginfo('wpurl') . '/wp-json')); wp_localize_script('angular-core', 'Directory', array('url' => get_bloginfo('template_directory'), 'site' => get_bloginfo('wpurl'))); wp_localize_script('angular-core', 'wpApiOptions', array('base' => json_url(), 'nonce' => wp_create_nonce('wp_json'))); // LESS CSS wp_enqueue_style('less-css', get_bloginfo('template_directory') . '/more-style.less', false, '1.0', 'all'); wp_enqueue_style('less-css'); wp_enqueue_style('less-css-css', get_bloginfo('template_directory') . '/more-style.css', false, '1.0', 'all'); // INCLUDE WHEN READY: wp_enqueue_style('less-css-css'); //TINY MCE wp_enqueue_script('tiny_mce'); }
public function tax_query($data) { $allowed = array('post_type', 'tax_query'); foreach ($data as $key => $value) { if (!in_array($key, $allowed)) { unset($data[$key]); } } if (!is_array($data) || empty($data) || !isset($data['tax_query'])) { return new WP_Error('jp_api_tax_query', __('Invalid tax query.'), array('status' => 500)); } $post_query = new WP_Query(); $posts_list = $post_query->query($data); $response = new WP_JSON_Response(); $response->query_navigation_headers($post_query); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (json_check_post_permission($post, 'read')) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, 'view'); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
function angularScripts() { // Angular Core wp_enqueue_script('angular-core', plugin_dir_url(__FILE__) . 'js/angular.min.js', array('jquery'), null, false); wp_enqueue_script('angular-sanitize', plugin_dir_url(__FILE__) . 'js/angular-sanitize.min.js', array('jquery'), null, false); wp_enqueue_script('html-janitor', plugin_dir_url(__FILE__) . 'js/html-janitor.js', array('jquery'), null, false); wp_enqueue_script('angular-app', plugin_dir_url(__FILE__) . 'js/angular-app.js', array('html-janitor'), null, false); // Angular Factories wp_enqueue_script('angular-factories', plugin_dir_url(__FILE__) . 'js/angular-factories.js', array('angular-app'), null, false); // Angular Directives wp_enqueue_script('angular-post-directives', plugin_dir_url(__FILE__) . 'js/angular-posts-directives.js', array('angular-factories'), null, false); // Template Directory $template_directory = array('list_detail' => plugin_dir_url(__FILE__) . 'angularjs-templates/list-detail.html', 'single_detail' => plugin_dir_url(__FILE__) . 'angularjs-templates/single-detail.html', 'new_post' => plugin_dir_url(__FILE__) . 'angularjs-templates/new-post.html', 'post_content' => plugin_dir_url(__FILE__) . 'angularjs-templates/post-content.html'); // TEMPLATE OVERRIDES if (file_exists(get_stylesheet_directory() . '/angularjs-templates/list-detail.html')) { $template_directory['list_detail'] = get_stylesheet_directory_uri() . '/angularjs-templates/list-detail.html'; } if (file_exists(get_stylesheet_directory() . '/angularjs-templates/single-detail.html')) { $template_directory['single_detail'] = get_stylesheet_directory_uri() . '/angularjs-templates/single-detail.html'; } if (file_exists(get_stylesheet_directory() . '/angularjs-templates/new-post.html')) { $template_directory['new_post'] = get_stylesheet_directory_uri() . '/angularjs-templates/new-post.html'; } if (file_exists(get_stylesheet_directory() . '/angularjs-templates/post-content.html')) { $template_directory['post_content'] = get_stylesheet_directory_uri() . '/angularjs-templates/post-content.html'; } $angularjs_for_wp_localize = array('site' => get_bloginfo('wpurl'), 'nonce' => wp_create_nonce('wp_json'), 'template_directory' => $template_directory); if (function_exists('json_url')) { $angularjs_for_wp_localize['base'] = json_url(); } if (function_exists('rest_get_url_prefix')) { $angularjs_for_wp_localize['base'] = get_bloginfo('wpurl') . '/' . rest_get_url_prefix() . '/wp/v2'; } // Localize Variables wp_localize_script('angular-core', 'wpAngularVars', $angularjs_for_wp_localize); }
function __social_scripts_enqueue() { $dev = false; if ($dev) { wp_enqueue_script('hello-js', API_SOCIAL_URL . '/assets/js/hello.all.js', array('jquery'), API_SOCIAL_LOGIN_VERSION, false); wp_enqueue_script('social-js', API_SOCIAL_URL . '/assets/js/social.js', array('hello-js'), API_SOCIAL_LOGIN_VERSION, false); } else { wp_enqueue_script('hello-js', API_SOCIAL_URL . '/build/js/hello.all.min.js', array('jquery'), API_SOCIAL_LOGIN_VERSION, false); wp_enqueue_script('social-js', API_SOCIAL_URL . '/build/js/social.min.js', array('hello-js'), API_SOCIAL_LOGIN_VERSION, false); } $app_data = array('api_url' => get_bloginfo('wpurl') . '/wp-json'); if (function_exists('rest_get_url_prefix')) { $app_data['api_url'] = get_bloginfo('wpurl') . '/' . rest_get_url_prefix() . '/social_login'; } elseif (function_exists('json_url')) { $app_data['api_url'] = json_url(); } $social_app = $this->__get_social_apps(); foreach ($social_app as $key => $value) { if (!empty($value)) { $app_data[$key] = $value; } } wp_localize_script('social-js', 'socialLogin', $app_data); }
/** * Enqueue script & localize data */ function angular_wp_api_scripts() { // Leave if WP-API is not activated if (!defined('JSON_API_VERSION')) { return; } // Leave if not specifically requested from the theme or a plugin if (!($config = get_theme_support('angular-wp-api'))) { return; } // Array of dependencies $script_dependencies = null; // Data for localization $script_data = null; // Script dependency from theme support if (isset($config[0])) { $script_dependencies = $config[0]; } // Script data from theme support if (isset($config[1])) { $script_data = $config[1]; } // Data for localization $script_data['base'] = json_url(); $script_data['nonce'] = wp_create_nonce('wp_json'); // Provide user id if logged in if (is_user_logged_in()) { $script_data['user_id'] = get_current_user_id(); } else { $script_data['user_id'] = 0; } // Enqueue the script after dependency, in the footer wp_enqueue_script('angular-wp-api', plugins_url('angular-wp-api.min.js', __FILE__), apply_filters('angular_wp_api_script_dependencies', $script_dependencies), '', true); // Localize filterable data for script wp_localize_script('angular-wp-api', 'wpAPIData', apply_filters('angular_wp_api_local_data', $script_data)); }
protected function prepare_user($user, $context = 'view') { $user_fields = array('ID' => $user->ID, 'username' => $user->user_login, 'name' => $user->display_name, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'nickname' => $user->nickname, 'slug' => $user->user_nicename, 'URL' => $user->user_url, 'avatar' => json_get_avatar_url($user->user_email), 'description' => $user->description); $user_fields['registered'] = date('c', strtotime($user->user_registered)); if ($context === 'view' || $context === 'edit') { $user_fields['roles'] = $user->roles; $user_fields['capabilities'] = $user->allcaps; $user_fields['email'] = false; } if ($context === 'edit') { // The user's specific caps should only be needed if you're editing // the user, as allcaps should handle most uses $user_fields['email'] = $user->user_email; $user_fields['extra_capabilities'] = $user->caps; } $user_fields['meta'] = array('links' => array('self' => json_url('/users/' . $user->ID), 'archives' => json_url('/users/' . $user->ID . '/posts'))); return apply_filters('json_prepare_user', $user_fields, $user, $context); }
/** * Retrieve posts. * * @since 3.4.0 * * The optional $filter parameter modifies the query used to retrieve posts. * Accepted keys are 'post_type', 'post_status', 'number', 'offset', * 'orderby', and 'order'. * * The optional $fields parameter specifies what fields will be included * in the response array. * * @uses wp_get_recent_posts() * @see WP_JSON_Posts::get_post() for more on $fields * @see get_posts() for more on $filter values * * @param array $filter Parameters to pass through to `WP_Query` * @param string $context * @param string|array $type Post type slug, or array of slugs * @param int $page Page number (1-indexed) * @return stdClass[] Collection of Post entities */ public function list_posts($filter = array(), $context = 'view', $type = 'posts', $page = 1) { $query = array(); //Block screen ids $internal_menu_screens = array("100", "1100", "1200", "1300", "1400", "1500", "2100", "5200"); //swap type to screen id and screen_id to type if ($type != 'posts') { $screens = $this->get_screens(); $screen_id = $type; $type = $screens[$type]; } else { $this->set_status(404); return array('message' => __('Invalid Type Param.')); } //Redirect to other function and return weather api service if ($type == 'weather-api') { $screen_attrs = $this->get_screen_attrs(); $struct = $this->get_weather($type); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'result' => $struct); return $data; } //Redirect to other function and return dashboard and screen service if (in_array($screen_id, $internal_menu_screens)) { /* if ($parent == '9301') { return $this->get_screens_data(); } */ $screen_attrs = $this->get_screen_attrs(); $screen_image = ""; //$page_id = $this->get_ID_by_page_slug('dashboard'); $page_id = $this->get_post_id_by_screen_id($screen_id); $screen_image_data = get_field('screen_image', $page_id); $screen_image = empty($screen_image_data['url']) || $screen_image_data['url'] == 'null' || $screen_image_data['url'] == null ? "" : $screen_image_data['url']; // if($screen_id=='2100' || $screen_id=='1500') { // return array($page_id); // } $struct = $this->get_screen_content($page_id); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => $screen_image, 'result' => $struct); return $data; } //Redirect to other function and return in room ordering service if ($type == 'in-room-ordering') { $screen_attrs = $this->get_screen_attrs(); //$struct = $this->get_taxonomy_terms('food_category'); $struct = $this->get_menu_category('food_category'); $total_menu_count = !empty($struct['total_menu_count']) ? $struct['total_menu_count'] : 0; unset($struct['total_menu_count']); $data = array('count' => count($struct), 'total_menu_count' => $total_menu_count, 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'currency' => "\$", 'result' => $struct); return $data; } //Redirect to other function and return todays events service if ($type == 'todays-events') { $screen_attrs = $this->get_screen_attrs(); $struct = $this->get_list_events($type); $banner_data = $this->get_banner_image_by_slug('events'); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'banner_image' => $banner_data['image'], 'description' => $banner_data['description'], 'result' => $struct); return $data; } //Redirect to other function and return activity guide service if ($type == 'activity-guide') { $screen_attrs = $this->get_screen_attrs(); $struct = $this->get_list_activity_guide($type); $banner_data = $this->get_banner_image_by_slug('activity-guide'); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'banner_image' => $banner_data['image'], 'description' => $banner_data['description'], 'title' => 'Activities and Events', 'weekday_title' => 'Week day Activities', 'weekend_title' => 'Weekend Activities', 'result' => $struct); return $data; } // Validate post types and permissions $query['post_type'] = array(); foreach ((array) $type as $type_name) { $post_type = get_post_type_object($type_name); if (!(bool) $post_type || !$post_type->show_in_json) { return new WP_Error('json_invalid_post_type', sprintf(__('The post type "%s" is not valid'), $type_name), array('status' => 403)); } $query['post_type'][] = $post_type->name; } global $wp; // Allow the same as normal WP $valid_vars = apply_filters('query_vars', $wp->public_query_vars); // If the user has the correct permissions, also allow use of internal // query parameters, which are only undesirable on the frontend // // To disable anyway, use `add_filter('json_private_query_vars', '__return_empty_array');` if (current_user_can($post_type->cap->edit_posts)) { $private = apply_filters('json_private_query_vars', $wp->private_query_vars); $valid_vars = array_merge($valid_vars, $private); } // Define our own in addition to WP's normal vars $json_valid = array('posts_per_page'); $valid_vars = array_merge($valid_vars, $json_valid); // Filter and flip for querying $valid_vars = apply_filters('json_query_vars', $valid_vars); $valid_vars = array_flip($valid_vars); // Exclude the post_type query var to avoid dodging the permission // check above unset($valid_vars['post_type']); foreach ($valid_vars as $var => $index) { if (isset($filter[$var])) { $query[$var] = apply_filters('json_query_var-' . $var, $filter[$var]); } } // Special parameter handling $query['paged'] = absint($page); $query['posts_per_page'] = -1; $post_query = new WP_Query(); $posts_list = $post_query->query($query); $response = new WP_JSON_Response(); $response->query_navigation_headers($post_query); /* if (!$posts_list) { $response->set_data(array()); return $response; } */ // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (!$this->check_read_permission($post)) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, $context); if (is_wp_error($post_data)) { continue; } $post_data['tag_name'] = $post['post_name']; $post_data = $this->format_get_data($post_data, $post['ID']); $struct[] = $post_data; } $screen_attrs = $this->get_screen_attrs(); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'result' => $struct); $response->set_data($data); return $response; }
/** * Get the site index. * * This endpoint describes the capabilities of the site. * * @todo Should we generate text documentation too based on PHPDoc? * * @return array Index entity */ public function get_index() { // General site data $available = array('name' => get_option('blogname'), 'description' => get_option('blogdescription'), 'URL' => get_option('siteurl'), 'routes' => array(), 'authentication' => array(), 'meta' => array('links' => array('help' => 'https://github.com/WP-API/WP-API', 'profile' => 'https://raw.github.com/WP-API/WP-API/master/docs/schema.json'))); // Find the available routes foreach ($this->get_routes() as $route => $callbacks) { $data = array(); $route = preg_replace('#\\(\\?P(<\\w+?>).*?\\)#', '$1', $route); $methods = array(); foreach (self::$method_map as $name => $bitmask) { foreach ($callbacks as $callback) { // Skip to the next route if any callback is hidden if ($callback[1] & self::HIDDEN_ENDPOINT) { continue 3; } if ($callback[1] & $bitmask) { $data['supports'][] = $name; } if ($callback[1] & self::ACCEPT_JSON) { $data['accepts_json'] = true; } // For non-variable routes, generate links if (strpos($route, '<') === false) { $data['meta'] = array('self' => json_url($route)); } } } $available['routes'][$route] = apply_filters('json_endpoints_description', $data); } return apply_filters('json_index', $available); }
/** * Prepares post data for return in an XML-RPC object. * * @access protected * * @param array $post The unprepared post data * @param string $context The context for the prepared post. (view|view-revision|edit|embed|single-parent) * @return array The prepared post data */ protected function prepare_post($post, $context = 'view') { // Holds the data for this post. $_post = array('ID' => (int) $post['ID']); $post_type = get_post_type_object($post['post_type']); if (!json_check_post_permission($post, 'read')) { return new WP_Error('json_user_cannot_read', __('Sorry, you cannot read this post.'), array('status' => 401)); } $previous_post = null; if (!empty($GLOBALS['post'])) { $previous_post = $GLOBALS['post']; } $post_obj = get_post($post['ID']); // Don't allow unauthenticated users to read password-protected posts if (!empty($post['post_password'])) { if (!json_check_post_permission($post, 'edit')) { return new WP_Error('json_user_cannot_read', __('Sorry, you cannot read this post.'), array('status' => 403)); } // Fake the correct cookie to fool post_password_required(). // Without this, get_the_content() will give a password form. require_once ABSPATH . 'wp-includes/class-phpass.php'; $hasher = new PasswordHash(8, true); $value = $hasher->HashPassword($post['post_password']); $_COOKIE['wp-postpass_' . COOKIEHASH] = wp_slash($value); } $GLOBALS['post'] = $post_obj; setup_postdata($post_obj); // prepare common post fields $post_fields = array('title' => get_the_title($post['ID']), 'status' => $post['post_status'], 'type' => $post['post_type'], 'author' => (int) $post['post_author'], 'content' => apply_filters('the_content', $post['post_content']), 'parent' => (int) $post['post_parent'], 'link' => get_permalink($post['ID'])); $post_fields_extended = array('slug' => $post['post_name'], 'guid' => apply_filters('get_the_guid', $post['guid']), 'excerpt' => $this->prepare_excerpt($post['post_excerpt']), 'menu_order' => (int) $post['menu_order'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'sticky' => $post['post_type'] === 'post' && is_sticky($post['ID'])); $post_fields_raw = array('title_raw' => $post['post_title'], 'content_raw' => $post['post_content'], 'excerpt_raw' => $post['post_excerpt'], 'guid_raw' => $post['guid'], 'post_meta' => $this->handle_get_post_meta($post['ID'])); // Dates $timezone = json_get_timezone(); if ($post['post_date_gmt'] === '0000-00-00 00:00:00') { $post_fields['date'] = null; $post_fields_extended['date_tz'] = null; $post_fields_extended['date_gmt'] = null; } else { $post_date = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $post['post_date'], $timezone); $post_fields['date'] = json_mysql_to_rfc3339($post['post_date']); $post_fields_extended['date_tz'] = $post_date->format('e'); $post_fields_extended['date_gmt'] = json_mysql_to_rfc3339($post['post_date_gmt']); } if ($post['post_modified_gmt'] === '0000-00-00 00:00:00') { $post_fields['modified'] = null; $post_fields_extended['modified_tz'] = null; $post_fields_extended['modified_gmt'] = null; } else { $modified_date = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $post['post_modified'], $timezone); $post_fields['modified'] = json_mysql_to_rfc3339($post['post_modified']); $post_fields_extended['modified_tz'] = $modified_date->format('e'); $post_fields_extended['modified_gmt'] = json_mysql_to_rfc3339($post['post_modified_gmt']); } // Authorized fields // TODO: Send `Vary: Authorization` to clarify that the data can be // changed by the user's auth status if (json_check_post_permission($post, 'edit')) { $post_fields_extended['password'] = $post['post_password']; } // Consider future posts as published if ($post_fields['status'] === 'future') { $post_fields['status'] = 'publish'; } // Fill in blank post format $post_fields['format'] = get_post_format($post['ID']); if (empty($post_fields['format'])) { $post_fields['format'] = 'standard'; } if (0 === $post['post_parent']) { $post_fields['parent'] = null; } if (('view' === $context || 'view-revision' == $context) && 0 !== $post['post_parent']) { // Avoid nesting too deeply // This gives post + post-extended + meta for the main post, // post + meta for the parent and just meta for the grandparent $parent = get_post($post['post_parent'], ARRAY_A); $post_fields['parent'] = $this->prepare_post($parent, 'embed'); } // Merge requested $post_fields fields into $_post $_post = array_merge($_post, $post_fields); // Include extended fields. We might come back to this. $_post = array_merge($_post, $post_fields_extended); if ('edit' === $context) { if (json_check_post_permission($post, 'edit')) { $_post = array_merge($_post, $post_fields_raw); } else { $GLOBALS['post'] = $previous_post; if ($previous_post) { setup_postdata($previous_post); } return new WP_Error('json_cannot_edit', __('Sorry, you cannot edit this post'), array('status' => 403)); } } elseif ('view-revision' == $context) { if (json_check_post_permission($post, 'edit')) { $_post = array_merge($_post, $post_fields_raw); } else { $GLOBALS['post'] = $previous_post; if ($previous_post) { setup_postdata($previous_post); } return new WP_Error('json_cannot_view', __('Sorry, you cannot view this revision'), array('status' => 403)); } } // Entity meta $links = array('self' => json_url('/posts/' . $post['ID']), 'author' => json_url('/users/' . $post['post_author']), 'collection' => json_url('/posts')); if ('view-revision' != $context) { $links['replies'] = json_url('/posts/' . $post['ID'] . '/comments'); $links['version-history'] = json_url('/posts/' . $post['ID'] . '/revisions'); } $_post['meta'] = array('links' => $links); if (!empty($post['post_parent'])) { $_post['meta']['links']['up'] = json_url('/posts/' . (int) $post['post_parent']); } $GLOBALS['post'] = $previous_post; if ($previous_post) { setup_postdata($previous_post); } return apply_filters('json_prepare_post', $_post, $post, $context); }
public function add_meta($id, $data) { $id = (int) $id; if (empty($id)) { return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); } $post = get_post($id, ARRAY_A); if (empty($post['ID'])) { return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); } /* if ( ! $this->check_edit_permission( $post ) ) { return new WP_Error( 'json_cannot_edit', __( 'Sorry, you cannot edit this post' ), array( 'status' => 403 ) ); } */ if (!array_key_exists('key', $data)) { return new WP_Error('json_post_missing_key', __('Missing meta key.'), array('status' => 400)); } if (!array_key_exists('value', $data)) { return new WP_Error('json_post_missing_value', __('Missing meta value.'), array('status' => 400)); } if (empty($data['key'])) { return new WP_Error('json_meta_invalid_key', __('Invalid meta key.'), array('status' => 400)); } if (!$this->is_valid_meta_data($data['value'])) { // for now let's not allow updating of arrays, objects or serialized values. return new WP_Error('json_post_invalid_action', __('Invalid provided meta data for action.'), array('status' => 400)); } if (is_protected_meta($data['key'])) { return new WP_Error('json_meta_protected', sprintf(__('%s is marked as a protected field.'), $data['key']), array('status' => 403)); } $meta_key = wp_slash($data['key']); $value = wp_slash($data['value']); $result = add_post_meta($id, $meta_key, $value); if (!$result) { return new WP_Error('json_meta_could_not_add', __('Could not add post meta.'), array('status' => 400)); } $response = json_ensure_response($this->get_meta($id, $result)); if (is_wp_error($response)) { return $response; } $response->set_status(201); $response->header('Location', json_url('/posts/' . $id . '/meta/' . $result)); return $response; }
/** * Create a new user. * * @param $data * @return mixed */ public function create_user($data) { if (!current_user_can('create_users')) { return new WP_Error('json_cannot_create', __('Sorry, you are not allowed to create users.'), array('status' => 403)); } if (!empty($data['ID'])) { return new WP_Error('json_user_exists', __('Cannot create existing user.'), array('status' => 400)); } $user_id = $this->insert_user($data); if (is_wp_error($user_id)) { return $user_id; } $response = $this->get_user($user_id); if (!$response instanceof WP_JSON_ResponseInterface) { $response = new WP_JSON_Response($response); } $response->set_status(201); $response->header('Location', json_url('/users/' . $user_id)); return $response; }
/** * Edit a form given an ID. This is an API endpoint. * * @param int $id * @param array $data * @param array $_headers * @since 6.0 * @return int|WP_Error|WP_JSON_ResponseInterface */ function edit_form($id, $data, $_headers = array()) { $id = (int) $id; if (empty($id)) { return new WP_Error('json_invalid_id_ccf_form', esc_html__('Invalid form ID.', 'custom-contact-forms'), array('status' => 404)); } $form = get_post($id, ARRAY_A); if (empty($form['ID'])) { return new WP_Error('json_invalid_ccf_form', esc_html__('Invalid form.', 'custom-contact-forms'), array('status' => 404)); } // @todo: remove hack. Needed for broken API if (isset($data['author'])) { unset($data['author']); } // @todo: remove hack. Needed for broken API if (isset($data['date'])) { unset($data['date']); } // @todo: remove hack. Needed for broken API if (isset($data['date_gmt'])) { unset($data['date_gmt']); } $result = $this->insert_post($data); if ($result instanceof WP_Error) { return $result; } if (isset($data['fields'])) { if (empty($data['fields'])) { $data['fields'] = array(); } $this->create_and_map_fields($data['fields'], $result); } if (isset($data['buttonText'])) { update_post_meta($result, 'ccf_form_buttonText', sanitize_text_field($data['buttonText'])); } if (isset($data['description'])) { update_post_meta($result, 'ccf_form_description', sanitize_text_field($data['description'])); } if (isset($data['completionActionType'])) { update_post_meta($result, 'ccf_form_completion_action_type', sanitize_text_field($data['completionActionType'])); } if (isset($data['completionMessage'])) { update_post_meta($result, 'ccf_form_completion_message', sanitize_text_field($data['completionMessage'])); } if (isset($data['pause'])) { update_post_meta($result, 'ccf_form_pause', (bool) $data['pause']); } if (isset($data['pauseMessage'])) { update_post_meta($result, 'ccf_form_pause_message', sanitize_text_field($data['pauseMessage'])); } if (isset($data['completionRedirectUrl'])) { update_post_meta($result, 'ccf_form_completion_redirect_url', esc_url_raw($data['completionRedirectUrl'])); } if (isset($data['sendEmailNotifications'])) { update_post_meta($result, 'ccf_form_send_email_notifications', (bool) $data['sendEmailNotifications']); } if (isset($data['emailNotificationAddresses'])) { update_post_meta($result, 'ccf_form_email_notification_addresses', sanitize_text_field($data['emailNotificationAddresses'])); } if (isset($data['emailNotificationFromType'])) { update_post_meta($result, 'ccf_form_email_notification_from_type', sanitize_text_field($data['emailNotificationFromType'])); } if (isset($data['emailNotificationFromAddress'])) { update_post_meta($result, 'ccf_form_email_notification_from_address', sanitize_text_field($data['emailNotificationFromAddress'])); } if (isset($data['emailNotificationFromField'])) { update_post_meta($result, 'ccf_form_email_notification_from_field', sanitize_text_field($data['emailNotificationFromField'])); } if (isset($data['emailNotificationFromNameType'])) { update_post_meta($result, 'ccf_form_email_notification_from_name_type', sanitize_text_field($data['emailNotificationFromNameType'])); } if (isset($data['emailNotificationFromName'])) { update_post_meta($result, 'ccf_form_email_notification_from_name', sanitize_text_field($data['emailNotificationFromName'])); } if (isset($data['emailNotificationFromNameField'])) { update_post_meta($result, 'ccf_form_email_notification_from_name_field', sanitize_text_field($data['emailNotificationFromNameField'])); } if (isset($data['emailNotificationSubjectType'])) { update_post_meta($result, 'ccf_form_email_notification_subject_type', sanitize_text_field($data['emailNotificationSubjectType'])); } if (isset($data['emailNotificationSubject'])) { update_post_meta($result, 'ccf_form_email_notification_subject', sanitize_text_field($data['emailNotificationSubject'])); } if (isset($data['emailNotificationSubjectField'])) { update_post_meta($result, 'ccf_form_email_notification_subject_field', sanitize_text_field($data['emailNotificationSubjectField'])); } $response = json_ensure_response($this->get_post($result)); $response->set_status(201); $response->header('Location', json_url('/ccf/forms/' . $result)); return $response; }
public function add_meta($id, $data) { $id = (int) $id; if (empty($id)) { $this->set_status(404); return array('message' => __('Invalid post ID.')); } $post = get_post($id, ARRAY_A); if (empty($post['ID'])) { $this->set_status(404); return array('message' => __('Invalid post ID.')); } if (!array_key_exists('key', $data)) { $this->set_status(400); return array('message' => __('Missing meta key.')); } if (!array_key_exists('value', $data)) { $this->set_status(400); return array('message' => __('Missing meta value.')); } if (empty($data['key'])) { $this->set_status(400); return array('message' => __('Invalid meta key.')); } if (!$this->is_valid_meta_data($data['value'])) { // for now let's not allow updating of arrays, objects or serialized values. $this->set_status(400); return array('message' => __('Invalid provided meta data for action.')); } if (is_protected_meta($data['key'])) { $this->set_status(403); return array('message' => __('Forbidden Error.')); } $meta_key = wp_slash($data['key']); $value = wp_slash($data['value']); $result = add_post_meta($id, $meta_key, $value); if (!$result) { $this->set_status(400); return array('message' => __('Could not add post meta.')); } $response = json_ensure_response($this->get_meta($id, $result)); if (is_wp_error($response)) { return $response; } $response->set_status(201); $response->header('Location', json_url('/posts/' . $id . '/meta/' . $result)); return $response; }
protected function prepare_post($post, $context = 'view') { // holds the data for this post. built up based on $fields $_post = array('ID' => (int) $post['ID']); $post_type = get_post_type_object($post['post_type']); //if ( ! $this->check_read_permission( $post ) ) //return new WP_Error( 'json_user_cannot_read', __( 'Sorry, you cannot read this post.' ), array( 'status' => 401 ) ); // prepare common post fields $post_fields = array('title' => get_the_title($post['ID']), 'status' => $post['post_status'], 'type' => $post['post_type'], 'author' => (int) $post['post_author'], 'content' => apply_filters('the_content', $post['post_content']), 'parent' => (int) $post['post_parent'], 'link' => get_permalink($post['ID'])); $post_fields_extended = array('slug' => $post['post_name'], 'guid' => apply_filters('get_the_guid', $post['guid']), 'excerpt' => $this->prepare_excerpt($post['post_excerpt']), 'menu_order' => (int) $post['menu_order'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'sticky' => $post['post_type'] === 'post' && is_sticky($post['ID'])); $post_fields_raw = array('title_raw' => $post['post_title'], 'content_raw' => $post['post_content'], 'guid_raw' => $post['guid'], 'post_meta' => $this->get_all_meta($post['ID'])); // Dates $timezone = $this->server->get_timezone(); $date = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $post['post_date'], $timezone); $post_fields['date'] = $date->format('c'); $post_fields_extended['date_tz'] = $date->format('e'); $post_fields_extended['date_gmt'] = date('c', strtotime($post['post_date_gmt'])); $modified = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $post['post_modified'], $timezone); $post_fields['modified'] = $modified->format('c'); $post_fields_extended['modified_tz'] = $modified->format('e'); $post_fields_extended['modified_gmt'] = date('c', strtotime($post['post_modified_gmt'])); // Authorized fields // TODO: Send `Vary: Authorization` to clarify that the data can be // changed by the user's auth status //if ( current_user_can( $post_type->cap->edit_post, $post['ID'] ) ) { // $post_fields_extended['password'] = $post['post_password']; //} // Consider future posts as published if ($post_fields['status'] === 'future') { $post_fields['status'] = 'publish'; } // Fill in blank post format $post_fields['format'] = get_post_format($post['ID']); if (empty($post_fields['format'])) { $post_fields['format'] = 'standard'; } if (('view' === $context || 'view-revision' == $context) && 0 !== $post['post_parent']) { // Avoid nesting too deeply // This gives post + post-extended + meta for the main post, // post + meta for the parent and just meta for the grandparent $parent = get_post($post['post_parent'], ARRAY_A); $post_fields['parent'] = $this->prepare_post($parent, 'embed'); } // Merge requested $post_fields fields into $_post $_post = array_merge($_post, $post_fields); // Include extended fields. We might come back to this. $_post = array_merge($_post, $post_fields_extended); if ('edit' === $context) { //if ( current_user_can( $post_type->cap->edit_post, $post['ID'] ) ) { if (is_wp_error($post_fields_raw['post_meta'])) { return $post_fields_raw['post_meta']; } $_post = array_merge($_post, $post_fields_raw); //} else { // return new WP_Error( 'json_cannot_edit', __( 'Sorry, you cannot edit this post' ), array( 'status' => 403 ) ); //} } elseif ('view-revision' == $context) { //if ( current_user_can( $post_type->cap->edit_post, $post['ID'] ) ) { $_post = array_merge($_post, $post_fields_raw); //} else { // return new WP_Error( 'json_cannot_view', __( 'Sorry, you cannot view this revision' ), array( 'status' => 403 ) ); //} } // Entity meta $links = array('self' => json_url('/posts/' . $post['ID']), 'author' => json_url('/users/' . $post['post_author']), 'collection' => json_url('/posts')); if ('view-revision' != $context) { $links['replies'] = json_url('/posts/' . $post['ID'] . '/comments'); $links['version-history'] = json_url('/posts/' . $post['ID'] . '/revisions'); } $_post['meta'] = array('links' => $links); if (!empty($post['post_parent'])) { $_post['meta']['links']['up'] = json_url('/posts/' . (int) $post['post_parent']); } return apply_filters('json_prepare_post', $_post, $post, $context); }
/** * Filter the post type archive link * * @param array $data Post type data * @param stdClass $type Internal post type data * @return array Filtered data */ public function type_archive_link($data, $type) { if ($type->name !== $this->type) { return $data; } $data['meta']['links']['archives'] = json_url($this->base); return $data; }
/** * Filter the post type archive link * * @param array $data Post type data * @param stdClass $type Internal post type data * @return array Filtered data */ public function type_archive_link($data, $type) { if ($type->name !== 'attachment') { return $data; } $data['meta']['links']['archives'] = json_url('/media'); return $data; }
?> "> </p> </form> <?php if ($deploy_active) { ?> <div id="current-keys"> <p> <?php _e(sprintf('Public Key: %1s', $public_remote), 'pods-deploy'); ?> </p> <p> <?php _e(sprintf('Private Key: %1s', $private_remote), 'pods-deploy'); ?> </p> <p> <?php _e(sprintf("This root URL for this site's API is: %1s", json_url()), 'pods-deploy'); ?> </p> </div> <?php } ?> </div>
public function import_commits($p_repo, $p_commit_ids, $p_branch = '') { static $s_parents = array(); static $s_counter = 0; $t_repoid = $p_repo->info['hub_repoid']; if (is_array($p_commit_ids)) { $s_parents = array_merge($s_parents, $p_commit_ids); } else { $s_parents[] = $p_commit_ids; } $t_changesets = array(); while (count($s_parents) > 0 && $s_counter < 200) { $t_commit_id = array_shift($s_parents); echo "Retrieving {$t_commit_id} ... <br/>"; $t_uri = $this->api_uri($p_repo, "projects/{$t_repoid}/repository/commits/{$t_commit_id}"); $t_member = null; $t_json = json_url($t_uri, $t_member); if (false === $t_json || is_null($t_json)) { # Some error occured retrieving the commit echo "failed.\n"; continue; } else { if (!property_exists($t_json, 'id')) { echo "failed ({$t_json->message}).\n"; continue; } } list($t_changeset, $t_commit_parents) = $this->json_commit_changeset($p_repo, $t_json, $p_branch); if ($t_changeset) { $t_changesets[] = $t_changeset; } $s_parents = array_merge($s_parents, $t_commit_parents); } $s_counter = 0; return $t_changesets; }
/** * Prepare post data * * @param array $post The unprepared post data * @param string $context The context for the prepared post. (view|view-revision|edit|embed|single-parent) * * @return array The prepared post data */ protected function prepare_post($post, $context = 'view') { // Holds the data for this post. $_post = array('ID' => absint($post['ID'])); if (!$this->check_read_permission($post)) { return new WP_Error('json_user_cannot_read', __('Sorry, you cannot read this post.'), array('status' => 401)); } $post_obj = get_post($post['ID']); $GLOBALS['post'] = $post_obj; setup_postdata($post_obj); // Prepare common post fields $post_fields = array('title' => get_the_title($post['ID']), 'type' => $post['post_type'], 'link' => get_permalink($post['ID'])); $post_fields_extended = array('slug' => $post['post_name'], 'avatar' => Helper::get_avatar_url($post['ID'], 512), 'excerpt' => $this->prepare_excerpt($post['post_excerpt']), 'content' => apply_filters('the_content', $post['post_content']), 'byline' => esc_html(get_post_meta($post['ID'], 'byline', true))); // Get connected company $companies = get_posts(array('connected_type' => 'hiring', 'connected_items' => $post, 'posts_per_page' => -1, 'suppress_filters' => false)); $post_fields_extended['company'] = array('ID' => absint($companies[0]->ID), 'title' => get_the_title($companies[0]->ID), 'link' => get_permalink($companies[0]->ID), 'slug' => $companies[0]->post_name, 'excerpt' => $this->prepare_excerpt($companies[0]->post_excerpt), 'meta' => array('self' => json_url('/talents/' . $companies[0]->ID), 'collection' => json_url('/talents'))); // Dates if ('0000-00-00 00:00:00' === $post['post_date_gmt']) { $post_fields['date'] = null; $post_fields_extended['date_gmt'] = null; } else { $post_fields['date'] = json_mysql_to_rfc3339($post['post_date']); $post_fields_extended['date_gmt'] = json_mysql_to_rfc3339($post['post_date_gmt']); } if ('0000-00-00 00:00:00' === $post['post_modified_gmt']) { $post_fields['modified'] = null; $post_fields_extended['modified_gmt'] = null; } else { $post_fields['modified'] = json_mysql_to_rfc3339($post['post_modified']); $post_fields_extended['modified_gmt'] = json_mysql_to_rfc3339($post['post_modified_gmt']); } // Merge requested $post_fields fields into $_post $_post = array_merge($_post, $post_fields); // Include extended fields. We might come back to this. $_post = array_merge($_post, $post_fields_extended); // Entity meta $links = array('self' => json_url('/jobs/' . $post['ID']), 'collection' => json_url('/jobs')); $_post['meta'] = array('links' => $links); return apply_filters('json_prepare_talent', $_post, $post, $context); }
/** * Prepare term data for serialization * * @param array|object $term The unprepared term data * @return array The prepared term data */ protected function prepare_taxonomy_term($term, $context = 'view') { $base_url = '/taxonomies/' . $term->taxonomy . '/terms'; $data = array('ID' => (int) $term->term_taxonomy_id, 'name' => $term->name, 'slug' => $term->slug, 'description' => $term->description, 'taxonomy' => $term->taxonomy, 'parent' => (int) $term->parent, 'count' => (int) $term->count, 'link' => $this->get_posts_list_url_by_taxonomy($term), 'meta' => array('links' => array('collection' => json_url($base_url), 'self' => json_url($base_url . '/' . $term->term_id)))); if (!empty($data['parent']) && $context === 'view') { $data['parent'] = $this->get_taxonomy_term($term->taxonomy, $data['parent'], 'view-parent'); } elseif (empty($data['parent'])) { $data['parent'] = null; } return apply_filters('json_prepare_term', $data, $term, $context); }
/** * Prepares comment data for returning as a JSON response. * * @param stdClass $comment Comment object * @param array $requested_fields Fields to retrieve from the comment * @param string $context Where is the comment being loaded? * @return array Comment data for JSON serialization */ protected function prepare_comment($comment, $requested_fields = array('comment', 'meta'), $context = 'single') { $fields = array('ID' => (int) $comment->comment_ID, 'post' => (int) $comment->comment_post_ID); $post = (array) get_post($fields['post']); // Content $fields['content'] = apply_filters('comment_text', $comment->comment_content, $comment); // $fields['content_raw'] = $comment->comment_content; // Status switch ($comment->comment_approved) { case 'hold': case '0': $fields['status'] = 'hold'; break; case 'approve': case '1': $fields['status'] = 'approved'; break; case 'spam': case 'trash': default: $fields['status'] = $comment->comment_approved; break; } // Type $fields['type'] = apply_filters('get_comment_type', $comment->comment_type); if (empty($fields['type'])) { $fields['type'] = 'comment'; } // Post if ('single' === $context) { $parent = get_post($post['post_parent'], ARRAY_A); $fields['parent'] = $this->prepare_post($parent, 'single-parent'); } // Parent if (('single' === $context || 'single-parent' === $context) && (int) $comment->comment_parent) { $parent_fields = array('meta'); if ($context === 'single') { $parent_fields[] = 'comment'; } $parent = get_comment($post['post_parent']); $fields['parent'] = $this->prepare_comment($parent, $parent_fields, 'single-parent'); } // Parent $fields['parent'] = (int) $comment->comment_parent; // Author if ((int) $comment->user_id !== 0) { $fields['author'] = (int) $comment->user_id; } else { $fields['author'] = array('ID' => 0, 'name' => $comment->comment_author, 'URL' => $comment->comment_author_url, 'avatar' => json_get_avatar_url($comment->comment_author_email)); } // Date $timezone = json_get_timezone(); $date = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $comment->comment_date, $timezone); $fields['date'] = $date->format('c'); $fields['date_tz'] = $date->format('e'); $fields['date_gmt'] = date('c', strtotime($comment->comment_date_gmt)); // Meta $meta = array('links' => array('up' => json_url(sprintf('/posts/%d', (int) $comment->comment_post_ID)))); if (0 !== (int) $comment->comment_parent) { $meta['links']['in-reply-to'] = json_url(sprintf('/posts/%d/comments/%d', (int) $comment->comment_post_ID, (int) $comment->comment_parent)); } if ('single' !== $context) { $meta['links']['self'] = json_url(sprintf('/posts/%d/comments/%d', (int) $comment->comment_post_ID, (int) $comment->comment_ID)); } // Remove unneeded fields $data = array(); if (in_array('comment', $requested_fields)) { $data = array_merge($data, $fields); } if (in_array('meta', $requested_fields)) { $data['meta'] = $meta; } return apply_filters('json_prepare_comment', $data, $comment, $context); }