|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * JSON Structuring Markup Plugin |
| 5 | + * Copyright (C) 2016, Sami Ahmed Siddiqui <[email protected]> |
| 6 | + * |
| 7 | + * This file is a part of JSON Structuring Markup. |
| 8 | + * |
| 9 | + * JSON Structuring Markup is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | +
|
| 14 | + * JSON Structuring Markup is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | +
|
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * Schema.org Article Settings Page |
| 25 | + */ |
| 26 | +function json_structuring_markup_article_settings_page() { |
| 27 | + if ( !current_user_can( 'administrator' ) ) { |
| 28 | + wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 29 | + } |
| 30 | + if ( isset($_POST['submit']) ){ |
| 31 | + $schema = array( |
| 32 | + 'type' => $_POST['type'], |
| 33 | + 'default_image' => $_POST['default_image'], |
| 34 | + 'org_name' => $_POST['org_name'], |
| 35 | + 'org_logo' => $_POST['org_logo'], |
| 36 | + 'activated' => $_POST['activated'] |
| 37 | + ); |
| 38 | + update_option('json_structuring_markup_article', serialize( $schema ) ); |
| 39 | + } |
| 40 | + $schema_article_settings = unserialize( get_option('json_structuring_markup_article') ); |
| 41 | + if( isset($schema_article_settings) ){ |
| 42 | + $default_image = $schema_article_settings['default_image']; |
| 43 | + $org_name = $schema_article_settings['org_name']; |
| 44 | + $org_logo = $schema_article_settings['org_logo']; |
| 45 | + $activated = esc_attr( $schema_article_settings['activated'] ); |
| 46 | + $schema_activated_checked = ''; |
| 47 | + if($activated == 'on'){ |
| 48 | + $schema_activated_checked = 'checked'; |
| 49 | + } |
| 50 | + }else{ |
| 51 | + $default_image = ''; |
| 52 | + $org_name = get_bloginfo('name'); |
| 53 | + $org_logo = ''; |
| 54 | + $activated = 'off'; |
| 55 | + } |
| 56 | + echo '<div class="wrap">'; |
| 57 | + echo '<h1>Article SCHEMA Markup SETTINGS</h1>'; |
| 58 | + echo '<div>Provide the fields to make the validated schema for article.</div>'; |
| 59 | + echo '<form enctype="multipart/form-data" action="" method="POST" id="article-schema">'; |
| 60 | +?> |
| 61 | + <table class="schema-admin-table"> |
| 62 | + <caption>Basic Setting</caption> |
| 63 | + <tbody> |
| 64 | + <tr> |
| 65 | + <th>headline :</th> |
| 66 | + <td><small>Default : post_title</small></td> |
| 67 | + </tr> |
| 68 | + <tr> |
| 69 | + <th>datePublished :</th> |
| 70 | + <td><small>Default : get_the_time( DATE_ISO8601, ID )</small></td> |
| 71 | + </tr> |
| 72 | + <tr> |
| 73 | + <th>dateModified :</th> |
| 74 | + <td><small>Default : get_the_modified_time( DATE_ISO8601, false, ID )</small></td> |
| 75 | + </tr> |
| 76 | + <tr> |
| 77 | + <th>description :</th> |
| 78 | + <td><small>Default : post_excerpt</small></td> |
| 79 | + </tr> |
| 80 | + </tbody> |
| 81 | + </table> |
| 82 | + |
| 83 | + <table class="schema-admin-table"> |
| 84 | + <caption>mainEntityOfPage ( recommended )</caption> |
| 85 | + <tbody> |
| 86 | + <tr> |
| 87 | + <th>@type :</th> |
| 88 | + <td><small>"WebPage"</small></td> |
| 89 | + </tr> |
| 90 | + <tr> |
| 91 | + <th>@id :</th> |
| 92 | + <td><small>Default : get_permalink( ID )</small></td> |
| 93 | + </tr> |
| 94 | + </tbody> |
| 95 | + </table> |
| 96 | + |
| 97 | + <table class="schema-admin-table"> |
| 98 | + <caption>image ( required )</caption> |
| 99 | + <tbody> |
| 100 | + <tr> |
| 101 | + <th>@type :</th> |
| 102 | + <td><small>"ImageObject"</small></td> |
| 103 | + </tr> |
| 104 | + <tr> |
| 105 | + <th><label for="upload_image">url :</label></th> |
| 106 | + <td> |
| 107 | + <input type="text" name="default_image" id="logo" class="regular-text" required value="<?php echo esc_attr( $default_image ); ?>" /><small>Default: thumbnail(Featured Image)<br />When Featured image is not available then this image would be shown in the markup</small> |
| 108 | + </td> |
| 109 | + </tr> |
| 110 | + <tr> |
| 111 | + <th>height :</th> |
| 112 | + <td><small>Auto : The height of the image, in pixels.</small></td> |
| 113 | + </tr> |
| 114 | + <tr> |
| 115 | + <th>width :</th> |
| 116 | + <td><small>Auto : The width of the image, in pixels. Images should be at least 696 pixels wide.</small></td> |
| 117 | + </tr> |
| 118 | + </tbody> |
| 119 | + </table> |
| 120 | + |
| 121 | + <table class="schema-admin-table"> |
| 122 | + <caption>publisher( required )</caption> |
| 123 | + <tbody> |
| 124 | + <tr> |
| 125 | + <th>@type :</th> |
| 126 | + <td><small>"Organization"</small></td> |
| 127 | + </tr> |
| 128 | + <tr> |
| 129 | + <th><label for="name">Organization Name :</label></th> |
| 130 | + <td><input type="text" name="org_name" id="name" class="regular-text" required value="<?php echo $org_name; ?>" /><small>Default : bloginfo("name")</small></td> |
| 131 | + </tr> |
| 132 | + </tbody> |
| 133 | + </table> |
| 134 | + |
| 135 | + <table class="schema-admin-table"> |
| 136 | + <caption>logo ( required )</caption> |
| 137 | + <tbody> |
| 138 | + <tr> |
| 139 | + <th>@type :</th> |
| 140 | + <td><small>"ImageObject"</small></td> |
| 141 | + </tr> |
| 142 | + <tr> |
| 143 | + <th><label for="logo">url :</label></th> |
| 144 | + <td><input type="text" name="org_logo" id="logo" class="regular-text" required value="<?php echo $org_logo; ?>" /></td> |
| 145 | + </tr> |
| 146 | + <tr> |
| 147 | + <th>height :</th> |
| 148 | + <td><small>Auto : height >= 60px.</small></td> |
| 149 | + </tr> |
| 150 | + <tr> |
| 151 | + <th>width :</th> |
| 152 | + <td><small>Auto : width <= 600px.</small></td> |
| 153 | + </tr> |
| 154 | + </tbody> |
| 155 | + </table> |
| 156 | + |
| 157 | + <table class="schema-admin-table"> |
| 158 | + <tbody> |
| 159 | + <tr> |
| 160 | + <td><input type="checkbox" name="activated" value="on" <?php echo $schema_activated_checked; ?> /><strong>Activate SCHEMA.ORG Article</strong></td> |
| 161 | + </tr> |
| 162 | + </tbody> |
| 163 | + </table> |
| 164 | + |
| 165 | + <p>Setting Knowledge : <a href="https://developers.google.com/structured-data/rich-snippets/articles" title="Article Schema" target="_blank">https://developers.google.com/structured-data/rich-snippets/articles</a></p> |
| 166 | +<?php |
| 167 | + submit_button(); |
| 168 | + echo '</form>'; |
| 169 | + echo '</div>'; |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Print JSON-LD Article schema markup |
| 174 | + */ |
| 175 | +function json_structuring_markup_display_article(){ |
| 176 | + $schema_article_settings = unserialize( get_option('json_structuring_markup_article') ); |
| 177 | + $activated = ''; |
| 178 | + if( isset($schema_article_settings) ) { |
| 179 | + $default_image = esc_attr( $schema_article_settings['default_image'] ); |
| 180 | + $org_name = esc_attr( $schema_article_settings['org_name'] ); |
| 181 | + $org_logo = esc_attr( $schema_article_settings['org_logo'] ); |
| 182 | + $activated = esc_attr( $schema_article_settings['activated'] ); |
| 183 | + } |
| 184 | + if ( $activated == 'on' ) { |
| 185 | + global $post; |
| 186 | + $images = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
| 187 | + if( isset($images) && empty($images) ) { |
| 188 | + $images[0] = $default_image; |
| 189 | + } |
| 190 | + $excerpt = json_structuring_markup_escape_text_tags($post->post_excerpt); |
| 191 | + $content = $excerpt === "" ? mb_substr( json_structuring_markup_escape_text_tags( $post->post_content ), 0, 110 ) : $excerpt; |
| 192 | + $args = array( |
| 193 | + "@context" => "http://schema.org", |
| 194 | + "@type" => "Article", |
| 195 | + "mainEntityOfPage" => array( |
| 196 | + "@type" => "WebPage", |
| 197 | + "@id" => get_permalink( $post->ID ) |
| 198 | + ), |
| 199 | + "headline" => json_structuring_markup_escape_text_tags( $post->post_title ), |
| 200 | + "image" => array( |
| 201 | + "@type" => "ImageObject", |
| 202 | + "url" => $images[0], |
| 203 | + "width" => "auto", |
| 204 | + "height" => "auto" |
| 205 | + ), |
| 206 | + "datePublished" => get_the_time( DATE_ISO8601, $post->ID ), |
| 207 | + "dateModified" => get_post_modified_time( DATE_ISO8601, __return_false(), $post->ID ), |
| 208 | + "author" => array( |
| 209 | + "@type" => "Person", |
| 210 | + "name" => json_structuring_markup_escape_text_tags( get_the_author_meta( 'display_name', $post->post_author ) ) |
| 211 | + ), |
| 212 | + "publisher" => array( |
| 213 | + "@type" => "Organization", |
| 214 | + "name" => $org_name, |
| 215 | + "logo" => array( |
| 216 | + "@type" => "ImageObject", |
| 217 | + "url" => $org_logo, |
| 218 | + "width" => "auto", |
| 219 | + "height" => "auto" |
| 220 | + ) |
| 221 | + ), |
| 222 | + "description" => $content |
| 223 | + ); |
| 224 | + json_structuring_markup_set_schema_json( $args ); |
| 225 | + } |
| 226 | +} |
0 commit comments