Exemple #1
0
 /**
  * Function to return related permalinks for a given permalink to bootstrap the Related Content app until the off-line processing routines complete
  *
  * @return list of related permalinks in JSON
  */
 public static function permalink_related()
 {
     global $post;
     // Input Params
     $permalink = isset($_GET['permalink']) ? $_GET['permalink'] : NULL;
     $match = isset($_GET['match']) ? $_GET['match'] : "random";
     // match method
     $n = isset($_GET['n']) ? intval($_GET['n']) : 10;
     // number of related permalinks to return
     $related_permalink_list = array();
     // Get post ID
     if ($permalink == NULL) {
         // default to random match if no permalink is available
         $match = "random";
     } else {
         $post_id = url_to_postid($permalink);
         // for non-default paths - handle both https and http versions of the permalink
         if ($post_id == 0) {
             $parse = parse_url($permalink);
             if ($parse['scheme'] == "https") {
                 $permalink = str_replace("https", "http", $permalink);
                 $post_id = url_to_postid($permalink);
             } else {
                 if ($parse['scheme'] == "http") {
                     $permalink = str_replace("http", "https", $permalink);
                     $post_id = url_to_postid($permalink);
                 }
             }
         }
     }
     if ($match == "random") {
         $args = array('posts_per_page' => $n, 'orderby' => 'rand');
         $rand_posts = get_posts($args);
         foreach ($rand_posts as $post) {
             $related_link = array('page_id' => $post->ID, 'url' => get_permalink($post->ID), 'title' => $post->post_title, 'description' => $post->post_excerpt, 'image_url' => ShareaholicUtilities::permalink_thumbnail($post->ID, "medium"), 'score' => 1);
             array_push($related_permalink_list, $related_link);
         }
         wp_reset_postdata();
     } else {
         // other methods coming soon
     }
     // Construct results array
     $result = array('request' => array('api_key' => ShareaholicUtilities::get_option('api_key'), 'url' => $permalink), 'internal' => $related_permalink_list);
     header('Content-Type: application/json; charset=utf-8');
     header('Cache-Control: max-age=180');
     // 3 minutes
     echo json_encode($result);
     exit;
 }