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