/**
 * 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) {
            // output response
            bp_links_ajax_response_string(1, $embed_service->title(), $embed_service->description(), bp_get_links_auto_embed_panel_content($embed_service));
        }
        // 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'));
}
function bp_links_auto_embed_panel_content($embed_service = null)
{
    echo bp_get_links_auto_embed_panel_content($embed_service);
}