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