示例#1
0
/**
 * Generates Opengraph metadata.
 *
 * Currently for:
 * - home page
 * - author archive
 * - content
 */
function amt_add_opengraph_metadata_head($post, $attachments, $embedded_media, $options)
{
    if (apply_filters('amt_exclude_opengraph_metadata', false)) {
        return array();
    }
    $do_auto_opengraph = $options["auto_opengraph"] == "1" ? true : false;
    if (!$do_auto_opengraph) {
        return array();
    }
    $metadata_arr = array();
    // fb:app_id & fb:admins
    // We currently let users add the full meta tags for fb:app_id and fb:admins in the site wide meta tags box.
    // fb:app_id appears everywhere
    //if ( ! empty($options['social_main_facebook_app_id']) ) {
    //    $metadata_arr[] = '<meta property="fb:app_id" content="' . esc_attr( $options['social_main_facebook_app_id'] ) . '" />';
    //}
    // fb:admins appear everywhere
    //if ( ! empty($options['social_main_facebook_admins']) ) {
    //    $fb_admins_arr = explode(',', $options['social_main_facebook_admins']);
    //    foreach ( $fb_admins_arr as $fb_admin ) {
    //        $metadata_arr[] = '<meta property="fb:admins" content="' . esc_attr( trim($fb_admin) ) . '" />';
    //    }
    //}
    // no publisher meta tag for facebook, unless it is content
    // Custom content override
    if (amt_is_custom($post, $options)) {
        // Return metadata with:
        // add_filter( 'amt_custom_metadata_opengraph', 'my_function', 10, 5 );
        // Return an array of meta tags. Array item format: ['key_can_be_whatever'] = '<meta name="foo" content="bar" />'
        $metadata_arr = apply_filters('amt_custom_metadata_opengraph', $metadata_arr, $post, $options, $attachments, $embedded_media);
        // Default front page displaying the latest posts
    } elseif (amt_is_default_front_page()) {
        // Type
        $metadata_arr[] = '<meta property="og:type" content="website" />';
        // Site Name
        $metadata_arr[] = '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '" />';
        // Title - Note: Contains multipage information
        $metadata_arr['og:title'] = '<meta property="og:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // URL - Note: different method to get the permalink on paged archives
        if (is_paged()) {
            $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(get_pagenum_link(get_query_var('paged'))) . '" />';
        } else {
            $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(trailingslashit(get_bloginfo('url'))) . '" />';
        }
        // Site description - Note: Contains multipage information through amt_process_paged()
        $site_description = amt_get_site_description($options);
        if (empty($site_description)) {
            $site_description = get_bloginfo('description');
        }
        if (!empty($site_description)) {
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged($site_description)) . '" />';
        }
        // Locale
        $metadata_arr[] = '<meta property="og:locale" content="' . esc_attr(str_replace('-', '_', amt_get_language_site($options))) . '" />';
        // Site Image
        // Use the default image, if one has been set.
        $image_data = amt_get_default_image_data();
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_index', 'full');
            $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
        }
        // Front page using a static page
        // Note: might also contain a listing of posts which may be paged, so use amt_process_paged()
    } elseif (amt_is_static_front_page()) {
        // Type
        if ($options['author_profile_source'] == 'frontpage') {
            // The front page is treated as the profile page.
            $metadata_arr[] = '<meta property="og:type" content="profile" />';
        } else {
            $metadata_arr[] = '<meta property="og:type" content="website" />';
        }
        // Site Name
        $metadata_arr[] = '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '" />';
        // Title - Note: Contains multipage information
        $metadata_arr['og:title'] = '<meta property="og:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // URL - Note: different method to get the permalink on paged archives
        if (is_paged()) {
            $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(get_pagenum_link(get_query_var('paged'))) . '" />';
        } else {
            $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(trailingslashit(get_bloginfo('url'))) . '" />';
        }
        // Site Description - Note: Contains multipage information through amt_process_paged()
        $content_desc = amt_get_content_description($post);
        if (!empty($content_desc)) {
            // Use the pages custom description
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged($content_desc)) . '" />';
        } elseif (get_bloginfo('description')) {
            // Alternatively use the blog's description
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged(get_bloginfo('description'))) . '" />';
        }
        // Locale
        $metadata_arr[] = '<meta property="og:locale" content="' . esc_attr(str_replace('-', '_', amt_get_language_content($options, $post))) . '" />';
        // Site Image
        // First check if a global image override URL has been entered.
        // If yes, use this image URL and override all other images.
        $image_data = amt_get_image_attributes_array(amt_get_post_meta_image_url($post->ID));
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_index', 'full');
            $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
            //$global_image_override_url = amt_get_post_meta_image_url($post->ID);
            //if ( ! empty( $global_image_override_url ) ) {
            //    $metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw( $global_image_override_url ) . '" />';
            //    if ( is_ssl() || ( ! is_ssl() && $options["has_https_access"] == "1" ) ) {
            //        $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw( str_replace('http:', 'https:', $global_image_override_url ) ) . '" />';
            //    }
            // Then try the featured image, if exists.
        } elseif (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
            // Allow filtering of the image size.
            $image_size = apply_filters('amt_image_size_index', 'full');
            $metadata_arr = array_merge($metadata_arr, amt_get_opengraph_image_metatags($options, get_post_thumbnail_id($post->ID), $size = $image_size));
        } else {
            // Use the default image, if one has been set.
            $image_data = amt_get_default_image_data();
            if (!empty($image_data)) {
                $image_size = apply_filters('amt_image_size_index', 'full');
                $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
                if (!empty($image_meta_tags)) {
                    $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                }
            }
            // Alternatively, use default image
            //$metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw( $options["default_image_url"] ) . '" />';
            //if ( is_ssl() || ( ! is_ssl() && $options["has_https_access"] == "1" ) ) {
            //    $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw( str_replace('http:', 'https:', $options["default_image_url"] ) ) . '" />';
            //}
        }
        // Profile data (only if the front page has been set as the source of profile.)
        if ($options['author_profile_source'] == 'frontpage') {
            // Profile first and last name
            $last_name = get_the_author_meta('last_name', $post->post_author);
            if (!empty($last_name)) {
                $metadata_arr[] = '<meta property="profile:last_name" content="' . esc_attr($last_name) . '" />';
            }
            $first_name = get_the_author_meta('first_name', $post->post_author);
            if (!empty($first_name)) {
                $metadata_arr[] = '<meta property="profile:first_name" content="' . esc_attr($first_name) . '" />';
            }
        }
        // The posts index page - a static page displaying the latest posts
    } elseif (amt_is_static_home()) {
        // Type
        $metadata_arr[] = '<meta property="og:type" content="website" />';
        // Site Name
        $metadata_arr[] = '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '" />';
        // Title - Note: Contains multipage information
        $metadata_arr['og:title'] = '<meta property="og:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // URL - Note: different method to get the permalink on paged archives
        if (is_paged()) {
            $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(get_pagenum_link(get_query_var('paged'))) . '" />';
        } else {
            $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(get_permalink($post->ID)) . '" />';
        }
        // Site Description - Note: Contains multipage information through amt_process_paged()
        $content_desc = amt_get_content_description($post);
        if (!empty($content_desc)) {
            // Use the pages custom description
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged($content_desc)) . '" />';
        } elseif (get_bloginfo('description')) {
            // Alternatively use a generic description
            $metadata_arr[] = '<meta property="og:description" content="' . amt_process_paged("An index of the latest content.") . '" />';
        }
        // Locale
        $metadata_arr[] = '<meta property="og:locale" content="' . esc_attr(str_replace('-', '_', amt_get_language_content($options, $post))) . '" />';
        // Site Image
        // First check if a global image override URL has been entered.
        // If yes, use this image URL and override all other images.
        $image_data = amt_get_image_attributes_array(amt_get_post_meta_image_url($post->ID));
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_index', 'full');
            $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
            //$global_image_override_url = amt_get_post_meta_image_url($post->ID);
            //if ( ! empty( $global_image_override_url ) ) {
            //    $metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw( $global_image_override_url ) . '" />';
            //    if ( is_ssl() || ( ! is_ssl() && $options["has_https_access"] == "1" ) ) {
            //        $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw( str_replace('http:', 'https:', $global_image_override_url ) ) . '" />';
            //    }
            // Then try the featured image, if exists.
        } elseif (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
            // Allow filtering of the image size.
            $image_size = apply_filters('amt_image_size_index', 'full');
            $metadata_arr = array_merge($metadata_arr, amt_get_opengraph_image_metatags($options, get_post_thumbnail_id($post->ID), $size = $image_size));
        } else {
            // Use the default image, if one has been set.
            $image_data = amt_get_default_image_data();
            if (!empty($image_data)) {
                $image_size = apply_filters('amt_image_size_index', 'full');
                $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
                if (!empty($image_meta_tags)) {
                    $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                }
            }
            // Alternatively, use default image
            //$metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw( $options["default_image_url"] ) . '" />';
            //if ( is_ssl() || ( ! is_ssl() && $options["has_https_access"] == "1" ) ) {
            //    $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw( str_replace('http:', 'https:', $options["default_image_url"] ) ) . '" />';
            //}
        }
        // Category, Tag, Taxonomy archives
        // Note: product groups should pass the is_tax() validation, so no need for
        // amt_is_product_group(). We do not support other product groups.
        // amt_is_product_group() is used below to set the og:type to product.group.
    } elseif (is_category() || is_tag() || is_tax()) {
        // Taxonomy term object.
        // When viewing taxonomy archives, the $post object is the taxonomy term object. Check with: var_dump($post);
        $tax_term_object = $post;
        //var_dump($tax_term_object);
        // Type
        // In case of a product group taxonomy, we set the og:type to product.group
        if (amt_is_product_group()) {
            $metadata_arr[] = '<meta property="og:type" content="product.group" />';
        } else {
            $metadata_arr[] = '<meta property="og:type" content="website" />';
        }
        // Site Name
        $metadata_arr[] = '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '" />';
        // Title - Note: Contains multipage information
        $metadata_arr['og:title'] = '<meta property="og:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // URL - Note: different method to get the permalink on paged archives
        $url = get_term_link($tax_term_object);
        if (is_paged()) {
            $url = trailingslashit($url) . get_query_var('paged') . '/';
        }
        $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw($url) . '" />';
        // Description
        // If set, the description of the custom taxonomy term is used in the 'description' metatag.
        // Otherwise, a generic description is used.
        // Here we sanitize the provided description for safety
        $description_content = sanitize_text_field(amt_sanitize_description(term_description($tax_term_object->term_id, $tax_term_object->taxonomy)));
        // Note: Contains multipage information through amt_process_paged()
        if (empty($description_content)) {
            // Add a filtered generic description.
            // Filter name
            if (is_category()) {
                $generic_description = apply_filters('amt_generic_description_category_archive', __('Content filed under the %s category.', 'add-meta-tags'));
            } elseif (is_tag()) {
                $generic_description = apply_filters('amt_generic_description_tag_archive', __('Content tagged with %s.', 'add-meta-tags'));
            } elseif (is_tax()) {
                // Construct the filter name. Template: ``amt_generic_description_TAXONOMYSLUG_archive``
                $taxonomy_description_filter_name = sprintf('amt_generic_description_%s_archive', $tax_term_object->taxonomy);
                // var_dump($taxonomy_description_filter_name);
                // Generic description
                $generic_description = apply_filters($taxonomy_description_filter_name, __('Content filed under the %s taxonomy.', 'add-meta-tags'));
            }
            // Final generic description
            $generic_description = sprintf($generic_description, single_term_title($prefix = '', $display = false));
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
        } else {
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged($description_content)) . '" />';
        }
        // Locale
        $metadata_arr[] = '<meta property="og:locale" content="' . esc_attr(str_replace('-', '_', amt_get_language_site($options))) . '" />';
        // Image
        // Use an image from the 'Global image override' field.
        // Otherwise, use a user defined image via filter.
        // Otherwise use default image.
        $image_data = amt_get_image_attributes_array(amt_get_term_meta_image_url($tax_term_object->term_id));
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_index', 'full');
            $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
        } else {
            // First filter using a term/taxonomy agnostic filter name.
            $taxonomy_image_url = apply_filters('amt_taxonomy_force_image_url', '', $tax_term_object);
            if (empty($taxonomy_image_url)) {
                // Second filter (term/taxonomy dependent).
                // Construct the filter name. Template: ``amt_taxonomy_image_url_TAXONOMYSLUG_TERMSLUG``
                $taxonomy_image_url_filter_name = sprintf('amt_taxonomy_image_url_%s_%s', $tax_term_object->taxonomy, $tax_term_object->slug);
                //var_dump($taxonomy_image_url_filter_name);
                // The default image, if set, is used by default.
                $taxonomy_image_url = apply_filters($taxonomy_image_url_filter_name, $options["default_image_url"]);
            }
            if (!empty($taxonomy_image_url)) {
                $image_data = amt_get_image_attributes_array($taxonomy_image_url);
                if (!empty($image_data)) {
                    $image_size = apply_filters('amt_image_size_index', 'full');
                    $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
                    if (!empty($image_meta_tags)) {
                        $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                    }
                }
                //$metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw( $taxonomy_image_url ) . '" />';
                //if ( is_ssl() || ( ! is_ssl() && $options["has_https_access"] == "1" ) ) {
                //    $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw( str_replace('http:', 'https:', $taxonomy_image_url ) ) . '" />';
                //}
            }
        }
        // Author archive. First page is considered a profile page.
    } elseif (is_author()) {
        // Author object
        // NOTE: Inside the author archives `$post->post_author` does not contain the author object.
        // In this case the $post (get_queried_object()) contains the author object itself.
        // We also can get the author object with the following code. Slug is what WP uses to construct urls.
        // $author = get_user_by( 'slug', get_query_var( 'author_name' ) );
        // Also, ``get_the_author_meta('....', $author)`` returns nothing under author archives.
        // Access user meta with:  $author->description, $author->user_email, etc
        // $author = get_queried_object();
        $author = $post;
        // Type
        if (!is_paged() && $options['author_profile_source'] == 'default') {
            // We treat the first page of the archive as a profile, only if
            // the profile source has been set to 'default'
            $metadata_arr[] = '<meta property="og:type" content="profile" />';
        } else {
            $metadata_arr[] = '<meta property="og:type" content="website" />';
        }
        // Site Name
        $metadata_arr[] = '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '" />';
        // Title - Note: Contains multipage information
        //if ( ! is_paged() ) {
        //    // We treat the first page of the archive as a profile
        //    $metadata_arr[] = '<meta property="og:title" content="' . esc_attr( $author->display_name ) . ' profile page" />';
        //} else {
        //    $metadata_arr[] = '<meta property="og:title" content="' . esc_attr( amt_process_paged( "Content published by " . $author->display_name ) ) . '" />';
        //}
        $metadata_arr['og:title'] = '<meta property="og:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . ' profile page" />';
        // URL - Note: different method to get the permalink on paged archives
        // If a Facebook author profile URL has been provided, it has priority,
        // Otherwise fall back to the WordPress author archive.
        $fb_author_url = $author->amt_facebook_author_profile_url;
        if (!empty($fb_author_url)) {
            $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw($fb_author_url, array('http', 'https')) . '" />';
        } else {
            if (is_paged()) {
                $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(get_pagenum_link(get_query_var('paged'))) . '" />';
            } else {
                $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(get_author_posts_url($author->ID)) . '" />';
                // The following makes no sense here. 'get_author_posts_url( $author->ID )' will do in all cases.
                //$metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw( amt_get_local_author_profile_url( $author->ID, $options ) ) . '" />';
            }
        }
        // description - Note: Contains multipage information through amt_process_paged()
        if (is_paged()) {
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged("Content published by " . $author->display_name)) . '" />';
        } else {
            // Here we sanitize the provided description for safety
            // We treat the first page of the archive as a profile
            $author_description = sanitize_text_field(amt_sanitize_description($author->description));
            if (empty($author_description)) {
                $metadata_arr[] = '<meta property="og:description" content="' . esc_attr("Content published by " . $author->display_name) . '" />';
            } else {
                $metadata_arr[] = '<meta property="og:description" content="' . esc_attr($author_description) . '" />';
            }
        }
        // Locale
        $metadata_arr[] = '<meta property="og:locale" content="' . esc_attr(str_replace('-', '_', amt_get_language_site($options))) . '" />';
        // Profile Image
        // First use the global image override URL
        $image_data = amt_get_image_attributes_array(amt_get_user_meta_image_url($author->ID));
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_index', 'full');
            $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
        } else {
            $author_email = sanitize_email($author->user_email);
            $avatar_size = apply_filters('amt_avatar_size', 128);
            $avatar_url = '';
            // First try to get the avatar link by using get_avatar().
            // Important: for this to work the "Show Avatars" option should be enabled in Settings > Discussion.
            $avatar_img = get_avatar(get_the_author_meta('ID', $author->ID), $avatar_size, '', get_the_author_meta('display_name', $author->ID));
            if (!empty($avatar_img)) {
                if (preg_match("#src=['\"]([^'\"]+)['\"]#", $avatar_img, $matches)) {
                    $avatar_url = $matches[1];
                }
            } elseif (!empty($author_email)) {
                // If the user has provided an email, we use it to construct a gravatar link.
                $avatar_url = "http://www.gravatar.com/avatar/" . md5($author_email) . "?s=" . $avatar_size;
            }
            if (!empty($avatar_url)) {
                //$avatar_url = html_entity_decode($avatar_url, ENT_NOQUOTES, 'UTF-8');
                $metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw($avatar_url) . '" />';
                // Add an og:imagesecure_url if the image URL uses HTTPS
                if (strpos($avatar_url, 'https://') !== false) {
                    $metadata_arr[] = '<meta property="og:imagesecure_url" content="' . esc_url_raw($avatar_url) . '" />';
                }
                if (apply_filters('amt_extended_image_tags', true)) {
                    $metadata_arr[] = '<meta property="og:image:width" content="' . esc_attr($avatar_size) . '" />';
                    $metadata_arr[] = '<meta property="og:image:height" content="' . esc_attr($avatar_size) . '" />';
                    // Since we do not have a way to determine the image type, the following meta tag is commented out
                    // TODO: make a function that detects the image type from the file extension (if a file extension is available)
                    //$metadata_arr[] = '<meta property="og:image:type" content="image/jpeg" />';
                }
            }
        }
        // Profile data (only on the 1st page of the archive)
        if (!is_paged() && $options['author_profile_source'] == 'default') {
            // Profile first and last name
            $last_name = $author->last_name;
            if (!empty($last_name)) {
                $metadata_arr[] = '<meta property="profile:last_name" content="' . esc_attr($last_name) . '" />';
            }
            $first_name = $author->first_name;
            if (!empty($first_name)) {
                $metadata_arr[] = '<meta property="profile:first_name" content="' . esc_attr($first_name) . '" />';
            }
        }
        // Custom Post Type archives
    } elseif (is_post_type_archive()) {
        // Custom post type object.
        // When viewing custom post type archives, the $post object is the custom post type object. Check with: var_dump($post);
        $post_type_object = $post;
        //var_dump($post_type_object);
        // Type
        $metadata_arr[] = '<meta property="og:type" content="website" />';
        // Site Name
        $metadata_arr[] = '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '" />';
        // Title - Note: Contains multipage information
        $metadata_arr['og:title'] = '<meta property="og:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // URL - Note: different method to get the permalink on paged archives
        $url = get_post_type_archive_link($post_type_object->name);
        if (is_paged()) {
            $url = trailingslashit($url) . get_query_var('paged') . '/';
        }
        $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw($url) . '" />';
        // Description
        // Note: Contains multipage information through amt_process_paged()
        // Add a filtered generic description.
        // Construct the filter name. Template: ``amt_generic_description_posttype_POSTTYPESLUG_archive``
        $custom_post_type_description_filter_name = sprintf('amt_generic_description_posttype_%s_archive', $post_type_object->name);
        // var_dump($custom_post_type_description_filter_name);
        // Generic description
        $generic_description = apply_filters($custom_post_type_description_filter_name, __('%s archive.', 'add-meta-tags'));
        // Final generic description
        $generic_description = sprintf($generic_description, post_type_archive_title($prefix = '', $display = false));
        $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
        // Locale
        $metadata_arr[] = '<meta property="og:locale" content="' . esc_attr(str_replace('-', '_', amt_get_language_site($options))) . '" />';
        // Image
        // Use a user defined image via filter. Otherwise use default image.
        // First filter using a term/taxonomy agnostic filter name.
        $posttype_image_url = apply_filters('amt_posttype_force_image_url', '', $post_type_object);
        if (empty($posttype_image_url)) {
            // Second filter (post type dependent).
            // Construct the filter name. Template: ``amt_posttype_image_url_POSTTYPESLUG``
            $posttype_image_url_filter_name = sprintf('amt_posttype_image_url_%s', $post_type_object->name);
            //var_dump($posttype_image_url_filter_name);
            // The default image, if set, is used by default.
            $posttype_image_url = apply_filters($posttype_image_url_filter_name, $options["default_image_url"]);
        }
        if (!empty($posttype_image_url)) {
            $image_data = amt_get_image_attributes_array($posttype_image_url);
            if (!empty($image_data)) {
                $image_size = apply_filters('amt_image_size_index', 'full');
                $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
                if (!empty($image_meta_tags)) {
                    $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                }
            }
            //$metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw( $posttype_image_url ) . '" />';
            //if ( is_ssl() || ( ! is_ssl() && $options["has_https_access"] == "1" ) ) {
            //    $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw( str_replace('http:', 'https:', $posttype_image_url ) ) . '" />';
            //}
        }
        // Attachments
    } elseif (is_attachment()) {
        $mime_type = get_post_mime_type($post->ID);
        //$attachment_type = strstr( $mime_type, '/', true );
        // See why we do not use strstr(): http://www.codetrax.org/issues/1091
        $attachment_type = preg_replace('#\\/[^\\/]*$#', '', $mime_type);
        // First add metadata common to all attachment types.
        // Type
        // Note: there is no specific type for images/audio. We use article amd video
        // TODO: Check whether we could use another type specific to each attachment type.
        if ('video' == $attachment_type) {
            // video.other og:type for video attachment pages
            $og_type = 'video.other';
        } else {
            $og_type = 'article';
        }
        $og_type = apply_filters('amt_opengraph_og_type_attachment', $og_type);
        $metadata_arr[] = '<meta property="og:type" content="' . esc_attr($og_type) . '" />';
        // Site Name
        $metadata_arr[] = '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '" />';
        // Title
        $metadata_arr['og:title'] = '<meta property="og:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // URL
        $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(get_permalink($post->ID)) . '" />';
        // Description - We use the description defined by Add-Meta-Tags
        $content_desc = amt_get_content_description($post);
        if (!empty($content_desc)) {
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr($content_desc) . '" />';
        }
        // Locale
        $metadata_arr[] = '<meta property="og:locale" content="' . esc_attr(str_replace('-', '_', amt_get_language_content($options, $post))) . '" />';
        // og:updated_time
        $metadata_arr[] = '<meta property="og:updated_time" content="' . esc_attr(amt_iso8601_date($post->post_modified)) . '" />';
        // Metadata specific to each attachment type
        if ('image' == $attachment_type) {
            // Allow filtering of the image size.
            $image_size = apply_filters('amt_image_size_attachment', 'full');
            $metadata_arr = array_merge($metadata_arr, amt_get_opengraph_image_metatags($options, $post->ID, $size = $image_size));
        } elseif ('video' == $attachment_type) {
            if ($options["og_omit_video_metadata"] != "1") {
                // Video tags
                $metadata_arr[] = '<meta property="og:video" content="' . esc_url_raw(wp_get_attachment_url($post->ID)) . '" />';
                if (is_ssl() || !is_ssl() && $options["has_https_access"] == "1") {
                    $metadata_arr[] = '<meta property="og:video:secure_url" content="' . esc_url_raw(str_replace('http:', 'https:', wp_get_attachment_url($post->ID))) . '" />';
                }
                //
                //$metadata_arr[] = '<meta property="og:video:width" content="' . esc_attr( $main_size_meta[1] ) . '" />';
                //$metadata_arr[] = '<meta property="og:video:height" content="' . esc_attr( $main_size_meta[2] ) . '" />';
                $metadata_arr[] = '<meta property="og:video:type" content="' . esc_attr($mime_type) . '" />';
            }
        } elseif ('audio' == $attachment_type) {
            // Audio tags
            $metadata_arr[] = '<meta property="og:audio" content="' . esc_url_raw(wp_get_attachment_url($post->ID)) . '" />';
            if (is_ssl() || !is_ssl() && $options["has_https_access"] == "1") {
                $metadata_arr[] = '<meta property="og:audio:secure_url" content="' . esc_url_raw(str_replace('http:', 'https:', wp_get_attachment_url($post->ID))) . '" />';
            }
            $metadata_arr[] = '<meta property="og:audio:type" content="' . esc_attr($mime_type) . '" />';
        }
        // Article: meta tags
        // Dates
        $metadata_arr[] = '<meta property="article:published_time" content="' . esc_attr(amt_iso8601_date($post->post_date)) . '" />';
        $metadata_arr[] = '<meta property="article:modified_time" content="' . esc_attr(amt_iso8601_date($post->post_modified)) . '" />';
        // Author
        // If a Facebook author profile URL has been provided, it has priority,
        // Otherwise fall back to the WordPress author archive.
        $fb_author_url = get_the_author_meta('amt_facebook_author_profile_url', $post->post_author);
        if (!empty($fb_author_url)) {
            $metadata_arr[] = '<meta property="article:author" content="' . esc_url_raw($fb_author_url, array('http', 'https', 'mailto')) . '" />';
        } else {
            //$metadata_arr[] = '<meta property="article:author" content="' . esc_url_raw( get_author_posts_url( get_the_author_meta( 'ID', $post->post_author ) ) ) . '" />';
            $metadata_arr[] = '<meta property="article:author" content="' . esc_url_raw(amt_get_local_author_profile_url(get_the_author_meta('ID', $post->post_author), $options)) . '" />';
        }
        // Publisher
        // If a Facebook publisher profile URL has been provided, it has priority.
        // Otherwise fall back to the WordPress blog home url.
        if (!empty($options['social_main_facebook_publisher_profile_url'])) {
            $metadata_arr[] = '<meta property="article:publisher" content="' . esc_url_raw($options['social_main_facebook_publisher_profile_url'], array('http', 'https', 'mailto')) . '" />';
        } else {
            $metadata_arr[] = '<meta property="article:publisher" content="' . esc_url_raw(trailingslashit(get_bloginfo('url'))) . '" />';
        }
        // Posts, pages, custom content types (attachments excluded, caught in previous clause)
        // Note: content might be multipage. Process with amt_process_paged() wherever needed.
    } elseif (is_singular()) {
        // Site Name
        $metadata_arr[] = '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '" />';
        // Type
        // og:type set to 'video.other' for posts with post format set to video
        if (get_post_format($post->ID) == 'video') {
            $og_type = 'video.other';
            // og:type set to 'product' if amt_is_product() validates
            // See:
            //  * https://developers.facebook.com/docs/reference/opengraph/object-type/product/
            //  * https://developers.facebook.com/docs/payments/product
        } elseif (amt_is_product()) {
            $og_type = 'product';
            // In any other case 'article' is used as the og:type
        } else {
            $og_type = 'article';
        }
        // Allow filtering of og:type
        $og_type = apply_filters('amt_opengraph_og_type_content', $og_type);
        // Set og:type meta tag.
        $metadata_arr[] = '<meta property="og:type" content="' . esc_attr($og_type) . '" />';
        // Title
        // Note: Contains multipage information
        $metadata_arr['og:title'] = '<meta property="og:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // URL - Uses amt_get_permalink_for_multipage()
        $metadata_arr[] = '<meta property="og:url" content="' . esc_url_raw(amt_get_permalink_for_multipage($post)) . '" />';
        // Description - We use the description defined by Add-Meta-Tags
        // Note: Contains multipage information through amt_process_paged()
        $content_desc = amt_get_content_description($post);
        if (!empty($content_desc)) {
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr(amt_process_paged($content_desc)) . '" />';
        }
        // Locale
        $metadata_arr[] = '<meta property="og:locale" content="' . esc_attr(str_replace('-', '_', amt_get_language_content($options, $post))) . '" />';
        // og:updated_time
        $metadata_arr[] = '<meta property="og:updated_time" content="' . esc_attr(amt_iso8601_date($post->post_modified)) . '" />';
        // Image
        // First check if a global image override URL has been entered.
        // If yes, use this image URL and override all other images.
        $image_data = amt_get_image_attributes_array(amt_get_post_meta_image_url($post->ID));
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_content', 'full');
            $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
            //$global_image_override_url = amt_get_post_meta_image_url($post->ID);
            //if ( ! empty( $global_image_override_url ) ) {
            //    $metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw( $global_image_override_url ) . '" />';
            //    if ( is_ssl() || ( ! is_ssl() && $options["has_https_access"] == "1" ) ) {
            //        $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw( str_replace('http:', 'https:', $global_image_override_url ) ) . '" />';
            //    }
            // Further image processing
        } else {
            // Media Limits
            $image_limit = amt_metadata_get_image_limit($options);
            $video_limit = amt_metadata_get_video_limit($options);
            $audio_limit = amt_metadata_get_audio_limit($options);
            // Counters
            $ic = 0;
            // image counter
            $vc = 0;
            // video counter
            $ac = 0;
            // audio counter
            // We store the featured image ID in this variable so that it can easily be excluded
            // when all images are parsed from the $attachments array.
            $featured_image_id = 0;
            // Set to true if any image attachments are found. Use to finally add the default image
            // if no image attachments have been found.
            $has_images = false;
            if (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
                // Allow filtering of the image size.
                $image_size = apply_filters('amt_image_size_content', 'full');
                $metadata_arr = array_merge($metadata_arr, amt_get_opengraph_image_metatags($options, get_post_thumbnail_id($post->ID), $size = $image_size));
                // Finally, set the $featured_image_id
                $featured_image_id = get_post_thumbnail_id($post->ID);
                // Images have been found.
                $has_images = true;
                // Increase image counter
                $ic++;
            }
            // Process all attachments and add metatags (featured image will be excluded)
            foreach ($attachments as $attachment) {
                // Excluded the featured image since
                if ($attachment->ID != $featured_image_id) {
                    $mime_type = get_post_mime_type($attachment->ID);
                    //$attachment_type = strstr( $mime_type, '/', true );
                    // See why we do not use strstr(): http://www.codetrax.org/issues/1091
                    $attachment_type = preg_replace('#\\/[^\\/]*$#', '', $mime_type);
                    if ('image' == $attachment_type && $ic < $image_limit) {
                        // Image tags
                        // Allow filtering of the image size.
                        $image_size = apply_filters('amt_image_size_content', 'full');
                        $metadata_arr = array_merge($metadata_arr, amt_get_opengraph_image_metatags($options, $attachment->ID, $size = $image_size));
                        // Images have been found.
                        $has_images = true;
                        // Increase image counter
                        $ic++;
                    } elseif ('video' == $attachment_type && $vc < $video_limit) {
                        if ($options["og_omit_video_metadata"] != "1") {
                            // Video tags
                            $metadata_arr[] = '<meta property="og:video" content="' . esc_url_raw(wp_get_attachment_url($attachment->ID)) . '" />';
                            if (is_ssl() || !is_ssl() && $options["has_https_access"] == "1") {
                                $metadata_arr[] = '<meta property="og:video:secure_url" content="' . esc_url_raw(str_replace('http:', 'https:', wp_get_attachment_url($attachment->ID))) . '" />';
                            }
                            //$metadata_arr[] = '<meta property="og:video:width" content="' . esc_attr( $main_size_meta[1] ) . '" />';
                            //$metadata_arr[] = '<meta property="og:video:height" content="' . esc_attr( $main_size_meta[2] ) . '" />';
                            $metadata_arr[] = '<meta property="og:video:type" content="' . esc_attr($mime_type) . '" />';
                            // Increase video counter
                            $vc++;
                        }
                    } elseif ('audio' == $attachment_type && $ac < $audio_limit) {
                        // Audio tags
                        $metadata_arr[] = '<meta property="og:audio" content="' . esc_url_raw(wp_get_attachment_url($attachment->ID)) . '" />';
                        if (is_ssl() || !is_ssl() && $options["has_https_access"] == "1") {
                            $metadata_arr[] = '<meta property="og:audio:secure_url" content="' . esc_url_raw(str_replace('http:', 'https:', wp_get_attachment_url($attachment->ID))) . '" />';
                        }
                        $metadata_arr[] = '<meta property="og:audio:type" content="' . esc_attr($mime_type) . '" />';
                        // Increase audio counter
                        $ac++;
                    }
                }
            }
            // Embedded Media
            foreach ($embedded_media['images'] as $embedded_item) {
                if ($ic == $image_limit) {
                    break;
                }
                $metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw($embedded_item['image']) . '" />';
                $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw(str_replace('http:', 'https:', $embedded_item['image'])) . '" />';
                if (apply_filters('amt_extended_image_tags', true)) {
                    $metadata_arr[] = '<meta property="og:image:width" content="' . esc_attr($embedded_item['width']) . '" />';
                    $metadata_arr[] = '<meta property="og:image:height" content="' . esc_attr($embedded_item['height']) . '" />';
                    $metadata_arr[] = '<meta property="og:image:type" content="image/jpeg" />';
                }
                // Images have been found.
                $has_images = true;
                // Increase image counter
                $ic++;
            }
            foreach ($embedded_media['videos'] as $embedded_item) {
                if ($options["og_omit_video_metadata"] != "1") {
                    if ($vc == $video_limit) {
                        break;
                    }
                    $metadata_arr[] = '<meta property="og:video" content="' . esc_url_raw($embedded_item['player']) . '" />';
                    $metadata_arr[] = '<meta property="og:video:secure_url" content="' . esc_url_raw(str_replace('http:', 'https:', $embedded_item['player'])) . '" />';
                    $metadata_arr[] = '<meta property="og:video:type" content="application/x-shockwave-flash" />';
                    $metadata_arr[] = '<meta property="og:video:width" content="' . esc_attr($embedded_item['width']) . '" />';
                    $metadata_arr[] = '<meta property="og:video:height" content="' . esc_attr($embedded_item['height']) . '" />';
                    // Increase video counter
                    $vc++;
                }
            }
            foreach ($embedded_media['sounds'] as $embedded_item) {
                if ($ac == $audio_limit) {
                    break;
                }
                $metadata_arr[] = '<meta property="og:audio" content="' . esc_url_raw($embedded_item['player']) . '" />';
                $metadata_arr[] = '<meta property="og:audio:secure_url" content="' . esc_url_raw(str_replace('http:', 'https:', $embedded_item['player'])) . '" />';
                $metadata_arr[] = '<meta property="og:audio:type" content="application/x-shockwave-flash" />';
                // Increase audio counter
                $ac++;
            }
            // If no images have been found so far use the default image, if set.
            if ($has_images === false) {
                // Use the default image, if one has been set.
                $image_data = amt_get_default_image_data();
                if (!empty($image_data)) {
                    $image_size = apply_filters('amt_image_size_content', 'full');
                    $image_meta_tags = amt_get_opengraph_image_metatags($options, $image_data, $size = $image_size);
                    if (!empty($image_meta_tags)) {
                        $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                    }
                }
                //$metadata_arr[] = '<meta property="og:image" content="' . esc_url_raw( $options["default_image_url"] ) . '" />';
                //if ( is_ssl() || ( ! is_ssl() && $options["has_https_access"] == "1" ) ) {
                //    $metadata_arr[] = '<meta property="og:image:secure_url" content="' . esc_url_raw( str_replace('http:', 'https:', $options["default_image_url"] ) ) . '" />';
                //}
            }
        }
        // og:referenced
        $referenced_url_list = amt_get_referenced_items($post);
        foreach ($referenced_url_list as $referenced_url) {
            $referenced_url = trim($referenced_url);
            if (!empty($referenced_url)) {
                $metadata_arr[] = '<meta property="og:referenced" content="' . esc_url_raw($referenced_url) . '" />';
            }
        }
        // Article: meta tags
        if ($og_type == 'article') {
            // Dates
            $metadata_arr[] = '<meta property="article:published_time" content="' . esc_attr(amt_iso8601_date($post->post_date)) . '" />';
            $metadata_arr[] = '<meta property="article:modified_time" content="' . esc_attr(amt_iso8601_date($post->post_modified)) . '" />';
            // Author
            // If a Facebook author profile URL has been provided, it has priority,
            // Otherwise fall back to the WordPress author archive.
            $fb_author_url = get_the_author_meta('amt_facebook_author_profile_url', $post->post_author);
            if (!empty($fb_author_url)) {
                $metadata_arr[] = '<meta property="article:author" content="' . esc_url_raw($fb_author_url, array('http', 'https', 'mailto')) . '" />';
            } else {
                //$metadata_arr[] = '<meta property="article:author" content="' . esc_url_raw( get_author_posts_url( get_the_author_meta( 'ID', $post->post_author ) ) ) . '" />';
                $metadata_arr[] = '<meta property="article:author" content="' . esc_url_raw(amt_get_local_author_profile_url(get_the_author_meta('ID', $post->post_author), $options)) . '" />';
            }
            // Publisher
            // If a Facebook publisher profile URL has been provided, it has priority.
            // Otherwise fall back to the WordPress blog home url.
            if (!empty($options['social_main_facebook_publisher_profile_url'])) {
                $metadata_arr[] = '<meta property="article:publisher" content="' . esc_url_raw($options['social_main_facebook_publisher_profile_url'], array('http', 'https', 'mailto')) . '" />';
            } else {
                $metadata_arr[] = '<meta property="article:publisher" content="' . esc_url_raw(trailingslashit(get_bloginfo('url'))) . '" />';
            }
            /*
            // article:section: We use the first category as the section.
            $first_cat = amt_get_first_category($post);
            if ( ! empty( $first_cat ) ) {
                $metadata_arr[] = '<meta property="article:section" content="' . esc_attr( $first_cat ) . '" />';
            }
            */
            // article:section: We use print an ``article:section`` meta tag for each of the post's categories.
            $categories = get_the_category($post->ID);
            $categories = apply_filters('amt_post_categories_for_opengraph', $categories);
            foreach ($categories as $cat) {
                $section = trim($cat->cat_name);
                if (!empty($section) && $cat->slug != 'uncategorized') {
                    $metadata_arr[] = '<meta property="article:section" content="' . esc_attr($section) . '" />';
                }
            }
            // article:tag: Keywords are listed as post tags
            $keywords = explode(',', amt_get_content_keywords($post, $auto = true, $exclude_categories = true));
            foreach ($keywords as $tag) {
                $tag = trim($tag);
                if (!empty($tag)) {
                    $metadata_arr[] = '<meta property="article:tag" content="' . esc_attr($tag) . '" />';
                }
            }
        } elseif ($og_type == 'video.other') {
            // Dates
            $metadata_arr[] = '<meta property="video:release_date" content="' . esc_attr(amt_iso8601_date($post->post_date)) . '" />';
            // video:tag: Keywords are listed as post tags
            $keywords = explode(',', amt_get_content_keywords($post));
            foreach ($keywords as $tag) {
                $tag = trim($tag);
                if (!empty($tag)) {
                    $metadata_arr[] = '<meta property="video:tag" content="' . esc_attr($tag) . '" />';
                }
            }
        } elseif ($og_type == 'product') {
            // Extend the current metadata with properties of the Product object.
            // See:
            //  * https://developers.facebook.com/docs/reference/opengraph/object-type/product/
            //  * https://developers.facebook.com/docs/payments/product
            $metadata_arr = apply_filters('amt_product_data_opengraph', $metadata_arr, $post);
        }
    }
    // Filtering of the generated Opengraph metadata
    $metadata_arr = apply_filters('amt_opengraph_metadata_head', $metadata_arr);
    return $metadata_arr;
}
/**
 * Generates basic metadata for the head area.
 *
 */
