function wppa_add_metatags()
{
    global $wpdb;
    global $thumb;
    // Share info for sm that uses og
    $id = wppa_get_get('photo');
    if ($id) {
        $imgurl = wppa_get_photo_url($id);
        if (wppa_is_video($id)) {
            $imgurl = wppa_fix_poster_ext($imgurl, $id);
        }
    } else {
        $imgurl = '';
    }
    if ($id) {
        if (wppa_switch('wppa_og_tags_on')) {
            wppa_cache_thumb($id);
            if ($thumb) {
                $title = wppa_get_photo_name($id);
                $desc = wppa_get_og_desc($id);
                $url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $site = get_bloginfo('name');
                echo '
<!-- WPPA+ Share data -->
<meta property="og:site_name" content="' . esc_attr(sanitize_text_field($site)) . '" />
<meta property="og:type" content="article" />
<meta property="og:url" content="' . esc_url(sanitize_text_field($url)) . '" />
<meta property="og:title" content="' . esc_attr(sanitize_text_field($title)) . '" />
<meta property="og:image" content="' . esc_url(sanitize_text_field($imgurl)) . '" />
<meta property="og:description" content="' . esc_attr(sanitize_text_field($desc)) . '" />
<!-- WPPA+ End Share data -->
';
            }
        }
    }
    // To make sure we are on a page that contains at least %%wppa%% we check for Get var 'wppa-album'.
    // This also narrows the selection of featured photos to those that exist in the current album.
    $done = array();
    if (wppa_get_get('album')) {
        if (wppa_switch('wppa_meta_page')) {
            $album = wppa_get_get('album');
            $photos = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s AND `status` = 'featured'", $album), ARRAY_A);
            wppa_dbg_q('Q-meta1');
            wppa_cache_photo('add', $photos);
            if ($photos) {
                echo "\n<!-- WPPA+ BEGIN Featured photos on this page -->";
                foreach ($photos as $photo) {
                    $thumb = $photo;
                    // Set to global to reduce queries when getting the name
                    $id = $photo['id'];
                    $content = esc_attr(sanitize_text_field(wppa_get_keywords($id)));
                    if ($content && !in_array($content, $done)) {
                        echo '
<meta name="keywords" content="' . $content . '" >';
                        $done[] = $content;
                    }
                }
                echo "\n<!-- WPPA+ END Featured photos on this page -->\n";
            }
        }
    } elseif (wppa_switch('wppa_meta_all')) {
        $photos = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `status` = 'featured'", ARRAY_A);
        wppa_dbg_q('Q-meta2');
        wppa_cache_photo('add', $photos);
        if ($photos) {
            echo "\n<!-- WPPA+ BEGIN Featured photos on this site -->";
            foreach ($photos as $photo) {
                $thumb = $photo;
                // Set to global to reduce queries when getting the name
                $id = $photo['id'];
                $content = esc_attr(sanitize_text_field(wppa_get_keywords($id)));
                if ($content && !in_array($content, $done)) {
                    echo '
<meta name="keywords" content="' . $content . '" >';
                    $done[] = $content;
                }
            }
            echo "\n<!-- WPPA+ END Featured photos on this site -->\n";
        }
    }
    // Facebook Admin and App
    if ((wppa_switch('wppa_share_on') || wppa_switch('wppa_share_on_widget')) && (wppa_switch('wppa_facebook_comments') || wppa_switch('wppa_facebook_like') || wppa_switch('wppa_share_facebook'))) {
        echo "\n<!-- WPPA+ BEGIN Facebook meta tags -->";
        if (wppa_opt('wppa_facebook_admin_id')) {
            echo "\n\t<meta property=\"fb:admins\" content=\"" . wppa_opt('wppa_facebook_admin_id') . "\" />";
        }
        if (wppa_opt('wppa_facebook_app_id')) {
            echo "\n\t<meta property=\"fb:app_id\" content=\"" . wppa_opt('wppa_facebook_app_id') . "\" />";
        }
        if ($imgurl) {
            echo '
<link rel="image_src" href="' . esc_url($imgurl) . '" />';
        }
        echo '
<!-- WPPA+ END Facebook meta tags -->
';
    }
}
function wppa_add_metatags()
{
    global $wpdb;
    // Share info for sm that uses og
    $id = wppa_get_get('photo');
    if (!wppa_photo_exists($id)) {
        $id = false;
    }
    if ($id) {
        // SM may not accept images from the cloud.
        wppa('for_sm', true);
        $imgurl = wppa_get_photo_url($id);
        wppa('for_sm', false);
        if (wppa_is_video($id)) {
            $imgurl = wppa_fix_poster_ext($imgurl, $id);
        }
    } else {
        $imgurl = '';
    }
    if ($id) {
        if (wppa_switch('og_tags_on')) {
            $thumb = wppa_cache_thumb($id);
            if ($thumb) {
                $title = wppa_get_photo_name($id);
                $desc = wppa_get_og_desc($id);
                $url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $site = get_bloginfo('name');
                $mime = wppa_get_mime_type($id);
                echo '
<!-- WPPA+ Og Share data -->
<meta property="og:site_name" content="' . esc_attr(sanitize_text_field($site)) . '" />
<meta property="og:type" content="article" />
<meta property="og:url" content="' . $url . '" />
<meta property="og:title" content="' . esc_attr(sanitize_text_field($title)) . '" />';
                if ($mime) {
                    echo '
<meta property="og:image" content="' . esc_url(sanitize_text_field($imgurl)) . '" />
<meta property="og:image:type" content="' . $mime . '" />
<meta property="og:image:width" content="' . wppa_get_photox($id) . '" />
<meta property="og:image:height" content="' . wppa_get_photoy($id) . '" />';
                }
                if ($desc) {
                    echo '
<meta property="og:description" content="' . esc_attr(sanitize_text_field($desc)) . '" />';
                }
                echo '
<!-- WPPA+ End Og Share data -->
';
            }
        }
        if (wppa_switch('share_twitter') && wppa_opt('twitter_account')) {
            $thumb = wppa_cache_thumb($id);
            // Twitter wants at least 280px in width, and at least 150px in height
            if ($thumb) {
                $x = wppa_get_photo_item($id, 'photox');
                $y = wppa_get_photo_item($id, 'photoy');
            }
            if ($thumb && $x >= 280 && $y >= 150) {
                $title = wppa_get_photo_name($id);
                $desc = wppa_get_og_desc($id);
                $url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $site = get_bloginfo('name');
                echo '
<!-- WPPA+ Twitter Share data -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="' . wppa_opt('twitter_account') . '">
<meta name="twitter:creator" content="' . wppa_opt('twitter_account') . '">
<meta name="twitter:title" content="' . esc_attr(sanitize_text_field($title)) . '">
<meta name="twitter:description" content="' . esc_attr(sanitize_text_field($desc)) . '">
<meta name="twitter:image" content="' . esc_url(sanitize_text_field($imgurl)) . '">
<!-- WPPA+ End Twitter Share data -->
';
            } elseif ($thumb && $x >= 120 && $y >= 120) {
                $title = wppa_get_photo_name($id);
                $desc = wppa_get_og_desc($id);
                $url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $site = get_bloginfo('name');
                echo '
<!-- WPPA+ Twitter Share data -->
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="' . wppa_opt('twitter_account') . '">
<meta name="twitter:title" content="' . esc_attr(sanitize_text_field($title)) . '">
<meta name="twitter:description" content="' . esc_attr(sanitize_text_field($desc)) . '">
<meta name="twitter:image" content="' . esc_url(sanitize_text_field($imgurl)) . '">
<!-- WPPA+ End Twitter Share data -->
';
            }
        }
    }
    // To make sure we are on a page that contains at least [wppa] we check for Get var 'wppa-album'.
    // This also narrows the selection of featured photos to those that exist in the current album.
    $done = array();
    $album = '';
    if (isset($_REQUEST['album'])) {
        $album = $_REQUEST['album'];
    } elseif (isset($_REQUEST['wppa-album'])) {
        $album = $_REQUEST['wppa-album'];
    }
    $album = strip_tags($album);
    if (strlen($album == 12)) {
        $album = wppa_get_get('album');
    }
    if ($album) {
        if (wppa_switch('meta_page')) {
            $photos = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s AND `status` = 'featured'", $album), ARRAY_A);
            wppa_cache_photo('add', $photos);
            if ($photos) {
                echo "\n<!-- WPPA+ BEGIN Featured photos on this page -->";
                foreach ($photos as $photo) {
                    $id = $photo['id'];
                    $content = esc_attr(sanitize_text_field(wppa_get_keywords($id)));
                    if ($content && !in_array($content, $done)) {
                        echo '
<meta name="keywords" content="' . $content . '" >';
                        $done[] = $content;
                    }
                }
                echo "\n<!-- WPPA+ END Featured photos on this page -->\n";
            }
        }
    } elseif (wppa_switch('meta_all')) {
        $photos = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `status` = 'featured'", ARRAY_A);
        wppa_cache_photo('add', $photos);
        if ($photos) {
            echo "\n<!-- WPPA+ BEGIN Featured photos on this site -->";
            foreach ($photos as $photo) {
                $thumb = $photo;
                // Set to global to reduce queries when getting the name
                $id = $photo['id'];
                $content = esc_attr(sanitize_text_field(wppa_get_keywords($id)));
                if ($content && !in_array($content, $done)) {
                    echo '
<meta name="keywords" content="' . $content . '" >';
                    $done[] = $content;
                }
            }
            echo "\n<!-- WPPA+ END Featured photos on this site -->\n";
        }
    }
    // Facebook Admin and App
    if ((wppa_switch('share_on') || wppa_switch('share_on_widget')) && (wppa_switch('facebook_comments') || wppa_switch('facebook_like') || wppa_switch('share_facebook'))) {
        echo "\n<!-- WPPA+ BEGIN Facebook meta tags -->";
        if (wppa_opt('facebook_admin_id')) {
            echo "\n\t<meta property=\"fb:admins\" content=\"" . wppa_opt('facebook_admin_id') . "\" />";
        }
        if (wppa_opt('facebook_app_id')) {
            echo "\n\t<meta property=\"fb:app_id\" content=\"" . wppa_opt('facebook_app_id') . "\" />";
        }
        if ($imgurl) {
            echo '
<link rel="image_src" href="' . esc_url($imgurl) . '" />';
        }
        echo '
<!-- WPPA+ END Facebook meta tags -->
';
    }
}