Exemplo n.º 1
0
/**
 * Display auto-embed panel on the create/admin form
 */
function bp_links_ajax_link_auto_embed_url()
{
    check_ajax_referer('bp_links_save_link-auto-embed');
    try {
        // try to load a service
        $embed_service = BP_Links_Embed::FromUrl($_POST['url']);
        // did we get a rich media service?
        if ($embed_service instanceof BP_Links_Embed_From_Url) {
            // capture embed panel content
            ob_start();
            bp_links_auto_embed_panel_content($embed_service);
            // output response
            bp_links_ajax_response_string(1, $embed_service->title(), $embed_service->description(), ob_get_clean());
        }
        // NOT rich media, fall back to page parser
        $page_parser = BP_Links_Embed_Page_Parser::GetInstance();
        if ($page_parser->from_url($_POST['url'])) {
            $page_title = $page_parser->title();
            $page_desc = $page_parser->description();
            if (!empty($page_title) || !empty($page_desc)) {
                // output response
                bp_links_ajax_response_string(2, $page_title, $page_desc);
            }
        }
    } catch (BP_Links_Embed_User_Exception $e) {
        bp_links_ajax_response_string(-1, esc_html($e->getMessage()));
    } catch (Exception $e) {
        // fall through to generic error for all other exceptions
        // TODO comment out this debug line before tagging a version
        //		bp_links_ajax_response_string( -1, esc_html( $e->getMessage() ) );
    }
    // if all else fails, just spit out generic warning message
    bp_links_ajax_response_string(-2, __('Auto-fill not available for this URL.', 'buddypress-links'));
}
 private function find_elements()
 {
     //
     // try to get the title
     //
     $page_title = $this->parser->title();
     if (!empty($page_title)) {
         $this->data()->title = $page_title;
     } else {
         return false;
     }
     //
     // try to get the description
     //
     $page_desc = $this->parser->description();
     if (!empty($page_desc)) {
         $this->data()->description = $page_desc;
     } else {
         $this->data()->description = null;
     }
     //
     // try to find some images
     //
     $page_images = $this->parser->images(100, 800, 2, 50);
     $page_images_sorted = $this->filter_images($page_images);
     $page_images_bytes = $this->get_images_bytes($page_images_sorted);
     if (is_array($page_images_bytes) && count($page_images_bytes)) {
         // use the array as is
         $this->data()->images = $page_images_bytes;
         // set the default index if image selection has at least one entry
         if (count($this->image_selection())) {
             // set index to first key from array
             $this->data()->images_idx = key($page_images_bytes);
         } else {
             // no images returned by selection method
             $this->data()->images_idx = null;
         }
     } else {
         return false;
     }
     return true;
 }
 /**
  * Get singleton instance
  *
  * @return BP_Links_Embed_Page_Parser
  */
 public static final function GetInstance()
 {
     if (empty(self::$instance)) {
         self::$instance = new BP_Links_Embed_Page_Parser();
     }
     return self::$instance;
 }