function amt_add_basic_metadata_head($post, $attachments, $embedded_media, $options)
{
    $do_description = $options["auto_description"] == "1" ? true : false;
    $do_keywords = $options["auto_keywords"] == "1" ? true : false;
    $do_noodp_description = $options["noodp_description"] == "1" ? true : false;
    // Array to store metadata
    $metadata_arr = array();
    // Robots Meta Tag.
    $robots_content = '';
    if ($do_noodp_description && (is_front_page() || is_singular())) {
        // Add NOODP on posts and pages
        $robots_content = 'NOODP,NOYDIR';
        // Allow filtering of the robots meta tag content.
        $robots_content = apply_filters('amt_robots_data', $robots_content);
    }
    // Add a robots meta tag if its content is not empty.
    if (!empty($robots_content)) {
        $metadata_arr[] = '<meta name="robots" content="' . $robots_content . '" />';
    }
    // Default front page displaying latest posts
    if (amt_is_default_front_page()) {
        // Description and Keywords from the Add-Meta-Tags settings override
        // default behaviour.
        // Description
        if ($do_description) {
            // Use the site description from the Add-Meta-Tags settings.
            // Fall back to the blog description.
            $site_description = $options["site_description"];
            if (empty($site_description)) {
                // Alternatively, use the blog description
                // Here we sanitize the provided description for safety
                $site_description = sanitize_text_field(amt_sanitize_description(get_bloginfo('description')));
            }
            // If we have a description, use it in the description meta-tag of the front page
            if (!empty($site_description)) {
                // Note: Contains multipage information through amt_process_paged()
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($site_description)) . '" />';
            }
        }
        // Keywords
        if ($do_keywords) {
            // Use the site keywords from the Add-Meta-Tags settings.
            // Fall back to the blog categories.
            $site_keywords = $options["site_keywords"];
            if (empty($site_keywords)) {
                // Alternatively, use the blog categories
                // Here we sanitize the provided keywords for safety
                $site_keywords = sanitize_text_field(amt_sanitize_keywords(amt_get_all_categories()));
            }
            // If we have keywords, use them in the keywords meta-tag of the front page
            if (!empty($site_keywords)) {
                $metadata_arr[] = '<meta name="keywords" content="' . esc_attr($site_keywords) . '" />';
            }
        }
        // Attachments
    } elseif (is_attachment()) {
        // has to be before is_singular() since is_singular() is true for attachments.
        // Description
        if ($do_description) {
            $description = amt_get_content_description($post, $auto = $do_description);
            if (!empty($description)) {
                // Note: Contains multipage information through amt_process_paged()
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($description)) . '" />';
            }
        }
        // No keywords
        // Content pages and static pages used as "front page" and "posts page"
        // This also supports products via is_singular()
    } elseif (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
        // Description
        if ($do_description) {
            $description = amt_get_content_description($post, $auto = $do_description);
            if (!empty($description)) {
                // Note: Contains multipage information through amt_process_paged()
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($description)) . '" />';
            }
        }
        // Keywords
        if ($do_keywords) {
            $keywords = amt_get_content_keywords($post, $auto = $do_keywords);
            if (!empty($keywords)) {
                $metadata_arr[] = '<meta name="keywords" content="' . esc_attr($keywords) . '" />';
                // Static Posts Index Page
                // If no keywords have been set in the metabox and this is the static page,
                // which displayes the latest posts, use the categories of the posts in the loop.
            } elseif (amt_is_static_home()) {
                // Here we sanitize the provided keywords for safety
                $cats_from_loop = sanitize_text_field(amt_sanitize_keywords(implode(', ', amt_get_categories_from_loop())));
                if (!empty($cats_from_loop)) {
                    $metadata_arr[] = '<meta name="keywords" content="' . esc_attr($cats_from_loop) . '" />';
                }
            }
        }
        // 'news_keywords'
        $newskeywords = amt_get_post_meta_newskeywords($post->ID);
        if (!empty($newskeywords)) {
            $metadata_arr[] = '<meta name="news_keywords" content="' . esc_attr($newskeywords) . '" />';
        }
        // per post full meta tags
        $full_metatags_for_content = amt_get_post_meta_full_metatags($post->ID);
        if (!empty($full_metatags_for_content)) {
            $metadata_arr[] = html_entity_decode(stripslashes($full_metatags_for_content));
        }
        // Category based archives
    } elseif (is_category()) {
        if ($do_description) {
            // If set, the description of the category is used in the 'description' metatag.
            // Otherwise, a generic description is used.
            // Here we sanitize the provided description for safety
            $description_content = sanitize_text_field(amt_sanitize_description(category_description()));
            // Note: Contains multipage information through amt_process_paged()
            if (empty($description_content)) {
                // Add a filtered generic description.
                $generic_description = apply_filters('amt_generic_description_category_archive', __('Content filed under the %s category.', 'add-meta-tags'));
                $generic_description = sprintf($generic_description, single_cat_title($prefix = '', $display = false));
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
            } else {
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($description_content)) . '" />';
            }
        }
        if ($do_keywords) {
            // The category name alone is included in the 'keywords' metatag
            // Here we sanitize the provided keywords for safety
            $cur_cat_name = sanitize_text_field(amt_sanitize_keywords(single_cat_title($prefix = '', $display = false)));
            if (!empty($cur_cat_name)) {
                $metadata_arr[] = '<meta name="keywords" content="' . esc_attr($cur_cat_name) . '" />';
            }
        }
    } elseif (is_tag()) {
        if ($do_description) {
            // If set, the description of the tag is used in the 'description' metatag.
            // Otherwise, a generic description is used.
            // Here we sanitize the provided description for safety
            $description_content = sanitize_text_field(amt_sanitize_description(tag_description()));
            // Note: Contains multipage information through amt_process_paged()
            if (empty($description_content)) {
                // Add a filtered generic description.
                $generic_description = apply_filters('amt_generic_description_tag_archive', __('Content tagged with %s.', 'add-meta-tags'));
                $generic_description = sprintf($generic_description, single_tag_title($prefix = '', $display = false));
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
            } else {
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($description_content)) . '" />';
            }
        }
        if ($do_keywords) {
            // The tag name alone is included in the 'keywords' metatag
            // Here we sanitize the provided keywords for safety
            $cur_tag_name = sanitize_text_field(amt_sanitize_keywords(single_tag_title($prefix = '', $display = false)));
            if (!empty($cur_tag_name)) {
                $metadata_arr[] = '<meta name="keywords" content="' . esc_attr($cur_tag_name) . '" />';
            }
        }
        // Custom taxonomies - Should be after is_category() and is_tag(), as it would catch those taxonomies as well.
        // This also supports product groups via is_tax(). Only product groups that are WordPress custom taxonomies are supported.
    } elseif (is_tax()) {
        // Taxonomy term object.
        // When viewing taxonomy archives, the $post object is the taxonomy term object. Check with: var_dump($post);
        $tax_term_object = $post;
        //var_dump($tax_term_object);
        if ($do_description) {
            // If set, the description of the custom taxonomy term is used in the 'description' metatag.
            // Otherwise, a generic description is used.
            // Here we sanitize the provided description for safety
            $description_content = sanitize_text_field(amt_sanitize_description(term_description($tax_term_object->term_id, $tax_term_object->taxonomy)));
            // Note: Contains multipage information through amt_process_paged()
            if (empty($description_content)) {
                // Add a filtered generic description.
                // Construct the filter name. Template: ``amt_generic_description_TAXONOMYSLUG_archive``
                $taxonomy_description_filter_name = sprintf('amt_generic_description_%s_archive', $tax_term_object->taxonomy);
                // var_dump($taxonomy_description_filter_name);
                $generic_description = apply_filters($taxonomy_description_filter_name, __('Content filed under the %s taxonomy.', 'add-meta-tags'));
                $generic_description = sprintf($generic_description, single_term_title($prefix = '', $display = false));
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
            } else {
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($description_content)) . '" />';
            }
        }
        if ($do_keywords) {
            // The taxonomy term name alone is included in the 'keywords' metatag.
            // Here we sanitize the provided keywords for safety.
            $cur_tax_term_name = sanitize_text_field(amt_sanitize_keywords(single_term_title($prefix = '', $display = false)));
            if (!empty($cur_tax_term_name)) {
                $metadata_arr[] = '<meta name="keywords" content="' . esc_attr($cur_tax_term_name) . '" />';
            }
        }
    } elseif (is_author()) {
        // Author object
        // NOTE: Inside the author archives `$post->post_author` does not contain the author object.
        // In this case the $post (get_queried_object()) contains the author object itself.
        // We also can get the author object with the following code. Slug is what WP uses to construct urls.
        // $author = get_user_by( 'slug', get_query_var( 'author_name' ) );
        // Also, ``get_the_author_meta('....', $author)`` returns nothing under author archives.
        // Access user meta with:  $author->description, $author->user_email, etc
        // $author = get_queried_object();
        $author = $post;
        // If a bio has been set in the user profile, use it in the description metatag of the
        // first page of the author archive *ONLY*. The other pages of the author archive use a generic description.
        // This happens because the 1st page of the author archive is considered the profile page
        // by the other metadata modules.
        // Otherwise use a generic meta tag.
        if ($do_description) {
            // Here we sanitize the provided description for safety
            $author_description = sanitize_text_field(amt_sanitize_description($author->description));
            if (empty($author_description) || is_paged()) {
                // Note: Contains multipage information through amt_process_paged()
                // Add a filtered generic description.
                $generic_description = apply_filters('amt_generic_description_author_archive', __('Content published by %s.', 'add-meta-tags'));
                $generic_description = sprintf($generic_description, $author->display_name);
                $metadata_arr[] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
            } else {
                $metadata_arr[] = '<meta name="description" content="' . esc_attr($author_description) . '" />';
            }
        }
        // For the keywords metatag use the categories of the posts the author has written and are displayed in the current page.
        if ($do_keywords) {
            // Here we sanitize the provided keywords for safety
            $cats_from_loop = sanitize_text_field(amt_sanitize_keywords(implode(', ', amt_get_categories_from_loop())));
            if (!empty($cats_from_loop)) {
                $metadata_arr[] = '<meta name="keywords" content="' . esc_attr($cats_from_loop) . '" />';
            }
        }
    }
    // Add site wide meta tags
    if (!empty($options["site_wide_meta"])) {
        $metadata_arr[] = html_entity_decode(stripslashes($options["site_wide_meta"]));
    }
    // On every page print the copyright head link
    if (!empty($options["copyright_url"])) {
        $metadata_arr[] = '<link rel="copyright" type="text/html" title="' . esc_attr(get_bloginfo('name')) . ' Copyright Information" href="' . esc_url_raw($options["copyright_url"]) . '" />';
    }
    // Filtering of the generated basic metadata
    $metadata_arr = apply_filters('amt_basic_metadata_head', $metadata_arr);
    return $metadata_arr;
}
示例#3
0
/**
 * Generates basic metadata for the head area.
 *
 */
