metadata.php
215 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 | 'day' => current_time( 'j' ), |
| 43 | 'day_lz' => current_time( 'd' ), |
| 44 | 'day_ws' => current_time( 'D' ), |
| 45 | 'day_wf' => current_time( 'l' ), |
| 46 | 'month' => current_time( 'n' ), |
| 47 | 'month_lz' => current_time( 'm' ), |
| 48 | 'month_ws' => current_time( 'M' ), |
| 49 | 'month_wf' => current_time( 'F' ), |
| 50 | 'year' => current_time( 'Y' ), |
| 51 | 'year_2d' => current_time( 'y' ), |
| 52 | |
| 53 | ); |
| 54 | |
| 55 | if( in_the_loop()) { |
| 56 | |
| 57 | $d = self::meta_by_id( get_the_ID() ); |
| 58 | |
| 59 | }else{ |
| 60 | |
| 61 | if( is_home() && get_option( 'show_on_front' ) == 'page' ){ |
| 62 | |
| 63 | $d = self::meta_by_id( get_option( 'page_for_posts' ) ); |
| 64 | |
| 65 | }elseif( is_front_page() || ( is_home() && ( get_option( 'show_on_front' ) == 'posts' || !get_option( 'page_for_posts' ) ) ) ){ |
| 66 | |
| 67 | $d = array( |
| 68 | 'title' => get_bloginfo( 'name' ), |
| 69 | 'url' => get_bloginfo( 'url' ), |
| 70 | 'post_excerpt' => get_bloginfo( 'description' ), |
| 71 | 'short_url' => get_bloginfo( 'url' ), |
| 72 | ); |
| 73 | |
| 74 | }elseif( is_singular() ){ |
| 75 | |
| 76 | if( is_object( $post ) ){ |
| 77 | $d = self::meta_by_id( $post->ID ); |
| 78 | } |
| 79 | |
| 80 | }elseif( is_tax() || is_tag() || is_category() ){ |
| 81 | |
| 82 | $term = get_queried_object(); |
| 83 | $d = array( |
| 84 | 'title' => wp_title( '', false ), |
| 85 | 'url' => get_term_link( $term, $term->taxonomy ), |
| 86 | 'post_excerpt' => $term->description |
| 87 | ); |
| 88 | |
| 89 | }elseif( function_exists( 'get_post_type_archive_link' ) && is_post_type_archive() ){ |
| 90 | |
| 91 | $post_type = get_query_var( 'post_type' ); |
| 92 | $post_type_obj = get_post_type_object( $post_type ); |
| 93 | |
| 94 | $d = array( |
| 95 | 'title' => wp_title( '', false ), |
| 96 | 'url' => get_post_type_archive_link( $post_type ), |
| 97 | 'post_excerpt' => $post_type_obj->description |
| 98 | ); |
| 99 | |
| 100 | }elseif( is_date() ){ |
| 101 | |
| 102 | if( is_day() ){ |
| 103 | |
| 104 | $d = array( |
| 105 | 'title' => wp_title( '', false ), |
| 106 | 'url' => get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) |
| 107 | ); |
| 108 | |
| 109 | }elseif( is_month() ){ |
| 110 | |
| 111 | $d = array( |
| 112 | 'title' => wp_title( '', false ), |
| 113 | 'url' => get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) |
| 114 | ); |
| 115 | |
| 116 | }elseif( is_year ){ |
| 117 | |
| 118 | $d = array( |
| 119 | 'title' => wp_title( '', false ), |
| 120 | 'url' => get_year_link( get_query_var( 'year' ) ) |
| 121 | ); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | }elseif( is_author() ){ |
| 126 | |
| 127 | $d = array( |
| 128 | 'title' => wp_title( '', false ), |
| 129 | 'url' => get_author_posts_url( get_query_var( 'author' ), get_query_var( 'author_name' ) ) |
| 130 | ); |
| 131 | |
| 132 | }elseif( is_search() ){ |
| 133 | |
| 134 | $d = array( |
| 135 | 'title' => wp_title( '', false ), |
| 136 | 'url' => get_search_link() |
| 137 | ); |
| 138 | |
| 139 | }elseif( is_404() ){ |
| 140 | |
| 141 | $d = array( |
| 142 | 'title' => wp_title( '', false ), |
| 143 | 'url' => home_url( esc_url( $_SERVER['REQUEST_URI'] ) ) |
| 144 | ); |
| 145 | |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | $meta = wp_parse_args( $d, $defaults ); |
| 150 | $meta = array_map( 'trim', $meta ); |
| 151 | $meta = apply_filters( 'sc_mod_metadata', $meta ); |
| 152 | |
| 153 | return $meta; |
| 154 | |
| 155 | } |
| 156 | |
| 157 | public static function meta_by_id( $id ){ |
| 158 | |
| 159 | $d = array(); |
| 160 | |
| 161 | if( $id ){ |
| 162 | $d = array( |
| 163 | 'title' => get_the_title( $id ), |
| 164 | 'url' => get_permalink( $id ), |
| 165 | 'short_url' => wp_get_shortlink( $id ), |
| 166 | |
| 167 | 'post_id' => $id, |
| 168 | 'post_excerpt' => self::excerpt( 100 ), |
| 169 | 'post_comments_count' => get_comments_number( $id ), |
| 170 | 'post_image' => self::post_image( $id ), |
| 171 | 'post_author' => get_the_author(), |
| 172 | 'post_date' => get_the_date() |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | if( $d[ 'short_url' ] == '' ){ |
| 177 | $d[ 'short_url' ] = $d[ 'url' ]; |
| 178 | } |
| 179 | |
| 180 | return $d; |
| 181 | |
| 182 | } |
| 183 | |
| 184 | public static function excerpt( $length = 250 ){ |
| 185 | |
| 186 | global $post; |
| 187 | |
| 188 | if( !is_object( $post ) ){ |
| 189 | return ''; |
| 190 | } |
| 191 | |
| 192 | $excerpt = $post->post_excerpt; // using $post->post_excerpt instead of get_the_excerpt as the_content filter loses shortcode formatting |
| 193 | |
| 194 | $excerpt_text = ( empty( $excerpt ) ) ? strip_tags( strip_shortcodes( $post->post_content ) ) : $excerpt; |
| 195 | return substr( $excerpt_text, 0, $length ); |
| 196 | |
| 197 | } |
| 198 | |
| 199 | public static function post_image( $post_id ){ |
| 200 | |
| 201 | $thumbnail = get_the_post_thumbnail_url( $post_id ); |
| 202 | |
| 203 | if( $thumbnail === false ){ |
| 204 | return ''; |
| 205 | }else{ |
| 206 | return $thumbnail; |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | |
| 211 | } |
| 212 | |
| 213 | Shortcoder_Metadata::init(); |
| 214 | |
| 215 | ?> |