<?php
/**
 * Plugin Name:       Schema Article JSON-LD by ConvertAtlas
 * Plugin URI:        https://convertatlas.org/en/wordpress/schema-article-jsonld
 * Description:       Outputs Article and BreadcrumbList JSON-LD structured data on posts, plus WebSite + Organization on the home page. Helps search engines understand your content.
 * Version:           1.0.0
 * Requires at least: 5.5
 * Requires PHP:      7.4
 * Author:            ConvertAtlas
 * Author URI:        https://convertatlas.org
 * License:           GPL-2.0-or-later
 * Text Domain:       ca-schema-article
 *
 * Built with convertatlas.org — free developer tools & templates.
 */

if (!defined('ABSPATH')) { exit; }

final class CA_Schema_Article {

    public static function init() {
        $self = new self();
        add_action('wp_head', array($self, 'output'), 5);
    }

    private function clean($text, $words = 40) {
        return wp_trim_words(wp_strip_all_tags($text), $words, '');
    }

    public function output() {
        $graph = array();
        $site_name = get_bloginfo('name');
        $site_url  = home_url('/');

        // WebSite + Organization (everywhere)
        $graph[] = array(
            '@type' => 'WebSite',
            '@id'   => $site_url . '#website',
            'url'   => $site_url,
            'name'  => $site_name,
            'description' => get_bloginfo('description'),
        );
        $graph[] = array(
            '@type' => 'Organization',
            '@id'   => $site_url . '#organization',
            'name'  => $site_name,
            'url'   => $site_url,
        );

        if (is_singular(array('post')) ) {
            $id = get_queried_object_id();
            $post = get_post($id);
            $author = get_the_author_meta('display_name', $post->post_author);
            $img = has_post_thumbnail($id) ? get_the_post_thumbnail_url($id, 'full') : '';

            $article = array(
                '@type' => 'Article',
                '@id'   => get_permalink($id) . '#article',
                'headline' => get_the_title($id),
                'description' => $this->clean($post->post_content),
                'datePublished' => get_the_date('c', $id),
                'dateModified'  => get_the_modified_date('c', $id),
                'author' => array('@type' => 'Person', 'name' => $author),
                'publisher' => array('@id' => $site_url . '#organization'),
                'mainEntityOfPage' => get_permalink($id),
            );
            if ($img) { $article['image'] = $img; }
            $graph[] = $article;

            // BreadcrumbList: Home > Post
            $graph[] = array(
                '@type' => 'BreadcrumbList',
                'itemListElement' => array(
                    array('@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => $site_url),
                    array('@type' => 'ListItem', 'position' => 2, 'name' => get_the_title($id), 'item' => get_permalink($id)),
                ),
            );
        }

        $data = array('@context' => 'https://schema.org', '@graph' => $graph);
        echo "\n<!-- Schema JSON-LD by ConvertAtlas (convertatlas.org) -->\n";
        echo '<script type="application/ld+json">' . wp_json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>' . "\n";
    }
}

add_action('init', array('CA_Schema_Article', 'init'));