function amt_add_basic_metadata_head($post, $attachments, $embedded_media, $options)
{
    if (apply_filters('amt_exclude_basic_metadata', false)) {
        return array();
    }
    $do_description = $options["auto_description"] == "1" ? true : false;
    $do_keywords = $options["auto_keywords"] == "1" ? true : false;
    $do_noodp_description = $options["noodp_description"] == "1" ? true : false;
    // Array to store metadata
    $metadata_arr = array();
    // Pre-processing
    // Store hreflang links in array
    $hreflang_links_arr = array();
    // Store base robots options
    $robots_options = array();
    if ($do_noodp_description && (is_front_page() || is_singular() || is_category() || is_tag() || is_tax() || is_author())) {
        // Add NOODP on posts and pages
        $robots_options[] = 'noodp';
        $robots_options[] = 'noydir';
    }
    // Store full meta tags (site wide and post specific)
    // Add site wide meta tags
    $full_metatags_as_string = '';
    if (!empty($options['site_wide_meta'])) {
        $full_metatags_for_site = html_entity_decode(stripslashes($options['site_wide_meta']));
        $full_metatags_as_string .= apply_filters('amt_full_metatags_site', $full_metatags_for_site);
        $full_metatags_as_string .= PHP_EOL;
    }
    // Full meta tags
    if (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
        // per post full meta tags
        $full_metatags_for_content = amt_get_post_meta_full_metatags($post->ID);
        $full_metatags_for_content = html_entity_decode(stripslashes($full_metatags_for_content));
        $full_metatags_as_string .= apply_filters('amt_full_metatags_post', $full_metatags_for_content);
    } elseif (is_category() || is_tag() || is_tax()) {
        // Term specific full meta tags ($post is a term object)
        $full_metatags_for_term = amt_get_term_meta_full_metatags($post->term_id);
        $full_metatags_for_term = html_entity_decode(stripslashes($full_metatags_for_term));
        $full_metatags_as_string .= apply_filters('amt_full_metatags_term', $full_metatags_for_term);
    } elseif (is_author()) {
        // User specific full meta tags ($post is a user object)
        $full_metatags_for_user = amt_get_user_meta_full_metatags($post->ID);
        $full_metatags_for_user = html_entity_decode(stripslashes($full_metatags_for_user));
        $full_metatags_as_string .= apply_filters('amt_full_metatags_term', $full_metatags_for_user);
    }
    // Sanitize
    //$full_metatags_as_string = esc_textarea( wp_kses( $full_metatags_as_string, amt_get_allowed_html_kses() ) );
    $full_metatags_as_string = wp_kses($full_metatags_as_string, amt_get_allowed_html_kses());
    // Make array of full meta tags
    $full_meta_tags = preg_split('#\\R#', $full_metatags_as_string, NULL, PREG_SPLIT_NO_EMPTY);
    // Process
    if (apply_filters('amt_full_metatags_processor_enable', true)) {
        // Store processed meta tags here
        $processed_full_meta_tags = array();
        // Field substitutions currently take place only on content pages.
        // TODO: See if this can be expanded to terms, authors.
        // Store the post's custom fields
        $custom_fields = null;
        // Store the post object's custom fields.
        //
        if (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
            // Get an array of all custom fields names of the post object.
            $custom_fields = get_post_custom_keys($post->ID);
        }
        // Iterate over full meta tags
        foreach ($full_meta_tags as $single_meta_tag) {
            // Note: Field value substitutions take place first, outside the elseif clauses.
            // Process substitutions of special notation with data from Custom Fields
            // Supported special notation:
            //   [field=Field Name]
            // Notes:
            // - 'Field Name' is the name of custom field.
            // - If the custom field with name 'Field Name' does not exist, the meta tag
            //   that contains it is omitted.
            // - If the value of the field is an empty string, then the substitution
            //   takes place normally.
            //
            // The regex pattern fo our special notation.
            $special_notation_pattern = '#(?:\\[field\\=)([^\\]]+)(?:\\])#';
            // The following covers content pages, as $custom_fields is only set on content pages. See above.
            if (!empty($custom_fields) && isset($post->ID)) {
                // This also assumes that we have a post object since custom fields
                // are set only on content pages, otherwise it is null.
                // Check for special notation
                if (preg_match($special_notation_pattern, $single_meta_tag, $matches)) {
                    //var_dump($matches);
                    // If the field name of the special notation does not match
                    // any custom field name, omit the meta tag as per the rules above.
                    if (!in_array($matches[1], $custom_fields)) {
                        continue;
                    }
                    // Since there is special notation and the field name from the special
                    // notation exists in the $custom_fields array, iterate over the available
                    // custom fields and perform the substitutions.
                    foreach ($custom_fields as $custom_field) {
                        // Check if it matches the field name of the special notation
                        if ($custom_field == $matches[1]) {
                            // Fetch the custom field's value
                            $field_value = get_post_meta($post->ID, $custom_field, true);
                            // Sanitize value
                            // TODO: this can be a problem depending on the value and the used sanitization function.
                            $field_value = esc_attr(sanitize_text_field($field_value));
                            // Perform the substitution even if the the value is an empty string as per the rules above
                            $single_meta_tag = str_replace(sprintf('[field=%s]', $custom_field), $field_value, $single_meta_tag);
                        }
                    }
                }
            } else {
                // In any other case, just remove the meta tags which contain the special notation.
                if (preg_match($special_notation_pattern, $single_meta_tag, $tmp)) {
                    continue;
                }
            }
            // Process the PAGEINFO variable.
            // If the current page is the 1st page of any archive or of multipage content,
            // PAGEINFO is just stripped. For subsequent pages of archives or multipage
            // content, PAGEINFO is replaced with page based path (page/N/ for archives or N/ for multipage content)
            //
            // For paginated archives or paginated main page with latest posts.
            $has_paging_info = false;
            if (is_paged()) {
                $paged = get_query_var('paged');
                // paged
                if ($paged && $paged >= 2) {
                    $single_meta_tag = str_replace('PAGEINFO', 'page/' . $paged . '/', $single_meta_tag);
                    $has_paging_info = true;
                }
                // For a Post or Page that has been divided into pages using the <!--nextpage--> QuickTag
            } else {
                $paged = get_query_var('page');
                // page
                if ($paged && $paged >= 2) {
                    $single_meta_tag = str_replace('PAGEINFO', $paged . '/', $single_meta_tag);
                    $has_paging_info = true;
                }
            }
            // If this is not paged, strip PAGEINFO
            if ($has_paging_info === false) {
                $single_meta_tag = str_replace('PAGEINFO', '', $single_meta_tag);
            }
            // Process custom canonical link
            // If a rel="canonical" meta tags exists, we deactivate WordPress' 'rel_canonical' action,
            // Since it is assumed that a custom canonical link has been added.
            //if ( preg_match( '# rel="canonical" #', $post_full_meta_tags, $tmp ) ) {
            if (strpos($single_meta_tag, ' rel="canonical" ') !== false) {
                // Remove default WordPress action
                remove_action('wp_head', 'rel_canonical');
            } elseif (strpos($single_meta_tag, ' name="robots" ') !== false) {
                if (preg_match('# content="([^"]+)" #', $single_meta_tag, $matches)) {
                    $tmp_robots_opts = explode(',', $matches[1]);
                    foreach ($tmp_robots_opts as $single_robots_option) {
                        $single_robots_option_cleaned = strtolower(trim($single_robots_option));
                        if (!empty($single_robots_option_cleaned)) {
                            $robots_options[] = $single_robots_option_cleaned;
                        }
                    }
                }
                // We simply collect options. Do not add any robots meta tags to the processed meta tags array.
                continue;
            } elseif (strpos($single_meta_tag, ' hreflang="') !== false) {
                // Simply add to the hreflang links array for later processing
                $hreflang_links_arr[] = $single_meta_tag;
                // We simply collect hreflang links for later processing. Do not add them to the processed meta tags array.
                continue;
            }
            // If we have reached here, add the meta tags to the array with processed meta tags.
            $processed_full_meta_tags[] = $single_meta_tag;
        }
    } else {
        // Full meta tags processor not enabled
        $processed_full_meta_tags = $full_meta_tags;
    }
    //var_dump($full_meta_tags);
    //var_dump($processed_full_meta_tags);
    // Add Meta Tags
    // Add a robots meta tag if robots options exist.
    // Backwards compatible filter. TODO: This is deprecated. Needs to be deleted after a while.
    $old_options_as_string = apply_filters('amt_robots_data', '');
    if (!empty($old_options_as_string)) {
        foreach (explode(',', $old_options_as_string) as $single_robots_option) {
            $single_robots_option_cleaned = strtolower(trim($single_robots_option));
            if (!empty($single_robots_option_cleaned)) {
                $robots_options[] = $single_robots_option_cleaned;
            }
        }
    }
    // Add robot_options filtering
    $robots_options = apply_filters('amt_robots_options', $robots_options);
    if (version_compare(PHP_VERSION, '5.3', '<')) {
        // The flag is not supported
        $robots_options = array_unique($robots_options);
    } else {
        $robots_options = array_unique($robots_options, SORT_STRING);
    }
    if (!empty($robots_options)) {
        $metadata_arr['basic:robots'] = '<meta name="robots" content="' . esc_attr(implode(',', $robots_options)) . '" />';
    }
    // Add full meta tags
    // Merge meta tags
    $processed_full_meta_tags = apply_filters('amt_full_metatags_processed', $processed_full_meta_tags);
    if (!empty($processed_full_meta_tags)) {
        $metadata_arr = array_merge($metadata_arr, $processed_full_meta_tags);
    }
    // Add copyright link
    // On every page print the copyright head link
    $copyright_url = amt_get_site_copyright_url($options);
    //if ( empty($copyright_url)) {
    //    $copyright_url = trailingslashit( get_bloginfo('url') );
    //}
    if (!empty($copyright_url)) {
        $metadata_arr['basic:copyright'] = '<link rel="copyright" type="text/html" title="' . esc_attr(get_bloginfo('name')) . ' ' . __('copyright information', 'add-meta-tags') . '" href="' . esc_url($copyright_url) . '" />';
    }
    // hreflang link element
    // This section also expects an array of extra hreflang links that may have
    // been collected from the full meta tags boxes.
    if ($options['generate_hreflang_links'] == '1') {
        if (is_singular()) {
            $locale = amt_get_language_content($options, $post);
            $hreflang = amt_get_the_hreflang($locale, $options);
            $hreflang_url = amt_get_permalink_for_multipage($post);
        } else {
            $locale = amt_get_language_site($options);
            $hreflang = amt_get_the_hreflang($locale, $options);
            $hreflang_url = '';
            if (amt_is_default_front_page()) {
                $hreflang_url = trailingslashit(get_bloginfo('url'));
            } elseif (is_category() || is_tag() || is_tax()) {
                // $post is a term object
                $hreflang_url = get_term_link($post);
            } elseif (is_author()) {
                // $post is an author object
                $hreflang_url = get_author_posts_url($post->ID);
            } elseif (is_year()) {
                $archive_year = get_the_time('Y');
                $hreflang_url = get_year_link($archive_year);
            } elseif (is_month()) {
                $archive_year = get_the_time('Y');
                $archive_month = get_the_time('m');
                $hreflang_url = get_month_link($archive_year, $archive_month);
            } elseif (is_day()) {
                $archive_year = get_the_time('Y');
                $archive_month = get_the_time('m');
                $archive_day = get_the_time('d');
                $hreflang_url = get_day_link($archive_year, $archive_month, $archive_day);
            }
            // If paged information is available
            if (is_paged()) {
                //$hreflang_url = trailingslashit( $hreflang_url ) . get_query_var('paged') . '/';
                $hreflang_url = get_pagenum_link(get_query_var('paged'));
            }
        }
        // hreflang links array
        $hreflang_arr = array();
        // Add link element
        if (!empty($hreflang) && !empty($hreflang_url)) {
            $hreflang_arr[] = '<link rel="alternate" hreflang="' . esc_attr($hreflang) . '" href="' . esc_url_raw($hreflang_url) . '" />';
        }
        // Add extra hreflang links that have been collected from the full meta tags boxes
        if (!empty($hreflang_links_arr)) {
            $hreflang_arr = array_merge($hreflang_arr, $hreflang_links_arr);
        }
        // Allow filtering of the hreflang array
        $hreflang_arr = apply_filters('amt_hreflang_links', $hreflang_arr);
        // Add to to metadata array
        foreach ($hreflang_arr as $hreflang_link) {
            if (preg_match('# hreflang="([^"]+)" #', $hreflang_link, $matches)) {
                $metadata_arr['basic:hreflang:' . $matches[1]] = $hreflang_link;
            }
        }
    }
    // Basic Meta Tags
    // Custom content override
    if (amt_is_custom($post, $options)) {
        // Return metadata with:
        // add_filter( 'amt_custom_metadata_basic', 'my_function', 10, 5 );
        // Return an array of meta tags. Array item format: ['key_can_be_whatever'] = '<meta name="foo" content="bar" />'
        $metadata_arr = apply_filters('amt_custom_metadata_basic', $metadata_arr, $post, $options, $attachments, $embedded_media);
        // Default front page displaying latest posts
    } elseif (amt_is_default_front_page()) {
        // Description and Keywords from the Add-Meta-Tags settings override
        // default behaviour.
        // Description
        if ($do_description) {
            // Use the site description from the Add-Meta-Tags settings.
            // Fall back to the blog description.
            $site_description = amt_get_site_description($options);
            if (empty($site_description)) {
                // Alternatively, use the blog description
                // Here we sanitize the provided description for safety
                $site_description = sanitize_text_field(amt_sanitize_description(get_bloginfo('description')));
            }
            // If we have a description, use it in the description meta-tag of the front page
            if (!empty($site_description)) {
                // Note: Contains multipage information through amt_process_paged()
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($site_description)) . '" />';
            }
        }
        // Keywords
        if ($do_keywords) {
            // Use the site keywords from the Add-Meta-Tags settings.
            // Fall back to the blog categories.
            $site_keywords = amt_get_site_keywords($options);
            if (empty($site_keywords)) {
                // Alternatively, use the blog categories
                // Here we sanitize the provided keywords for safety
                $site_keywords = sanitize_text_field(amt_sanitize_keywords(amt_get_all_categories()));
            }
            // If we have keywords, use them in the keywords meta-tag of the front page
            if (!empty($site_keywords)) {
                $metadata_arr['basic:keywords'] = '<meta name="keywords" content="' . esc_attr($site_keywords) . '" />';
            }
        }
        // Attachments
    } elseif (is_attachment()) {
        // has to be before is_singular() since is_singular() is true for attachments.
        // Description
        if ($do_description) {
            $description = amt_get_content_description($post, $auto = $do_description);
            if (!empty($description)) {
                // Note: Contains multipage information through amt_process_paged()
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($description)) . '" />';
            }
        }
        // No keywords
        // Content pages and static pages used as "front page" and "posts page"
        // This also supports products via is_singular()
    } elseif (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
        // Description
        if ($do_description) {
            $description = amt_get_content_description($post, $auto = $do_description);
            if (!empty($description)) {
                // Note: Contains multipage information through amt_process_paged()
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($description)) . '" />';
            }
        }
        // Keywords
        if ($do_keywords) {
            $keywords = amt_get_content_keywords($post, $auto = $do_keywords);
            if (!empty($keywords)) {
                $metadata_arr['basic:keywords'] = '<meta name="keywords" content="' . esc_attr($keywords) . '" />';
                // Static Posts Index Page
                // If no keywords have been set in the metabox and this is the static page,
                // which displayes the latest posts, use the categories of the posts in the loop.
            } elseif (amt_is_static_home()) {
                // Here we sanitize the provided keywords for safety
                $cats_from_loop = sanitize_text_field(amt_sanitize_keywords(implode(', ', amt_get_categories_from_loop())));
                if (!empty($cats_from_loop)) {
                    $metadata_arr['basic:keywords'] = '<meta name="keywords" content="' . esc_attr($cats_from_loop) . '" />';
                }
            }
        }
        // 'news_keywords'
        $newskeywords = amt_get_post_meta_newskeywords($post->ID);
        if (!empty($newskeywords)) {
            $metadata_arr['basic:news_keywords'] = '<meta name="news_keywords" content="' . esc_attr($newskeywords) . '" />';
        }
        // Category based archives
    } elseif (is_category()) {
        if ($do_description) {
            // If set, the description of the category is used in the 'description' metatag.
            // Otherwise, a generic description is used.
            // Here we sanitize the provided description for safety
            $description_content = sanitize_text_field(amt_sanitize_description(category_description()));
            // Note: Contains multipage information through amt_process_paged()
            if (empty($description_content)) {
                // Add a filtered generic description.
                $generic_description = apply_filters('amt_generic_description_category_archive', __('Content filed under the %s category.', 'add-meta-tags'));
                $generic_description = sprintf($generic_description, single_cat_title($prefix = '', $display = false));
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
            } else {
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($description_content)) . '" />';
            }
        }
        if ($do_keywords) {
            // The category name alone is included in the 'keywords' metatag
            // Here we sanitize the provided keywords for safety
            $cur_cat_name = sanitize_text_field(amt_sanitize_keywords(single_cat_title($prefix = '', $display = false)));
            if (!empty($cur_cat_name)) {
                $metadata_arr['basic:keywords'] = '<meta name="keywords" content="' . esc_attr($cur_cat_name) . '" />';
            }
        }
    } elseif (is_tag()) {
        if ($do_description) {
            // If set, the description of the tag is used in the 'description' metatag.
            // Otherwise, a generic description is used.
            // Here we sanitize the provided description for safety
            $description_content = sanitize_text_field(amt_sanitize_description(tag_description()));
            // Note: Contains multipage information through amt_process_paged()
            if (empty($description_content)) {
                // Add a filtered generic description.
                $generic_description = apply_filters('amt_generic_description_tag_archive', __('Content tagged with %s.', 'add-meta-tags'));
                $generic_description = sprintf($generic_description, single_tag_title($prefix = '', $display = false));
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
            } else {
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($description_content)) . '" />';
            }
        }
        if ($do_keywords) {
            // The tag name alone is included in the 'keywords' metatag
            // Here we sanitize the provided keywords for safety
            $cur_tag_name = sanitize_text_field(amt_sanitize_keywords(single_tag_title($prefix = '', $display = false)));
            if (!empty($cur_tag_name)) {
                $metadata_arr['basic:keywords'] = '<meta name="keywords" content="' . esc_attr($cur_tag_name) . '" />';
            }
        }
        // Custom taxonomies - Should be after is_category() and is_tag(), as it would catch those taxonomies as well.
        // This also supports product groups via is_tax(). Only product groups that are WordPress custom taxonomies are supported.
    } elseif (is_tax()) {
        // Taxonomy term object.
        // When viewing taxonomy archives, the $post object is the taxonomy term object. Check with: var_dump($post);
        $tax_term_object = $post;
        //var_dump($tax_term_object);
        if ($do_description) {
            // If set, the description of the custom taxonomy term is used in the 'description' metatag.
            // Otherwise, a generic description is used.
            // Here we sanitize the provided description for safety
            $description_content = sanitize_text_field(amt_sanitize_description(term_description($tax_term_object->term_id, $tax_term_object->taxonomy)));
            // Note: Contains multipage information through amt_process_paged()
            if (empty($description_content)) {
                // Add a filtered generic description.
                // Construct the filter name. Template: ``amt_generic_description_TAXONOMYSLUG_archive``
                $taxonomy_description_filter_name = sprintf('amt_generic_description_%s_archive', $tax_term_object->taxonomy);
                // var_dump($taxonomy_description_filter_name);
                $generic_description = apply_filters($taxonomy_description_filter_name, __('Content filed under the %s taxonomy.', 'add-meta-tags'));
                $generic_description = sprintf($generic_description, single_term_title($prefix = '', $display = false));
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
            } else {
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($description_content)) . '" />';
            }
        }
        if ($do_keywords) {
            // The taxonomy term name alone is included in the 'keywords' metatag.
            // Here we sanitize the provided keywords for safety.
            $cur_tax_term_name = sanitize_text_field(amt_sanitize_keywords(single_term_title($prefix = '', $display = false)));
            if (!empty($cur_tax_term_name)) {
                $metadata_arr['basic:keywords'] = '<meta name="keywords" content="' . esc_attr($cur_tax_term_name) . '" />';
            }
        }
    } elseif (is_author()) {
        // Author object
        // NOTE: Inside the author archives `$post->post_author` does not contain the author object.
        // In this case the $post (get_queried_object()) contains the author object itself.
        // We also can get the author object with the following code. Slug is what WP uses to construct urls.
        // $author = get_user_by( 'slug', get_query_var( 'author_name' ) );
        // Also, ``get_the_author_meta('....', $author)`` returns nothing under author archives.
        // Access user meta with:  $author->description, $author->user_email, etc
        // $author = get_queried_object();
        $author = $post;
        // If a bio has been set in the user profile, use it in the description metatag of the
        // first page of the author archive *ONLY*. The other pages of the author archive use a generic description.
        // This happens because the 1st page of the author archive is considered the profile page
        // by the other metadata modules.
        // Otherwise use a generic meta tag.
        if ($do_description) {
            // Here we sanitize the provided description for safety
            $author_description = sanitize_text_field(amt_sanitize_description($author->description));
            if (empty($author_description) || is_paged()) {
                // Note: Contains multipage information through amt_process_paged()
                // Add a filtered generic description.
                $generic_description = apply_filters('amt_generic_description_author_archive', __('Content published by %s.', 'add-meta-tags'));
                $generic_description = sprintf($generic_description, $author->display_name);
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
            } else {
                $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr($author_description) . '" />';
            }
        }
        // For the keywords metatag use the categories of the posts the author has written and are displayed in the current page.
        if ($do_keywords) {
            // Here we sanitize the provided keywords for safety
            $cats_from_loop = sanitize_text_field(amt_sanitize_keywords(implode(', ', amt_get_categories_from_loop())));
            if (!empty($cats_from_loop)) {
                $metadata_arr['basic:keywords'] = '<meta name="keywords" content="' . esc_attr($cats_from_loop) . '" />';
            }
        }
        // Custom Post Type Archive
    } elseif (is_post_type_archive()) {
        // Custom post type object.
        // When viewing custom post type archives, the $post object is the custom post type object. Check with: var_dump($post);
        $post_type_object = $post;
        //var_dump($post_type_object);
        if ($do_description) {
            // Description
            // Note: Contains multipage information through amt_process_paged()
            // Add a filtered generic description.
            // Construct the filter name. Template: ``amt_generic_description_posttype_POSTTYPESLUG_archive``
            $custom_post_type_description_filter_name = sprintf('amt_generic_description_posttype_%s_archive', $post_type_object->name);
            // var_dump($custom_post_type_description_filter_name);
            // Generic description
            $generic_description = apply_filters($custom_post_type_description_filter_name, __('%s archive.', 'add-meta-tags'));
            // Final generic description
            $generic_description = sprintf($generic_description, post_type_archive_title($prefix = '', $display = false));
            $metadata_arr['basic:description'] = '<meta name="description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
        }
        // For the keywords metatag use the categories of the posts that are listed in the current page.
        if ($do_keywords) {
            // Here we sanitize the provided keywords for safety
            $cats_from_loop = sanitize_text_field(amt_sanitize_keywords(implode(', ', amt_get_categories_from_loop())));
            if (!empty($cats_from_loop)) {
                $metadata_arr['basic:keywords'] = '<meta name="keywords" content="' . esc_attr($cats_from_loop) . '" />';
            }
        }
    }
    // Filtering of the generated basic metadata
    $metadata_arr = apply_filters('amt_basic_metadata_head', $metadata_arr);
    return $metadata_arr;
}
/**
 * Generate Twitter Cards metadata for the content pages.
 */
