metadata.php
239 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Gives the page details for the services |
| 4 | * |
| 5 | */ |
| 6 | |
| 7 | if( ! defined( 'ABSPATH' ) ) exit; |
| 8 | |
| 9 | class Shortcoder_Metadata{ |
| 10 | |
| 11 | public static function init(){ |
| 12 | |
| 13 | } |
| 14 | |
| 15 | public static function metadata(){ |
| 16 | |
| 17 | global $post; |
| 18 | |
| 19 | $d = array(); |
| 20 | $defaults = array( |
| 21 | 'title' => '', |
| 22 | 'url' => '', |
| 23 | 'short_url' => '', |
| 24 | |
| 25 | 'post_id' => '', |
| 26 | 'post_excerpt' => '', |
| 27 | 'post_comments_count' => '', |
| 28 | 'post_image' => '', |
| 29 | 'post_author' => '', |
| 30 | 'post_date' => '', |
| 31 | 'post_modified_date' => '', |
| 32 | 'post_slug' => '', |
| 33 | |
| 34 | 'site_name' => get_bloginfo( 'name' ), |
| 35 | 'site_description' => get_bloginfo( 'description' ), |
| 36 | 'site_url' => get_bloginfo( 'url' ), |
| 37 | 'site_wpurl' => get_bloginfo( 'wpurl' ), |
| 38 | 'site_charset' => get_bloginfo( 'charset' ), |
| 39 | 'wp_version' => get_bloginfo( 'version' ), |
| 40 | 'stylesheet_url' => get_bloginfo( 'stylesheet_url' ), |
| 41 | 'stylesheet_directory' => get_bloginfo( 'stylesheet_directory' ), |
| 42 | 'template_url' => get_bloginfo( 'template_url' ), |
| 43 | 'atom_url' => get_bloginfo( 'atom_url' ), |
| 44 | 'rss_url' => get_bloginfo( 'rss2_url' ), |
| 45 | |
| 46 | 'day' => date_i18n( 'j' ), |
| 47 | 'day_lz' => date_i18n( 'd' ), |
| 48 | 'day_ws' => date_i18n( 'D' ), |
| 49 | 'day_wf' => date_i18n( 'l' ), |
| 50 | 'month' => date_i18n( 'n' ), |
| 51 | 'month_lz' => date_i18n( 'm' ), |
| 52 | 'month_ws' => date_i18n( 'M' ), |
| 53 | 'month_wf' => date_i18n( 'F' ), |
| 54 | 'year' => date_i18n( 'Y' ), |
| 55 | 'year_2d' => date_i18n( 'y' ), |
| 56 | |
| 57 | ); |
| 58 | |
| 59 | if( in_the_loop()) { |
| 60 | |
| 61 | $d = self::meta_by_id( get_the_ID() ); |
| 62 | |
| 63 | }else{ |
| 64 | |
| 65 | if( is_home() && get_option( 'show_on_front' ) == 'page' ){ |
| 66 | |
| 67 | $d = self::meta_by_id( get_option( 'page_for_posts' ) ); |
| 68 | |
| 69 | }elseif( is_front_page() || ( is_home() && ( get_option( 'show_on_front' ) == 'posts' || !get_option( 'page_for_posts' ) ) ) ){ |
| 70 | |
| 71 | $d = array( |
| 72 | 'title' => get_bloginfo( 'name' ), |
| 73 | 'url' => get_bloginfo( 'url' ), |
| 74 | 'post_excerpt' => get_bloginfo( 'description' ), |
| 75 | 'short_url' => get_bloginfo( 'url' ), |
| 76 | ); |
| 77 | |
| 78 | }elseif( is_singular() ){ |
| 79 | |
| 80 | if( is_object( $post ) ){ |
| 81 | $d = self::meta_by_id( $post->ID ); |
| 82 | } |
| 83 | |
| 84 | }elseif( is_tax() || is_tag() || is_category() ){ |
| 85 | |
| 86 | $term = get_queried_object(); |
| 87 | $d = array( |
| 88 | 'title' => wp_title( '', false ), |
| 89 | 'url' => get_term_link( $term, $term->taxonomy ), |
| 90 | 'post_excerpt' => $term->description |
| 91 | ); |
| 92 | |
| 93 | }elseif( function_exists( 'get_post_type_archive_link' ) && is_post_type_archive() ){ |
| 94 | |
| 95 | $post_type = get_query_var( 'post_type' ); |
| 96 | $post_type_obj = get_post_type_object( $post_type ); |
| 97 | |
| 98 | $d = array( |
| 99 | 'title' => wp_title( '', false ), |
| 100 | 'url' => get_post_type_archive_link( $post_type ), |
| 101 | 'post_excerpt' => $post_type_obj->description |
| 102 | ); |
| 103 | |
| 104 | }elseif( is_date() ){ |
| 105 | |
| 106 | if( is_day() ){ |
| 107 | |
| 108 | $d = array( |
| 109 | 'title' => wp_title( '', false ), |
| 110 | 'url' => get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) |
| 111 | ); |
| 112 | |
| 113 | }elseif( is_month() ){ |
| 114 | |
| 115 | $d = array( |
| 116 | 'title' => wp_title( '', false ), |
| 117 | 'url' => get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) |
| 118 | ); |
| 119 | |
| 120 | }elseif( is_year() ){ |
| 121 | |
| 122 | $d = array( |
| 123 | 'title' => wp_title( '', false ), |
| 124 | 'url' => get_year_link( get_query_var( 'year' ) ) |
| 125 | ); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | }elseif( is_author() ){ |
| 130 | |
| 131 | $d = array( |
| 132 | 'title' => wp_title( '', false ), |
| 133 | 'url' => get_author_posts_url( get_query_var( 'author' ), get_query_var( 'author_name' ) ) |
| 134 | ); |
| 135 | |
| 136 | }elseif( is_search() ){ |
| 137 | |
| 138 | $d = array( |
| 139 | 'title' => wp_title( '', false ), |
| 140 | 'url' => get_search_link() |
| 141 | ); |
| 142 | |
| 143 | }elseif( is_404() ){ |
| 144 | |
| 145 | $d = array( |
| 146 | 'title' => wp_title( '', false ), |
| 147 | 'url' => home_url( esc_url( $_SERVER['REQUEST_URI'] ) ) |
| 148 | ); |
| 149 | |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | $meta = wp_parse_args( $d, $defaults ); |
| 154 | foreach( $meta as $key => $val ){ |
| 155 | if( is_string( $val ) ){ |
| 156 | $val = trim( $val ); |
| 157 | } |
| 158 | $meta[ $key ] = $val; |
| 159 | } |
| 160 | $meta = apply_filters( 'sc_mod_metadata', $meta ); |
| 161 | |
| 162 | return $meta; |
| 163 | |
| 164 | } |
| 165 | |
| 166 | public static function meta_by_id( $id ){ |
| 167 | |
| 168 | $d = array(); |
| 169 | |
| 170 | if( $id ){ |
| 171 | $d = array( |
| 172 | 'title' => get_the_title( $id ), |
| 173 | 'url' => get_permalink( $id ), |
| 174 | 'short_url' => wp_get_shortlink( $id ), |
| 175 | |
| 176 | 'post_id' => $id, |
| 177 | 'post_excerpt' => self::excerpt( 100 ), |
| 178 | 'post_comments_count' => get_comments_number( $id ), |
| 179 | 'post_image' => self::post_image( $id ), |
| 180 | 'post_author' => get_the_author(), |
| 181 | 'post_date' => get_the_date(), |
| 182 | 'post_modified_date' => get_the_modified_date(), |
| 183 | 'post_slug' => self::post_slug() |
| 184 | ); |
| 185 | |
| 186 | if( $d[ 'short_url' ] == '' ){ |
| 187 | $d[ 'short_url' ] = $d[ 'url' ]; |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
| 192 | return $d; |
| 193 | |
| 194 | } |
| 195 | |
| 196 | public static function excerpt( $length = 250 ){ |
| 197 | |
| 198 | global $post; |
| 199 | |
| 200 | if( !is_object( $post ) ){ |
| 201 | return ''; |
| 202 | } |
| 203 | |
| 204 | $excerpt = $post->post_excerpt; // using $post->post_excerpt instead of get_the_excerpt as the_content filter loses shortcode formatting |
| 205 | |
| 206 | $excerpt_text = ( empty( $excerpt ) ) ? strip_tags( strip_shortcodes( $post->post_content ) ) : $excerpt; |
| 207 | return substr( $excerpt_text, 0, $length ); |
| 208 | |
| 209 | } |
| 210 | |
| 211 | public static function post_image( $post_id ){ |
| 212 | |
| 213 | $thumbnail = get_the_post_thumbnail_url( $post_id ); |
| 214 | |
| 215 | if( $thumbnail === false ){ |
| 216 | return ''; |
| 217 | }else{ |
| 218 | return $thumbnail; |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | public static function post_slug(){ |
| 224 | |
| 225 | global $post; |
| 226 | |
| 227 | if( !is_object( $post ) ){ |
| 228 | return ''; |
| 229 | } |
| 230 | |
| 231 | return $post->post_name; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | Shortcoder_Metadata::init(); |
| 238 | |
| 239 | ?> |