function amt_add_twitter_cards_metadata_head($post, $attachments, $embedded_media, $options)
{
    if (apply_filters('amt_exclude_twitter_cards_metadata', false)) {
        return array();
    }
    $do_auto_twitter = $options["auto_twitter"] == "1" ? true : false;
    if (!$do_auto_twitter) {
        return array();
    }
    $metadata_arr = array();
    // Custom content override
    if (amt_is_custom($post, $options)) {
        // Return metadata with:
        // add_filter( 'amt_custom_metadata_twitter_cards', 'my_function', 10, 5 );
        // Return an array of meta tags. Array item format: ['key_can_be_whatever'] = '<meta name="foo" content="bar" />'
        $metadata_arr = apply_filters('amt_custom_metadata_twitter_cards', $metadata_arr, $post, $options, $attachments, $embedded_media);
        return $metadata_arr;
    }
    // Front page and archives
    if (!is_singular() && !amt_is_static_home() && !amt_is_static_front_page() || amt_is_default_front_page() || is_category() || is_tag() || is_tax() || is_post_type_archive()) {
        // Note1: is_front_page() is used for the case in which a static page is used as the front page.
        // Note2: product groups should pass the is_tax() validation, so no need for
        // amt_is_product_group(). We do not support other product groups.
        // Default front page containing latest posts
        // Add a basic Twitter Card to the default home page that contains latest posts.
        // If static pages are used as the front page or the latest-posts page,
        // then they are treated as content and are processed below.
        if (amt_is_default_front_page()) {
            // Generate the card only if a publisher username has been set in the publisher settings
            if (!empty($options['social_main_twitter_publisher_username'])) {
                // Type
                $metadata_arr[] = '<meta name="twitter:card" content="' . amt_get_default_twitter_card_type($options) . '" />';
                // Creator
                $metadata_arr[] = '<meta name="twitter:creator" content="@' . esc_attr($options['social_main_twitter_publisher_username']) . '" />';
                // Publisher
                $metadata_arr[] = '<meta name="twitter:site" content="@' . esc_attr($options['social_main_twitter_publisher_username']) . '" />';
                // Title
                // Note: Contains multipage information
                $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
                // Site description - Note: Contains multipage information through amt_process_paged()
                $site_description = amt_get_site_description($options);
                if (empty($site_description)) {
                    $site_description = get_bloginfo('description');
                }
                if (!empty($site_description)) {
                    $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($site_description)) . '" />';
                }
                // Image. Use the default image (if set).
                $image_data = amt_get_default_image_data();
                if (!empty($image_data)) {
                    $image_size = apply_filters('amt_image_size_index', 'full');
                    $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                    if (!empty($image_meta_tags)) {
                        $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                    }
                }
                //$image_url = apply_filters( 'amt_twitter_cards_image_url_index', $options["default_image_url"] );
                //$metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $image_url ) . '" />';
            }
            // Taxonomy archives
            // Note: product groups should pass the is_tax() validation, so no need for
            // amt_is_product_group(). We do not support other product groups.
        } elseif (is_category() || is_tag() || is_tax()) {
            // Taxonomy term object.
            // When viewing taxonomy archives, the $post object is the taxonomy term object. Check with: var_dump($post);
            $tax_term_object = $post;
            //var_dump($tax_term_object);
            // Generate the card only if a publisher username has been set in the publisher settings
            if (!empty($options['social_main_twitter_publisher_username'])) {
                // Type
                $metadata_arr[] = '<meta name="twitter:card" content="' . amt_get_default_twitter_card_type($options) . '" />';
                // Creator
                $metadata_arr[] = '<meta name="twitter:creator" content="@' . esc_attr($options['social_main_twitter_publisher_username']) . '" />';
                // Publisher
                $metadata_arr[] = '<meta name="twitter:site" content="@' . esc_attr($options['social_main_twitter_publisher_username']) . '" />';
                // Title
                // Note: Contains multipage information
                $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
                // Description
                // If set, the description of the custom taxonomy term is used in the 'description' metatag.
                // Otherwise, a generic description is used.
                // Here we sanitize the provided description for safety
                $description_content = sanitize_text_field(amt_sanitize_description(term_description($tax_term_object->term_id, $tax_term_object->taxonomy)));
                // Note: Contains multipage information through amt_process_paged()
                if (empty($description_content)) {
                    // Add a filtered generic description.
                    // Filter name
                    if (is_category()) {
                        $generic_description = apply_filters('amt_generic_description_category_archive', __('Content filed under the %s category.', 'add-meta-tags'));
                    } elseif (is_tag()) {
                        $generic_description = apply_filters('amt_generic_description_tag_archive', __('Content tagged with %s.', 'add-meta-tags'));
                    } elseif (is_tax()) {
                        // Construct the filter name. Template: ``amt_generic_description_TAXONOMYSLUG_archive``
                        $taxonomy_description_filter_name = sprintf('amt_generic_description_%s_archive', $tax_term_object->taxonomy);
                        // var_dump($taxonomy_description_filter_name);
                        // Generic description
                        $generic_description = apply_filters($taxonomy_description_filter_name, __('Content filed under the %s taxonomy.', 'add-meta-tags'));
                    }
                    // Final generic description
                    $generic_description = sprintf($generic_description, single_term_title($prefix = '', $display = false));
                    $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
                } else {
                    $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($description_content)) . '" />';
                }
                // Image
                // Use an image from the 'Global image override' field.
                // Otherwise, use a user defined image via filter.
                // Otherwise use default image.
                $image_data = amt_get_image_data(amt_get_term_meta_image_url($tax_term_object->term_id));
                if (!empty($image_data)) {
                    $image_size = apply_filters('amt_image_size_index', 'full');
                    $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                    if (!empty($image_meta_tags)) {
                        $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                    }
                } else {
                    // First filter using a term/taxonomy agnostic filter name.
                    $taxonomy_image_url = apply_filters('amt_taxonomy_force_image_url', '', $tax_term_object);
                    if (empty($taxonomy_image_url)) {
                        // Second filter (term/taxonomy dependent).
                        // Construct the filter name. Template: ``amt_taxonomy_image_url_TAXONOMYSLUG_TERMSLUG``
                        $taxonomy_image_url_filter_name = sprintf('amt_taxonomy_image_url_%s_%s', $tax_term_object->taxonomy, $tax_term_object->slug);
                        //var_dump($taxonomy_image_url_filter_name);
                        // The default image, if set, is used by default.
                        $taxonomy_image_url = apply_filters($taxonomy_image_url_filter_name, $options["default_image_url"]);
                    }
                    if (!empty($taxonomy_image_url)) {
                        $image_data = amt_get_image_data($taxonomy_image_url);
                        if (!empty($image_data)) {
                            $image_size = apply_filters('amt_image_size_index', 'full');
                            $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                            if (!empty($image_meta_tags)) {
                                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                            }
                        }
                        //$metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $taxonomy_image_url ) . '" />';
                    }
                }
            }
            // Custom Post type Archives
        } elseif (is_post_type_archive()) {
            // Custom post type object.
            // When viewing custom post type archives, the $post object is the custom post type object. Check with: var_dump($post);
            $post_type_object = $post;
            //var_dump($post_type_object);
            // Generate the card only if a publisher username has been set in the publisher settings
            if (!empty($options['social_main_twitter_publisher_username'])) {
                // Type
                $metadata_arr[] = '<meta name="twitter:card" content="' . amt_get_default_twitter_card_type($options) . '" />';
                // Creator
                $metadata_arr[] = '<meta name="twitter:creator" content="@' . esc_attr($options['social_main_twitter_publisher_username']) . '" />';
                // Publisher
                $metadata_arr[] = '<meta name="twitter:site" content="@' . esc_attr($options['social_main_twitter_publisher_username']) . '" />';
                // Title
                // Note: Contains multipage information
                $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
                // Description
                // Note: Contains multipage information through amt_process_paged()
                // Add a filtered generic description.
                // Construct the filter name. Template: ``amt_generic_description_posttype_POSTTYPESLUG_archive``
                $custom_post_type_description_filter_name = sprintf('amt_generic_description_posttype_%s_archive', $post_type_object->name);
                // var_dump($custom_post_type_description_filter_name);
                // Generic description
                $generic_description = apply_filters($custom_post_type_description_filter_name, __('%s archive.', 'add-meta-tags'));
                // Final generic description
                $generic_description = sprintf($generic_description, post_type_archive_title($prefix = '', $display = false));
                $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($generic_description)) . '" />';
                // Image
                // Use a user defined image via filter. Otherwise use default image.
                // First filter using a term/taxonomy agnostic filter name.
                $posttype_image_url = apply_filters('amt_posttype_force_image_url', '', $post_type_object);
                if (empty($posttype_image_url)) {
                    // Second filter (post type dependent).
                    // Construct the filter name. Template: ``amt_posttype_image_url_POSTTYPESLUG``
                    $posttype_image_url_filter_name = sprintf('amt_posttype_image_url_%s', $post_type_object->name);
                    //var_dump($posttype_image_url_filter_name);
                    // The default image, if set, is used by default.
                    $posttype_image_url = apply_filters($posttype_image_url_filter_name, $options["default_image_url"]);
                }
                if (!empty($posttype_image_url)) {
                    $image_data = amt_get_image_data($posttype_image_url);
                    if (!empty($image_data)) {
                        $image_size = apply_filters('amt_image_size_index', 'full');
                        $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                        if (!empty($image_meta_tags)) {
                            $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                        }
                    }
                    //$metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $posttype_image_url ) . '" />';
                }
            }
        }
        return $metadata_arr;
    }
    // Further check (required in some reported cases)
    // Go no further if the content type does not validate is_singular().
    if (!is_singular()) {
        return array();
    }
    // Products
    // A 'product' Twitter Card is generated. See: https://dev.twitter.com/cards/types/product
    if (amt_is_product()) {
        // Type
        $metadata_arr[] = '<meta name="twitter:card" content="product" />';
        // Author and Publisher
        $metadata_arr = array_merge($metadata_arr, amt_get_twitter_cards_author_publisher_metatags($options, $post));
        // Title
        // Note: Contains multipage information
        //$metadata_arr[] = '<meta name="twitter:title" content="' . esc_attr( amt_process_paged( strip_tags( get_the_title($post->ID) ) ) ) . '" />';
        $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // Description - We use the description defined by Add-Meta-Tags
        // Note: Contains multipage information through amt_process_paged()
        $content_desc = amt_get_content_description($post);
        if (!empty($content_desc)) {
            $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($content_desc)) . '" />';
        }
        // Image
        // Use the featured image or the default image as a fallback.
        // Set to true if image meta tags have been added to the card, so that it does not
        // search for any more images.
        $image_metatags_added = false;
        // First check if a global image override URL has been entered.
        // If yes, use this image URL and override all other images.
        $image_data = amt_get_image_data(amt_get_post_meta_image_url($post->ID));
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_product', 'full');
            $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
            //$global_image_override_url = amt_get_post_meta_image_url($post->ID);
            //if ( $image_metatags_added === false && ! empty( $global_image_override_url ) ) {
            //    $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $global_image_override_url ) . '" />';
            // Images have been found.
            $image_metatags_added = true;
        }
        // Set the image size to use
        $image_size = apply_filters('amt_image_size_product', 'full');
        // If the content has a featured image, then we use it.
        if ($image_metatags_added === false && function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
            $main_size_meta = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $image_size);
            $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw($main_size_meta[0]) . '" />';
            if (apply_filters('amt_extended_image_tags', true)) {
                $metadata_arr[] = '<meta name="twitter:image:width" content="' . esc_attr($main_size_meta[1]) . '" />';
                $metadata_arr[] = '<meta name="twitter:image:height" content="' . esc_attr($main_size_meta[2]) . '" />';
            }
            // Images have been found.
            $image_metatags_added = true;
        }
        // If an image is still missing, then use the default image (if set).
        if ($image_metatags_added === false) {
            $image_data = amt_get_default_image_data();
            if (!empty($image_data)) {
                //$image_size = apply_filters( 'amt_image_size_index', 'full' );
                $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                if (!empty($image_meta_tags)) {
                    $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                }
            }
            //$metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $options["default_image_url"] ) . '" />';
        }
        //
        // The Product Twitter Card needs to be extended with the following required
        // in order to be valid: label1, data1, label2, data2
        //
        // For instance:
        //<meta name="twitter:label1" content="Genre">
        //<meta name="twitter:data1" content="Classic Rock">
        //<meta name="twitter:label2" content="Location">
        //<meta name="twitter:data2" content="National">
        //
        // The following filter is provided.
        // Filtering of the generated Twitter Cards metadata. $post is also passed.
        $metadata_arr = apply_filters('amt_product_data_twitter_cards', $metadata_arr, $post);
        // Attachments
    } elseif (is_attachment()) {
        $mime_type = get_post_mime_type($post->ID);
        //$attachment_type = strstr( $mime_type, '/', true );
        // See why we do not use strstr(): http://www.codetrax.org/issues/1091
        $attachment_type = preg_replace('#\\/[^\\/]*$#', '', $mime_type);
        // Images
        if ('image' == $attachment_type) {
            // $post is an image attachment
            // Image attachments
            //$image_meta = wp_get_attachment_metadata( $post->ID );   // contains info about all sizes
            // We use wp_get_attachment_image_src() since it constructs the URLs
            // Allow filtering of the image size.
            $image_size = apply_filters('amt_image_size_attachment', 'full');
            $main_size_meta = wp_get_attachment_image_src($post->ID, $image_size);
            // Type
            $metadata_arr[] = '<meta name="twitter:card" content="photo" />';
            // Author and Publisher
            $metadata_arr = array_merge($metadata_arr, amt_get_twitter_cards_author_publisher_metatags($options, $post));
            // Title
            $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
            // Description - We use the description defined by Add-Meta-Tags
            $content_desc = amt_get_content_description($post);
            if (!empty($content_desc)) {
                $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr($content_desc) . '" />';
            }
            // Image
            $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw($main_size_meta[0]) . '" />';
            if (apply_filters('amt_extended_image_tags', true)) {
                $metadata_arr[] = '<meta name="twitter:image:width" content="' . esc_attr($main_size_meta[1]) . '" />';
                $metadata_arr[] = '<meta name="twitter:image:height" content="' . esc_attr($main_size_meta[2]) . '" />';
            }
            // Audio & Video
        } elseif ($options["tc_enable_player_card_local"] == "1" && in_array($attachment_type, array('video', 'audio'))) {
            // Create player card for local video and audio attachments.
            // $post is an audio or video attachment
            // Type
            $metadata_arr[] = '<meta name="twitter:card" content="player" />';
            // Author and Publisher
            $metadata_arr = array_merge($metadata_arr, amt_get_twitter_cards_author_publisher_metatags($options, $post));
            // Title
            $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
            // Description - We use the description defined by Add-Meta-Tags
            $content_desc = amt_get_content_description($post);
            if (!empty($content_desc)) {
                $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr($content_desc) . '" />';
            }
            // twitter:player
            $metadata_arr[] = sprintf('<meta name="twitter:player" content="%s" />', esc_url_raw(amt_make_https(amt_embed_get_container_url($post->ID))));
            // Player size
            if ('video' == $attachment_type) {
                // Player size (this should be considered irrelevant of the video size)
                $player_size = apply_filters('amt_twitter_cards_video_player_size', array(640, 480));
            } elseif ('audio' == $attachment_type) {
                $player_size = apply_filters('amt_twitter_cards_audio_player_size', array(320, 30));
            }
            // twitter:player:width
            $metadata_arr[] = sprintf('<meta name="twitter:player:width" content="%d" />', esc_attr($player_size[0]));
            // twitter:player:height
            $metadata_arr[] = sprintf('<meta name="twitter:player:height" content="%d" />', esc_attr($player_size[1]));
            // twitter:image
            $preview_image_url = amt_embed_get_preview_image($post->ID);
            if (!empty($preview_image_url)) {
                $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw(amt_make_https($preview_image_url)) . '" />';
            }
            // twitter:player:stream
            $metadata_arr[] = '<meta name="twitter:player:stream" content="' . esc_url_raw(amt_make_https(amt_embed_get_stream_url($post->ID))) . '" />';
            // twitter:player:stream:content_type
            $metadata_arr[] = '<meta name="twitter:player:stream:content_type" content="' . esc_attr($mime_type) . '" />';
            //$metadata_arr[] = '<meta name="twitter:player:stream:content_type" content="video/mp4; codecs=&quot;avc1.42E01E1, mp4a.40.2&quot;">';
        }
        // Content
        // - standard format (post_format === false), aside, link, quote, status, chat (create summary card or summary_large_image if enforced)
        // - photo format (creates (summary_large_image card)
    } elseif (get_post_format($post->ID) === false || in_array(get_post_format($post->ID), array('image', 'aside', 'link', 'quote', 'status', 'chat'))) {
        // Render a summary card if standard format (or summary_large_image if enforced).
        // Render a summary_large_image card if image format.
        // Type
        if (get_post_format($post->ID) === false || in_array(get_post_format($post->ID), array('aside', 'link', 'quote', 'status', 'chat'))) {
            $metadata_arr[] = '<meta name="twitter:card" content="' . amt_get_default_twitter_card_type($options) . '" />';
            // Set the image size to use
            $image_size = apply_filters('amt_image_size_content', 'full');
        } elseif (get_post_format($post->ID) == 'image') {
            $metadata_arr[] = '<meta name="twitter:card" content="summary_large_image" />';
            // Set the image size to use
            // Since we need a bigger image, here we filter the image size through 'amt_image_size_attachment',
            // which typically returns a size bigger than 'amt_image_size_content'.
            $image_size = apply_filters('amt_image_size_attachment', 'full');
        }
        // Author and Publisher
        $metadata_arr = array_merge($metadata_arr, amt_get_twitter_cards_author_publisher_metatags($options, $post));
        // Title
        // Note: Contains multipage information
        $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // Description - We use the description defined by Add-Meta-Tags
        // Note: Contains multipage information through amt_process_paged()
        $content_desc = amt_get_content_description($post);
        if (!empty($content_desc)) {
            $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($content_desc)) . '" />';
        }
        // Image
        // Use the FIRST image ONLY
        // Set to true if image meta tags have been added to the card, so that it does not
        // search for any more images.
        $image_metatags_added = false;
        // First check if a global image override URL has been entered.
        // If yes, use this image URL and override all other images.
        $image_data = amt_get_image_data(amt_get_post_meta_image_url($post->ID));
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_content', 'full');
            $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
            //$global_image_override_url = amt_get_post_meta_image_url($post->ID);
            //if ( $image_metatags_added === false && ! empty( $global_image_override_url ) ) {
            //    $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $global_image_override_url ) . '" />';
            // Images have been found.
            $image_metatags_added = true;
        }
        // If the content has a featured image, then we use it.
        if ($image_metatags_added === false && function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
            $main_size_meta = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $image_size);
            $metadata_arr[] = '<meta name="twitter:image:src" content="' . esc_url_raw($main_size_meta[0]) . '" />';
            if (apply_filters('amt_extended_image_tags', true)) {
                $metadata_arr[] = '<meta name="twitter:image:width" content="' . esc_attr($main_size_meta[1]) . '" />';
                $metadata_arr[] = '<meta name="twitter:image:height" content="' . esc_attr($main_size_meta[2]) . '" />';
            }
            // Images have been found.
            $image_metatags_added = true;
        }
        // If a featured image is not set for this content, try to find the first image
        if ($image_metatags_added === false) {
            // Process all attachments and add metatags for the first image.
            foreach ($attachments as $attachment) {
                $mime_type = get_post_mime_type($attachment->ID);
                //$attachment_type = strstr( $mime_type, '/', true );
                // See why we do not use strstr(): http://www.codetrax.org/issues/1091
                $attachment_type = preg_replace('#\\/[^\\/]*$#', '', $mime_type);
                if ('image' == $attachment_type) {
                    // Image tags
                    $main_size_meta = wp_get_attachment_image_src($attachment->ID, $image_size);
                    $metadata_arr[] = '<meta name="twitter:image:src" content="' . esc_url_raw($main_size_meta[0]) . '" />';
                    if (apply_filters('amt_extended_image_tags', true)) {
                        $metadata_arr[] = '<meta name="twitter:image:width" content="' . esc_attr($main_size_meta[1]) . '" />';
                        $metadata_arr[] = '<meta name="twitter:image:height" content="' . esc_attr($main_size_meta[2]) . '" />';
                    }
                    // Images have been found.
                    $image_metatags_added = true;
                    // If an image is added, break.
                    break;
                }
            }
        }
        // If a local image-attachment is not set, try to find any embedded images
        if ($image_metatags_added === false) {
            // Embedded Media
            foreach ($embedded_media['images'] as $embedded_item) {
                if (get_post_format($post->ID) === false || in_array(get_post_format($post->ID), array('aside', 'link', 'quote', 'status', 'chat'))) {
                    $metadata_arr[] = '<meta name="twitter:image:src" content="' . esc_url_raw($embedded_item['thumbnail']) . '" />';
                    if (apply_filters('amt_extended_image_tags', true)) {
                        $metadata_arr[] = '<meta name="twitter:image:width" content="150" />';
                        $metadata_arr[] = '<meta name="twitter:image:height" content="150" />';
                    }
                } elseif (get_post_format($post->ID) == 'image') {
                    $metadata_arr[] = '<meta name="twitter:image:src" content="' . esc_url_raw($embedded_item['image']) . '" />';
                    if (apply_filters('amt_extended_image_tags', true)) {
                        $metadata_arr[] = '<meta name="twitter:image:width" content="' . esc_attr($embedded_item['width']) . '" />';
                        $metadata_arr[] = '<meta name="twitter:image:height" content="' . esc_attr($embedded_item['height']) . '" />';
                    }
                }
                // Images have been found.
                $image_metatags_added = true;
                // If an image is added, break.
                break;
            }
        }
        // If an image is still missing, then use the default image (if set).
        if ($image_metatags_added === false) {
            $image_data = amt_get_default_image_data();
            if (!empty($image_data)) {
                // Image size already set
                //$image_size = apply_filters( 'amt_image_size_index', 'full' );
                $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                if (!empty($image_meta_tags)) {
                    $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                }
            }
            //$metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $options["default_image_url"] ) . '" />';
        }
        // Content
        // - gallery format (creates gallery card)
    } elseif (get_post_format($post->ID) == 'gallery') {
        // Render a gallery card if gallery format.
        // Type
        $metadata_arr[] = '<meta name="twitter:card" content="gallery" />';
        // Author and Publisher
        $metadata_arr = array_merge($metadata_arr, amt_get_twitter_cards_author_publisher_metatags($options, $post));
        // Title
        // Note: Contains multipage information
        $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
        // Description - We use the description defined by Add-Meta-Tags
        // Note: Contains multipage information through amt_process_paged()
        $content_desc = amt_get_content_description($post);
        if (!empty($content_desc)) {
            $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($content_desc)) . '" />';
        }
        // Image
        // Set to true if image meta tags have been added to the card, so that it does not
        // search for any more images.
        $image_metatags_added = false;
        // First check if a global image override URL has been entered.
        // If yes, use this image URL and override all other images.
        $image_data = amt_get_image_data(amt_get_post_meta_image_url($post->ID));
        if (!empty($image_data)) {
            $image_size = apply_filters('amt_image_size_content', 'full');
            $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
            if (!empty($image_meta_tags)) {
                $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
            }
            //$global_image_override_url = amt_get_post_meta_image_url($post->ID);
            //if ( $image_metatags_added === false && ! empty( $global_image_override_url ) ) {
            // Note 'image0'
            //    $metadata_arr[] = '<meta name="twitter:image0" content="' . esc_url_raw( $global_image_override_url ) . '" />';
            // Images have been found.
            $image_metatags_added = true;
        }
        // Build the gallery
        if ($image_metatags_added === false) {
            // Image counter
            $k = 0;
            // Process all attachments and add metatags for the first image
            foreach ($attachments as $attachment) {
                $mime_type = get_post_mime_type($attachment->ID);
                //$attachment_type = strstr( $mime_type, '/', true );
                // See why we do not use strstr(): http://www.codetrax.org/issues/1091
                $attachment_type = preg_replace('#\\/[^\\/]*$#', '', $mime_type);
                if ('image' == $attachment_type) {
                    // Image tags
                    // Allow filtering of the image size.
                    $image_size = apply_filters('amt_image_size_content', 'full');
                    $main_size_meta = wp_get_attachment_image_src($attachment->ID, $image_size);
                    $metadata_arr[] = '<meta name="twitter:image' . $k . '" content="' . esc_url_raw($main_size_meta[0]) . '" />';
                    // Increment the counter
                    $k++;
                }
            }
            // Embedded Media
            foreach ($embedded_media['images'] as $embedded_item) {
                $metadata_arr[] = '<meta name="twitter:image' . $k . '" content="' . esc_url_raw($embedded_item['image']) . '" />';
                // Increment the counter
                $k++;
            }
        }
        // Content
        // - video/audio format (creates player card)
        // Note: The ``tc_enable_player_card_local`` option is checked after this initial check,
        // because 'player' twitter cards are always generated for embedded audio and video.
    } elseif (get_post_format($post->ID) == 'video' || get_post_format($post->ID) == 'audio') {
        $post_format = get_post_format($post->ID);
        $audio_video_metatags_complete = false;
        // Process local media only if it is allowed by the user.
        if ($audio_video_metatags_complete === false && $options["tc_enable_player_card_local"] == "1") {
            // Local media - Process all attachments and add metatags for the first video
            foreach ($attachments as $attachment) {
                $mime_type = get_post_mime_type($attachment->ID);
                //$attachment_type = strstr( $mime_type, '/', true );
                // See why we do not use strstr(): http://www.codetrax.org/issues/1091
                $attachment_type = preg_replace('#\\/[^\\/]*$#', '', $mime_type);
                // Get attachment metadata from WordPress
                $attachment_metadata = wp_get_attachment_metadata($attachment->ID);
                // We create player cards for video and audio attachments.
                // The post might have attachments of other types.
                if (!in_array($attachment_type, array('video', 'audio'))) {
                    continue;
                } elseif ($attachment_type != $post_format) {
                    continue;
                }
                // Render a player card for the first attached audio or video.
                // twitter:card
                $metadata_arr[] = '<meta name="twitter:card" content="player" />';
                // Author and Publisher
                $metadata_arr = array_merge($metadata_arr, amt_get_twitter_cards_author_publisher_metatags($options, $post));
                // twitter:title
                // Title - Note: Contains multipage information
                $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
                // twitter:description
                // Description - We use the description defined by Add-Meta-Tags
                // Note: Contains multipage information through amt_process_paged()
                $content_desc = amt_get_content_description($post);
                if (!empty($content_desc)) {
                    $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($content_desc)) . '" />';
                }
                // twitter:player
                $metadata_arr[] = sprintf('<meta name="twitter:player" content="%s" />', esc_url_raw(amt_make_https(amt_embed_get_container_url($attachment->ID))));
                // Player size
                if ($post_format == 'video') {
                    // Player size (this should be considered irrelevant of the video size)
                    $player_size = apply_filters('amt_twitter_cards_video_player_size', array(640, 480));
                } elseif ($post_format == 'audio') {
                    $player_size = apply_filters('amt_twitter_cards_audio_player_size', array(320, 30));
                }
                // twitter:player:width
                $metadata_arr[] = sprintf('<meta name="twitter:player:width" content="%d" />', esc_attr($player_size[0]));
                // twitter:player:height
                $metadata_arr[] = sprintf('<meta name="twitter:player:height" content="%d" />', esc_attr($player_size[1]));
                // twitter:player:stream
                $metadata_arr[] = '<meta name="twitter:player:stream" content="' . esc_url_raw(amt_make_https(amt_embed_get_stream_url($attachment->ID))) . '" />';
                // twitter:player:stream:content_type
                $metadata_arr[] = '<meta name="twitter:player:stream:content_type" content="' . esc_attr($mime_type) . '" />';
                //$metadata_arr[] = '<meta name="twitter:player:stream:content_type" content="video/mp4; codecs=&quot;avc1.42E01E1, mp4a.40.2&quot;">';
                // twitter:image
                // First check if a global image override URL has been set in the post's metabox.
                // If yes, use this image URL and override all other images.
                $image_data = amt_get_image_data(amt_get_post_meta_image_url($post->ID));
                if (!empty($image_data)) {
                    $image_size = apply_filters('amt_image_size_content', 'full');
                    $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                    if (!empty($image_meta_tags)) {
                        $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                    }
                    //$global_image_override_url = amt_get_post_meta_image_url($post->ID);
                    //if ( ! empty( $global_image_override_url ) ) {
                    //    $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $global_image_override_url ) . '" />';
                    // Else use the featured image if it exists
                } elseif (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
                    // Set the image size to use
                    $image_size = apply_filters('amt_image_size_content', 'full');
                    $main_size_meta = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $image_size);
                    $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw($main_size_meta[0]) . '" />';
                    //if ( apply_filters( 'amt_extended_image_tags', true ) ) {
                    //    $metadata_arr[] = '<meta name="twitter:image:width" content="' . esc_attr( $main_size_meta[1] ) . '" />';
                    //    $metadata_arr[] = '<meta name="twitter:image:height" content="' . esc_attr( $main_size_meta[2] ) . '" />';
                    //}
                    // Else use the attachment's featured image, if set.
                } else {
                    // Else use the attachment's featured image, if set.
                    $image_data = amt_embed_get_preview_image($attachment->ID);
                    if (!empty($image_data)) {
                        $image_size = apply_filters('amt_image_size_content', 'full');
                        $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                        if (!empty($image_meta_tags)) {
                            $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                        }
                    }
                    //$preview_image_url = amt_embed_get_preview_image( $attachment->ID );
                    //if ( ! empty( $preview_image_url ) ) {
                    //    $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( amt_make_https( $preview_image_url ) ) . '" />';
                    //}
                }
                $audio_video_metatags_complete = true;
                break;
            }
        }
        // Process embedded media only if a twitter player card has not been generated.
        if ($audio_video_metatags_complete === false) {
            // Determine the relevant array (videos or sounds)
            if ($post_format == 'video') {
                $embedded_items = $embedded_media['videos'];
            } elseif ($post_format == 'audio') {
                $embedded_items = $embedded_media['sounds'];
            }
            // Embedded Media
            foreach ($embedded_items as $embedded_item) {
                // Render a player card for the first embedded video.
                // twitter:card
                $metadata_arr[] = '<meta name="twitter:card" content="player" />';
                // Author and Publisher
                $metadata_arr = array_merge($metadata_arr, amt_get_twitter_cards_author_publisher_metatags($options, $post));
                // twitter:title
                // Title - Note: Contains multipage information
                $metadata_arr['twitter:title'] = '<meta name="twitter:title" content="' . esc_attr(amt_get_title_for_metadata($options, $post)) . '" />';
                // twitter:description
                // Description - We use the description defined by Add-Meta-Tags
                // Note: Contains multipage information through amt_process_paged()
                $content_desc = amt_get_content_description($post);
                if (!empty($content_desc)) {
                    $metadata_arr[] = '<meta name="twitter:description" content="' . esc_attr(amt_process_paged($content_desc)) . '" />';
                }
                // twitter:player
                $metadata_arr[] = '<meta name="twitter:player" content="' . esc_url_raw($embedded_item['player']) . '" />';
                // Player size
                // Alt Method: Size uses  $content_width
                //global $content_width;
                //$width = $content_width;
                //$height = absint(absint($content_width)*3/4);
                //$metadata_arr[] = '<meta name="twitter:width" content="' . esc_attr( $width ) . '" />';
                //$metadata_arr[] = '<meta name="twitter:height" content="' . esc_attr( $height ) . '" />';
                // twitter:player:width
                $metadata_arr[] = sprintf('<meta name="twitter:player:width" content="%d" />', esc_attr($embedded_item['width']));
                // twitter:player:height
                $metadata_arr[] = sprintf('<meta name="twitter:player:height" content="%d" />', esc_attr($embedded_item['height']));
                // twitter:image
                // First check if a global image override URL has been set in the post's metabox.
                // If yes, use this image URL and override all other images.
                $image_data = amt_get_image_data(amt_get_post_meta_image_url($post->ID));
                if (!empty($image_data)) {
                    $image_size = apply_filters('amt_image_size_content', 'full');
                    $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                    if (!empty($image_meta_tags)) {
                        $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                    }
                    //$global_image_override_url = amt_get_post_meta_image_url($post->ID);
                    //if ( ! empty( $global_image_override_url ) ) {
                    //    $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $global_image_override_url ) . '" />';
                    // Else use the featured image if it exists
                } elseif (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
                    // Set the image size to use
                    $image_size = apply_filters('amt_image_size_content', 'full');
                    $main_size_meta = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $image_size);
                    $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw($main_size_meta[0]) . '" />';
                    //if ( apply_filters( 'amt_extended_image_tags', true ) ) {
                    //    $metadata_arr[] = '<meta name="twitter:image:width" content="' . esc_attr( $main_size_meta[1] ) . '" />';
                    //    $metadata_arr[] = '<meta name="twitter:image:height" content="' . esc_attr( $main_size_meta[2] ) . '" />';
                    //}
                    // Else use the attachment's featured image, if set.
                } else {
                    $image_data = amt_get_image_data($embedded_item['thumbnail']);
                    if (!empty($image_data)) {
                        $image_size = apply_filters('amt_image_size_content', 'full');
                        $image_meta_tags = amt_get_twitter_cards_image_metatags($options, $image_data, $size = $image_size);
                        if (!empty($image_meta_tags)) {
                            $metadata_arr = array_merge($metadata_arr, $image_meta_tags);
                        }
                    }
                }
                // Else use the discovered preview image, if any.
                //} elseif ( ! empty( $embedded_item['thumbnail'] ) ) {
                //    $metadata_arr[] = '<meta name="twitter:image" content="' . esc_url_raw( $embedded_item['thumbnail'] ) . '" />';
                //}
                //
                $audio_video_metatags_complete = true;
                break;
            }
        }
    }
    // Filtering of the generated Twitter Card metadata
    $metadata_arr = apply_filters('amt_twitter_cards_metadata_head', $metadata_arr);
    return $metadata_arr;
}
/**
 * Replaces the text to be used in the title element, if a replacement text has been set.
 */
function amt_custom_title_tag($title)
{
    if (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
        // Get current post object
        $post = get_queried_object();
        if (is_null($post)) {
            return $title;
        }
        // Check if metadata is supported on this content type.
        $post_type = get_post_type($post);
        if (!in_array($post_type, amt_get_supported_post_types())) {
            return $title;
        }
        $custom_title = amt_get_post_meta_title($post->ID);
        if (!empty($custom_title)) {
            $custom_title = str_replace('%title%', $title, $custom_title);
            // Allow filtering of the custom title
            $custom_title = apply_filters('amt_custom_title', $custom_title);
            // Note: Contains multipage information through amt_process_paged()
            return esc_attr(amt_process_paged($custom_title));
        }
    }
    // WordPress adds multipage information if a custom title is not set.
    return $title;
}
示例#6
0
/**
 * This is a helper function that returns the post's or page's keywords.
 *
 * Important: MUST return sanitized data, unless this plugin has sanitized the data before storing to db.
 *
 */
function amt_get_content_keywords($post, $auto = true, $exclude_categories = false)
{
    // By default, if custom keywords have not been entered by the user in the
    // metabox, keywords are autogenerated. To stop this automatic generation
    // of keywords and return only the keywords that have been entered manually,
    // set $auto to false via the following filter.
    $auto = apply_filters('amt_generate_keywords_if_no_manual_data', $auto);
    $content_keywords = '';
    /*
     * Custom post field "keywords" overrides post's categories, tags (tags exist in WordPress 2.3 or newer)
     * and custom taxonomy terms (custom taxonomies exist since WP version 2.8).
     * %cats% is replaced by the post's categories.
     * %tags% is replaced by the post's tags.
     * %terms% is replaced by the post's custom taxonomy terms.
     */
    if (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
        $keyw_fld_content = amt_get_post_meta_keywords($post->ID);
        // If there is a custom field, use it
        if (!empty($keyw_fld_content)) {
            // On single posts, expand the %cats%, %tags% and %terms% placeholders.
            // This should not take place in pages (no categories, no tags by default)
            // or custom post types, the support of which for categories and tags is unknown.
            if (is_single()) {
                // Here we sanitize the provided keywords for safety
                $keywords_from_post_cats = sanitize_text_field(amt_sanitize_keywords(amt_get_keywords_from_post_cats($post)));
                if (!empty($keywords_from_post_cats)) {
                    $keyw_fld_content = str_replace("%cats%", $keywords_from_post_cats, $keyw_fld_content);
                }
                // Also, the %tags% placeholder is replaced by the post's tags (WordPress 2.3 or newer)
                if (version_compare(get_bloginfo('version'), '2.3', '>=')) {
                    // Here we sanitize the provided keywords for safety
                    $keywords_from_post_tags = sanitize_text_field(amt_sanitize_keywords(amt_get_post_tags($post)));
                    if (!empty($keywords_from_post_tags)) {
                        $keyw_fld_content = str_replace("%tags%", $keywords_from_post_tags, $keyw_fld_content);
                    }
                }
                // Also, the %terms% placeholder is replaced by the post's custom taxonomy terms (WordPress 2.8 or newer)
                if (version_compare(get_bloginfo('version'), '2.8', '>=')) {
                    // Here we sanitize the provided keywords for safety
                    $keywords_from_post_terms = sanitize_text_field(amt_sanitize_keywords(amt_get_keywords_from_custom_taxonomies($post)));
                    if (!empty($keywords_from_post_terms)) {
                        $keyw_fld_content = str_replace("%terms%", $keywords_from_post_terms, $keyw_fld_content);
                    }
                }
            }
            $content_keywords .= $keyw_fld_content;
            // Otherwise, generate the keywords from categories, tags and custom taxonomy terms.
            // Note:
            // Here we use is_singular(), so that pages are also checked for categories and tags.
            // By default, pages do not support categories and tags, but enabling such
            // functionality is trivial. See #1206 for more details.
        } elseif ($auto && is_singular()) {
            /*
             * Add keywords automatically.
             * Keywords consist of the post's categories, the post's tags (tags exist in WordPress 2.3 or newer)
             * and the terms of the custom taxonomies to which the post belongs (since WordPress 2.8).
             */
            // Categories - Here we sanitize the provided keywords for safety
            if ($exclude_categories === false) {
                $keywords_from_post_cats = sanitize_text_field(amt_sanitize_keywords(amt_get_keywords_from_post_cats($post)));
                if (!empty($keywords_from_post_cats)) {
                    $content_keywords .= $keywords_from_post_cats;
                }
            }
            // Tags - Here we sanitize the provided keywords for safety
            $keywords_from_post_tags = sanitize_text_field(amt_sanitize_keywords(amt_get_post_tags($post)));
            if (!empty($keywords_from_post_tags)) {
                if (!empty($content_keywords)) {
                    $content_keywords .= ", ";
                }
                $content_keywords .= $keywords_from_post_tags;
            }
            // Custom taxonomy terms - Here we sanitize the provided keywords for safety
            $keywords_from_post_custom_taxonomies = sanitize_text_field(amt_sanitize_keywords(amt_get_keywords_from_custom_taxonomies($post)));
            if (!empty($keywords_from_post_custom_taxonomies)) {
                if (!empty($content_keywords)) {
                    $content_keywords .= ", ";
                }
                $content_keywords .= $keywords_from_post_custom_taxonomies;
            }
        }
    }
    // Add post format to the list of keywords
    if ($auto && is_singular() && get_post_format($post->ID) !== false) {
        if (empty($content_keywords)) {
            $content_keywords .= get_post_format($post->ID);
        } else {
            $content_keywords .= ', ' . get_post_format($post->ID);
        }
    }
    /**
     * Finally, add the global keywords, if they are set in the administration panel.
     */
    #if ( !empty($content_keywords) && ( is_singular() || amt_is_static_front_page() || amt_is_static_home() ) ) {
    if ($auto && (is_singular() || amt_is_static_front_page() || amt_is_static_home())) {
        $options = get_option("add_meta_tags_opts");
        $global_keywords = amt_get_site_global_keywords($options);
        if (!empty($global_keywords)) {
            // If we have $content_keywords so far
            if (!empty($content_keywords)) {
                if (strpos($global_keywords, '%contentkw%') === false) {
                    // The placeholder ``%contentkw%`` has not been used. Append the content keywords to the global keywords.
                    $content_keywords = $global_keywords . ', ' . $content_keywords;
                } else {
                    // The user has used the placeholder ``%contentkw%``. Replace it with the content keywords.
                    $content_keywords = str_replace('%contentkw%', $content_keywords, $global_keywords);
                }
                // If $content_keywords have not been found.
            } else {
                if (strpos($global_keywords, '%contentkw%') === false) {
                    // The placeholder ``%contentkw%`` has not been used. Just use the global keywords as is.
                    $content_keywords = $global_keywords;
                } else {
                    // The user has used the placeholder ``%contentkw%``, but we do not have generated any content keywords => Delete the %contentkw% placeholder.
                    $global_keywords_new = array();
                    foreach (explode(',', $global_keywords) as $g_keyword) {
                        $g_keyword = trim($g_keyword);
                        if ($g_keyword != '%contentkw%') {
                            $global_keywords_new[] = $g_keyword;
                        }
                    }
                    if (!empty($global_keywords_new)) {
                        $content_keywords = implode(', ', $global_keywords_new);
                    }
                }
            }
        }
    }
    return $content_keywords;
}
示例#7
0
function amt_internal_get_title($options, $post, $title_templates, $force_custom_title_if_set = false, $caller_is_metadata_generator = false)
{
    // EARLY PROCESSING
    // First we check for a custom title whgich may have been inserted in the
    // relevant Custom Field of the supported types.
    $custom_title = '';
    if (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
        if (!is_null($post)) {
            // Check if metadata is supported on this content type.
            $post_type = get_post_type($post);
            if (in_array($post_type, amt_get_supported_post_types())) {
                // Store the custom title. Should be empty for post types which do not support a custom title or which have an empty custom title.
                $custom_title = amt_get_post_meta_title($post->ID);
                //$custom_title = str_replace('%title%', $title, $custom_title);
                // Allow filtering of the custom title
                $custom_title = apply_filters('amt_custom_title', $custom_title);
            }
        }
    }
    // Early processing in case advanced title management is TURNED OFF
    // This early processing takes place only for calls from the 'amt_get_title_for_title_element()' function.
    // WordPress constructs its own titles for the 'title' HTML element, so we
    // do not need to do further processing and title guessing here.
    // This early processing is NOT performed for calls from the 'amt_get_title_for_metadata()' function,
    // because the metadata generators
    if (!$caller_is_metadata_generator && array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '0') {
        if (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
            if (!empty($custom_title)) {
                // Contains paging information
                return amt_process_paged($custom_title);
            }
        }
        return;
    }
    // From now on Add-Meta-Tags generates the title.
    // TEMPLATE VARIABLES
    // Set template variable values
    // #entity_title#, #Entity_title#, #Entity_Title#, #page#, #page_total#, #site_name#, #site_tagline#, #year#, #month#, #month_name#, #day#
    // Date variables
    // Credit for the following here: http://wordpress.stackexchange.com/a/109674
    // https://developer.wordpress.org/reference/classes/wp_locale/
    $var_year = 0;
    $var_month = 0;
    $var_month_name = '';
    $var_day = 0;
    if (is_date()) {
        // On date archives the following have a value. On the default front page are zero
        $var_year = get_query_var('year');
        $var_month = get_query_var('monthnum');
        $var_month_name = '';
        if ($var_month) {
            $var_month_name = $GLOBALS['wp_locale']->get_month($var_month);
        }
        $var_day = get_query_var('day');
    } elseif (is_singular() || amt_is_static_front_page() || amt_is_static_home()) {
        $var_year = mysql2date('Y', $post->post_date);
        $var_month = mysql2date('m', $post->post_date);
        $var_month_name = '';
        if ($var_month) {
            $var_month_name = $GLOBALS['wp_locale']->get_month($var_month);
        }
        $var_day = mysql2date('d', $post->post_date);
    }
    // #page_total#
    global $wp_query;
    $page_total = 1;
    if (isset($wp_query->max_num_pages)) {
        $page_total = $wp_query->max_num_pages;
    }
    // #page#
    $page = 1;
    // For paginated archives or paginated main page with latest posts.
    if (is_paged()) {
        $paged = get_query_var('paged');
        // paged
        if ($paged && $paged >= 2) {
            $page = $paged;
        }
        // For a Post or Page that has been divided into pages using the <!--nextpage--> QuickTag
    } else {
        $paged = get_query_var('page');
        // page
        if ($paged && $paged >= 2) {
            $page = $paged;
        }
    }
    // #site_name#
    $site_name = get_bloginfo('name');
    // #site_tagline#
    $site_tagline = get_bloginfo('description');
    // MAIN PROCESSING
    // 1) generate title, 2) determine title template
    // #entity_title# and $entity_title_template
    $entity_title = '';
    $entity_title_template = '';
    // Default front page displaying the latest posts
    if (amt_is_default_front_page()) {
        // Entity title
        // $post is NULL
        $entity_title = get_bloginfo('name');
        // No custom title
        // Entity title template
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists('front_page_default_paged', $title_templates)) {
                $entity_title_template = $title_templates['front_page_default_paged'];
            } elseif (array_key_exists('front_page_default', $title_templates)) {
                $entity_title_template = $title_templates['front_page_default'];
            }
        }
        // Front page using a static page
        // Note: might also contain a listing of posts which may be paged, so use amt_process_paged()
    } elseif (amt_is_static_front_page()) {
        // Entity title
        $entity_title = strip_tags(get_the_title($post->ID));
        if (!empty($custom_title) && $force_custom_title_if_set) {
            $custom_title = str_replace('%title%', $entity_title, $custom_title);
            $entity_title = $custom_title;
        }
        // Entity title template
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists('front_page_static_paged', $title_templates)) {
                $entity_title_template = $title_templates['front_page_static_paged'];
            } elseif (array_key_exists('front_page_static', $title_templates)) {
                $entity_title_template = $title_templates['front_page_static'];
            }
        }
        // The posts index page - a static page displaying the latest posts
    } elseif (amt_is_static_home()) {
        // Entity title
        $entity_title = strip_tags(get_the_title($post->ID));
        if (!empty($custom_title) && $force_custom_title_if_set) {
            $custom_title = str_replace('%title%', $entity_title, $custom_title);
            $entity_title = $custom_title;
        }
        // Entity title template
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists('blog_index_static_paged', $title_templates)) {
                $entity_title_template = $title_templates['blog_index_static_paged'];
            } elseif (array_key_exists('blog_index_static', $title_templates)) {
                $entity_title_template = $title_templates['blog_index_static'];
            }
        }
        // Date Archives
        // Yearly Archive
    } elseif (is_year()) {
        // Entity title
        $entity_title = $var_year;
        // Entity title template
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists('archive_date_yearly_paged', $title_templates)) {
                $entity_title_template = $title_templates['archive_date_yearly_paged'];
            } elseif (array_key_exists('archive_date_yearly', $title_templates)) {
                $entity_title_template = $title_templates['archive_date_yearly'];
            }
        }
        // Monthly Archive
    } elseif (is_month()) {
        // Entity title
        $entity_title = $var_month_name;
        // Entity title template
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists('archive_date_monthly_paged', $title_templates)) {
                $entity_title_template = $title_templates['archive_date_monthly_paged'];
            } elseif (array_key_exists('archive_date_monthly', $title_templates)) {
                $entity_title_template = $title_templates['archive_date_monthly'];
            }
        }
        // Daily Archive
    } elseif (is_day()) {
        // Entity title
        $entity_title = $var_day;
        // Entity title template
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists('archive_date_daily_paged', $title_templates)) {
                $entity_title_template = $title_templates['archive_date_daily_paged'];
            } elseif (array_key_exists('archive_date_daily', $title_templates)) {
                $entity_title_template = $title_templates['archive_date_daily'];
            }
        }
        // Taxonomy Archive
        // $post is a taxonomy term object
    } elseif (is_category() || is_tag() || is_tax()) {
        // Entity title
        $entity_title = single_term_title($prefix = '', $display = false);
        // Entity title template
        $template_name = 'archive_taxonomy_' . $post->taxonomy;
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists($template_name . '_paged', $title_templates)) {
                $entity_title_template = $title_templates[$template_name . '_paged'];
            } elseif (array_key_exists($template_name, $title_templates)) {
                $entity_title_template = $title_templates[$template_name];
            }
        }
        // Author Archive
        // $post is an author object
    } elseif (is_author()) {
        // Entity title
        $entity_title = $post->display_name;
        // Entity title template
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists('archive_author_paged', $title_templates)) {
                $entity_title_template = $title_templates['archive_author_paged'];
            } elseif (array_key_exists('archive_author', $title_templates)) {
                $entity_title_template = $title_templates['archive_author'];
            }
        }
        // Custom Post Type Archive
    } elseif (is_post_type_archive()) {
        // Entity title
        $entity_title = post_type_archive_title($prefix = '', $display = false);
        // Entity title template
        // $post is a content type object
        $template_name = 'archive_posttype_' . $post->name;
        if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
            if ($page && $page >= 2 && array_key_exists($template_name . '_paged', $title_templates)) {
                $entity_title_template = $title_templates[$template_name . '_paged'];
            } elseif (array_key_exists($template_name, $title_templates)) {
                $entity_title_template = $title_templates[$template_name];
            }
        }
        // Content
    } elseif (is_singular()) {
        // Entity title
        // In some cases, like EDD downloads, get_the_title() also returns escaped HTML. Use strip_tags().
        $entity_title = strip_tags(get_the_title($post->ID));
        // Alternatively, use the_title_attribute(). See: https://codex.wordpress.org/Function_Reference/the_title_attribute
        //$entity_title = the_title_attribute( array( 'before'=>'', 'after'=>'', 'echo'=>false, $post->ID) );
        if (!empty($custom_title) && $force_custom_title_if_set) {
            $custom_title = str_replace('%title%', $entity_title, $custom_title);
            $entity_title = $custom_title;
        }
        // Attachments
        if (is_attachment()) {
            // Attachment type
            $mime_type = get_post_mime_type($post->ID);
            //$attachment_type = strstr( $mime_type, '/', true );
            // See why we do not use strstr(): http://www.codetrax.org/issues/1091
            $attachment_type = preg_replace('#\\/[^\\/]*$#', '', $mime_type);
            $template_name = 'content_attachment';
            if (array_key_exists('content_attachment_' . $attachment_type, $title_templates)) {
                $template_name = 'content_attachment_' . $attachment_type;
            }
            // Entity title template
            if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
                // No paging info
                if (array_key_exists($template_name, $title_templates)) {
                    $entity_title_template = $title_templates[$template_name];
                }
            }
            // Page
        } elseif (is_page()) {
            // Entity title template
            if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
                if ($page && $page >= 2 && array_key_exists('content_page_paged', $title_templates)) {
                    $entity_title_template = $title_templates['content_page_paged'];
                } elseif (array_key_exists('content_page', $title_templates)) {
                    $entity_title_template = $title_templates['content_page'];
                }
            }
            // Posts and custom post types (with post format checking)
        } else {
            $post_type = get_post_type($post);
            $post_format = get_post_format($post->ID);
            $template_name = 'content_' . $post_type;
            if ($post_format !== false && array_key_exists($template_name . '_' . $post_format, $title_templates)) {
                $template_name = $template_name . '_' . $post_format;
            }
            // Entity title template
            if (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
                if ($page && $page >= 2 && array_key_exists($template_name . '_paged', $title_templates)) {
                    $entity_title_template = $title_templates[$template_name . '_paged'];
                } elseif (array_key_exists($template_name, $title_templates)) {
                    $entity_title_template = $title_templates[$template_name];
                }
            }
        }
    }
    // LATE PROCESSING
    $title = '';
    // Late processing in case advanced title management is TURNED OFF
    // This late processing takes place only for calls from the 'amt_get_title_for_metadata()' function.
    // The metadata generators do not construct a title, so this has to be done here (above $entity_title).
    if ($caller_is_metadata_generator && array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '0') {
        if (!empty($entity_title)) {
            if ($page && $page >= 2) {
                $title = amt_process_paged($entity_title);
            } else {
                $title = $entity_title;
            }
        } else {
            // TODO: This appears in BuddyPress member pages. Maybe it should just set $title to an empty string.
            //$title = 'PROGRAMMING ERROR - MISSING TITLE';
            $title = '';
        }
    } elseif (empty($entity_title_template)) {
        // If the caller is a metadata generator, then the title cannot be determined otherwise.
        // So, if an $entity_title has been found we return it.
        if ($caller_is_metadata_generator) {
            // If a metadata generator requested a title, but a template was
            // not found, return an error message as the title, unless the
            // $entity_title is not empty, in which case set the title to it as is.
            if (!empty($entity_title)) {
                $title = $entity_title;
            } else {
                // TODO: Maybe it should just set $title to an empty string.
                //$title = 'TITLE TEMPLATE NOT FOUND';
                $title = '';
            }
        } else {
            // If the title was requested for the 'title' HTML element, but a template
            //was not found, return an empty string, so that the default WordPress title is used.
            // TODO: Check if still required
            $title = '';
        }
        // If advanced title management is enabled
    } elseif (array_key_exists('enable_advanced_title_management', $options) && $options['enable_advanced_title_management'] == '1') {
        $template_vars = array('#year#' => $var_year, '#month#' => $var_month, '#month_name#' => $var_month_name, '#day#' => $var_day, '#page#' => $page, '#page_total#' => $page_total, '#site_name#' => $site_name, '#site_tagline#' => $site_tagline, '#entity_title#' => $entity_title, '#Entity_title#' => ucfirst(strtolower($entity_title)), '#Entity_Title#' => ucwords(strtolower($entity_title)));
        // Replace variables in the template
        foreach ($template_vars as $var_name => $var_value) {
            $entity_title_template = str_replace($var_name, $var_value, $entity_title_template);
        }
        $title = $entity_title_template;
    }
    return $title;
}