| @@ -1,409 +1,584 @@ | ||
| 1 | 1 | <?php |
| 2 | - if(!function_exists('blockspare_render_block_core_latest_posts_list')){ | |
| 3 | - function blockspare_render_block_core_latest_posts_list($attributes) | |
| 4 | - { | |
| 5 | - | |
| 6 | - ob_start(); | |
| 7 | - $unq_class = mt_rand(100000,999999); | |
| 8 | - $blockuniqueclass = ''; | |
| 9 | - | |
| 10 | - if($attributes['enableTwoColumn']){ | |
| 11 | - $design = $attributes['design'].' list-col-2'; | |
| 12 | - }else{ | |
| 13 | - $design = $attributes['design'].' list-col-1'; | |
| 14 | - } | |
| 15 | - | |
| 16 | - if(!empty($attributes['uniqueClass'])){ | |
| 17 | - $blockuniqueclass = $attributes['uniqueClass']; | |
| 18 | - }else{ | |
| 19 | - $blockuniqueclass = 'blockspare-posts-block-list-'.$unq_class; | |
| 20 | - } | |
| 21 | - | |
| 22 | - $block_class = 'blockspare-posts-block-is-list'; | |
| 23 | - blockspare_query_loop_and_wrapper($attributes,$block_class,$design); | |
| 24 | - | |
| 25 | - $data_content = blockspare_post_style_css($blockuniqueclass ,$attributes); | |
| 26 | - $data_content .= ob_get_clean(); | |
| 27 | - return $data_content; | |
| 28 | - | |
| 29 | - } | |
| 30 | -} | |
| 2 | + | |
| 3 | +function blockspare_blocks_render_block_core_latest_posts_list($attributes) | |
| 4 | +{ | |
| 31 | 5 | |
| 32 | - /** | |
| 33 | - * Registers the post grid block on server | |
| 34 | - */ | |
| 35 | - if(!function_exists('blockspare_register_block_core_latest_posts_list')){ | |
| 36 | - function blockspare_register_block_core_latest_posts_list() | |
| 37 | - { | |
| 6 | + $unq_class = mt_rand(100000,999999); | |
| 7 | + $blockuniqueclass = ''; | |
| 38 | 8 | |
| 39 | - if (!function_exists('register_block_type')) { | |
| 40 | - return; | |
| 41 | - } | |
| 42 | - | |
| 43 | - | |
| 44 | - ob_start(); | |
| 45 | - include BLOCKSPARE_PLUGIN_DIR . 'src/blocks/latest-posts-list/block.json'; | |
| 46 | - $metadata = json_decode(ob_get_clean(), true); | |
| 47 | - | |
| 48 | - | |
| 49 | - /* Block attributes */ | |
| 50 | - register_block_type( | |
| 51 | - 'blockspare/blockspare-latest-posts-list', | |
| 52 | - array( | |
| 53 | - 'attributes' =>$metadata['attributes'], | |
| 54 | - 'render_callback' => 'blockspare_render_block_core_latest_posts_list', | |
| 55 | - ) | |
| 56 | - ); | |
| 9 | + if(!empty($attributes['uniqueClass'])){ | |
| 10 | + $blockuniqueclass = $attributes['uniqueClass']; | |
| 11 | + }else{ | |
| 12 | + $blockuniqueclass = 'blockspare-list-'.$unq_class; | |
| 57 | 13 | } |
| 58 | 14 | |
| 59 | - add_action('init', 'blockspare_register_block_core_latest_posts_list'); | |
| 60 | -} | |
| 15 | + | |
| 16 | + $categories = isset($attributes['categories']) ? $attributes['categories'] : ''; | |
| 17 | + | |
| 18 | + /* Setup the query */ | |
| 19 | + $grid_query = new WP_Query( | |
| 20 | + array( | |
| 21 | + 'posts_per_page' => $attributes['postsToShow'], | |
| 22 | + 'post_status' => 'publish', | |
| 23 | + 'order' => $attributes['order'], | |
| 24 | + 'orderby' => $attributes['orderBy'], | |
| 25 | + 'cat' => $categories, | |
| 26 | + 'offset' => $attributes['offset'], | |
| 27 | + 'post_type' => $attributes['postType'], | |
| 28 | + 'ignore_sticky_posts' => 1, | |
| 29 | + ) | |
| 30 | + ); | |
| 31 | + | |
| 32 | + $blockspare_content_markup = ''; | |
| 33 | + | |
| 34 | + /* Start the loop */ | |
| 35 | + if ($grid_query->have_posts()) { | |
| 36 | + | |
| 37 | + while ($grid_query->have_posts()) { | |
| 38 | + $grid_query->the_post(); | |
| 39 | + | |
| 40 | + /* Setup the post ID */ | |
| 41 | + $post_id = get_the_ID(); | |
| 42 | + | |
| 43 | + /* Setup the featured image ID */ | |
| 44 | + $post_thumb_id = get_post_thumbnail_id($post_id); | |
| 61 | 45 | |
| 46 | + $has_img_class = ''; | |
| 62 | 47 | |
| 63 | - /** | |
| 64 | - * Create API fields for additional info | |
| 65 | - */ | |
| 66 | - if(!function_exists('blockspare_post_register_rest_fields')){ | |
| 67 | - function blockspare_post_register_rest_fields() | |
| 68 | - { | |
| 69 | - | |
| 70 | - register_rest_field('post', 'featured_image_urls', | |
| 71 | - array( | |
| 72 | - 'get_callback' => 'blockspare_featured_image_urls', | |
| 73 | - 'update_callback' => null, | |
| 74 | - 'schema' => array( | |
| 75 | - 'description' => __('Different sized featured images', 'blockspare'), | |
| 76 | - 'type' => 'array', | |
| 77 | - ), | |
| 78 | - ) | |
| 79 | - ); | |
| 80 | - | |
| 81 | - // Excer | |
| 82 | - | |
| 83 | - /* Add author info */ | |
| 84 | - register_rest_field( | |
| 85 | - 'post', | |
| 86 | - 'author_info', | |
| 87 | - array( | |
| 88 | - 'get_callback' => 'blockspare_get_author_infos', | |
| 89 | - 'update_callback' => null, | |
| 90 | - 'schema' => null, | |
| 91 | - ) | |
| 92 | - ); | |
| 93 | - | |
| 94 | - /* Add author info */ | |
| 95 | - register_rest_field( | |
| 96 | - 'post', | |
| 97 | - 'category_info', | |
| 98 | - array( | |
| 99 | - 'get_callback' => 'blockspare_get_category_infos', | |
| 100 | - 'update_callback' => null, | |
| 101 | - 'schema' => null, | |
| 102 | - ) | |
| 103 | - ); | |
| 104 | - | |
| 105 | - /* Add author info */ | |
| 106 | - register_rest_field( | |
| 107 | - 'post', | |
| 108 | - 'tag_info', | |
| 109 | - array( | |
| 110 | - 'get_callback' => 'blockspare_get_tag_infos', | |
| 111 | - 'update_callback' => null, | |
| 112 | - 'schema' => null, | |
| 113 | - ) | |
| 114 | - ); | |
| 115 | - | |
| 116 | - register_rest_field( | |
| 117 | - 'post', | |
| 118 | - 'comment_count', | |
| 119 | - array( | |
| 120 | - 'get_callback' => 'blockspare_get_comment_count', | |
| 121 | - 'update_callback' => null, | |
| 122 | - 'schema' => null, | |
| 123 | - ) | |
| 124 | - ); | |
| 125 | - } | |
| 126 | - | |
| 127 | - add_action('rest_api_init', 'blockspare_post_register_rest_fields'); | |
| 128 | -} | |
| 129 | - | |
| 130 | - | |
| 131 | - /** | |
| 132 | - * Get author info for the rest field | |
| 133 | - * | |
| 134 | - * @param String $object The object type. | |
| 135 | - * @param String $field_name Name of the field to retrieve. | |
| 136 | - * @param String $request The current request object. | |
| 137 | - */ | |
| 138 | - if(!function_exists('blockspare_get_author_infos')){ | |
| 139 | - function blockspare_get_author_infos($object, $field_name, $request){ | |
| 140 | - if(!isset($object['author'])) | |
| 141 | - return; | |
| 48 | + if(!$post_thumb_id){ | |
| 49 | + $has_img_class = "post-has-no-image"; | |
| 50 | + } | |
| 51 | + | |
| 52 | + /* Setup the post classes */ | |
| 53 | + $post_classes = 'blockspare-post-single '; | |
| 54 | + | |
| 55 | + /* Add sticky class */ | |
| 56 | + if (is_sticky($post_id)) { | |
| 57 | + $post_classes .= ' sticky'; | |
| 58 | + } else { | |
| 59 | + $post_classes .= null; | |
| 60 | + } | |
| 61 | + | |
| 62 | + /* Join classes together */ | |
| 63 | + //$post_classes = join(' ', get_post_class($post_classes, $post_id)); | |
| 64 | + | |
| 65 | + | |
| 66 | + if ($attributes['enableBackgroundColor']) { | |
| 67 | + $post_classes .= 'has-background'; | |
| 68 | + } | |
| 142 | 69 | |
| 143 | - /* Get the author name */ | |
| 144 | - $author_data['display_name'] = get_the_author_meta('display_name', $object['author']); | |
| 145 | 70 | |
| 146 | - /* Get the author link */ | |
| 147 | - $author_data['author_link'] = get_author_posts_url($object['author']); | |
| 148 | - | |
| 149 | - /* Return the author data */ | |
| 150 | - return $author_data; | |
| 151 | - } | |
| 152 | - } | |
| 153 | - if(!function_exists('blockspare_get_category_infos')){ | |
| 154 | - function blockspare_get_category_infos($object, $field_name, $request){ | |
| 155 | - return get_the_category_list(' ', '', $object['id']); | |
| 156 | - | |
| 157 | - } | |
| 158 | - } | |
| 159 | - | |
| 160 | - if(!function_exists('blockspare_get_tag_infos')){ | |
| 161 | - function blockspare_get_tag_infos($object, $field_name, $request) { | |
| 162 | - | |
| 163 | - ob_start(); | |
| 164 | - $cate_name = ''; | |
| 165 | - if (!empty($object)) { | |
| 166 | - foreach ($object['categories'] as $cat_id) { | |
| 167 | - $cate_name = get_cat_name($cat_id); | |
| 71 | + | |
| 72 | + /* Start the markup for the post */ | |
| 73 | + $blockspare_content_markup .= sprintf( | |
| 74 | + '<div id="post-%1$s" class="%2$s %3$s">', | |
| 75 | + esc_attr($post_id), | |
| 76 | + esc_attr($post_classes), | |
| 77 | + $has_img_class | |
| 78 | + | |
| 79 | + | |
| 80 | + ); | |
| 81 | + | |
| 82 | + /* Get the featured image */ | |
| 83 | + if (isset($attributes['displayPostImage']) && $attributes['displayPostImage'] && $post_thumb_id) { | |
| 84 | + | |
| 85 | + | |
| 86 | + if (!empty($attributes['imageSize'])) { | |
| 87 | + $post_thumb_size = $attributes['imageSize']; | |
| 168 | 88 | } |
| 89 | + | |
| 90 | + if (has_post_thumbnail($post_id)) { | |
| 91 | + /* Output the featured image */ | |
| 92 | + $blockspare_content_markup .= sprintf( | |
| 93 | + '<figure class="blockspare-post-img"><a href="%1$s" rel="bookmark" aria-hidden="true" tabindex="-1" >%2$s</a></figure>', | |
| 94 | + esc_url(get_permalink($post_id)), | |
| 95 | + wp_kses_post(wp_get_attachment_image($post_thumb_id, $post_thumb_size)) | |
| 96 | + | |
| 97 | + ); | |
| 98 | + } | |
| 99 | + | |
| 169 | 100 | } |
| 170 | - ob_clean(); | |
| 171 | - return $cate_name; | |
| 101 | + | |
| 102 | + | |
| 103 | + /* Wrap the text content */ | |
| 104 | + $blockspare_content_markup .= sprintf( | |
| 105 | + '<div class="blockspare-post-content %1$s">', | |
| 106 | + $attributes['contentOrder'] | |
| 107 | + ); | |
| 108 | + | |
| 109 | + $blockspare_content_markup .= sprintf( | |
| 110 | + '<header class="blockspare-block-post-grid-header">' | |
| 111 | + ); | |
| 112 | + if ($attributes['displayPostCategory']) { | |
| 113 | + $blockspare_content_markup .= sprintf( | |
| 114 | + '<div class="blockspare-post-category" >%1$s</div>', | |
| 115 | + get_the_category_list(esc_html__(' ', 'blockspare'), '', $post_id) | |
| 116 | + | |
| 117 | + ); | |
| 118 | + } | |
| 119 | + | |
| 120 | + | |
| 121 | + /* Get the post title */ | |
| 122 | + $title = get_the_title($post_id); | |
| 123 | + | |
| 124 | + if (!$title) { | |
| 125 | + $title = __('Untitled', 'blockspare'); | |
| 126 | + } | |
| 127 | + | |
| 128 | + if (isset($attributes['displayPostTitle']) && $attributes['displayPostTitle']) { | |
| 129 | + | |
| 130 | + if (isset($attributes['postTitleTag'])) { | |
| 131 | + $post_title_tag = $attributes['postTitleTag']; | |
| 132 | + } else { | |
| 133 | + $post_title_tag = 'h4'; | |
| 134 | + } | |
| 135 | + | |
| 136 | + $blockspare_content_markup .= sprintf( | |
| 137 | + '<%3$s class="blockspare-block-post-grid-title"><a href="%1$s" class="blockspare-title-link" rel="bookmark"><span>%2$s</span></a></%3$s>', | |
| 138 | + esc_url(get_permalink($post_id)), | |
| 139 | + esc_html($title), | |
| 140 | + esc_attr($post_title_tag) | |
| 141 | + ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + if (isset($attributes['postType']) && ($attributes['postType'] === 'post') && (isset($attributes['displayPostAuthor']) || isset($attributes['displayPostDate']))) { | |
| 145 | + /* Wrap the byline content */ | |
| 146 | + $blockspare_content_markup .= sprintf( | |
| 147 | + '<div class="blockspare-block-post-grid-byline">' | |
| 148 | + ); | |
| 149 | + | |
| 150 | + /* Get the post author */ | |
| 151 | + if (isset($attributes['displayPostAuthor']) && $attributes['displayPostAuthor']) { | |
| 152 | + $blockspare_content_markup .= sprintf( | |
| 153 | + '<div class="blockspare-block-post-grid-author"><a class="blockspare-text-link" href="%2$s" itemprop="url" rel="author"><span itemprop="name">%1$s</span></a></div>', | |
| 154 | + esc_html(get_the_author_meta('display_name', get_the_author_meta('ID'))), | |
| 155 | + esc_url(get_author_posts_url(get_the_author_meta('ID'))) | |
| 156 | + | |
| 157 | + ); | |
| 158 | + } | |
| 159 | + | |
| 160 | + /* Get the post date */ | |
| 161 | + if (isset($attributes['displayPostDate']) && $attributes['displayPostDate']) { | |
| 162 | + $blockspare_content_markup .= sprintf( | |
| 163 | + '<time " datetime="%1$s" class="blockspare-block-post-grid-date" itemprop="datePublished">%2$s</time>', | |
| 164 | + esc_attr(get_the_date('c', $post_id)), | |
| 165 | + esc_html(get_the_date('', $post_id)) | |
| 166 | + | |
| 167 | + ); | |
| 168 | + } | |
| 169 | + | |
| 170 | + /* Close the byline content */ | |
| 171 | + $blockspare_content_markup .= sprintf( | |
| 172 | + '</div>' | |
| 173 | + ); | |
| 174 | + } | |
| 175 | + | |
| 176 | + /* Close the header content */ | |
| 177 | + $blockspare_content_markup .= sprintf( | |
| 178 | + '</header>' | |
| 179 | + ); | |
| 180 | + | |
| 181 | + /* Wrap the excerpt content */ | |
| 182 | + $blockspare_content_markup .= sprintf( | |
| 183 | + '<div class="blockspare-block-post-grid-excerpt">' | |
| 184 | + | |
| 185 | + | |
| 186 | + ); | |
| 187 | + | |
| 188 | + /* Get the excerpt */ | |
| 189 | + | |
| 190 | + global $post; | |
| 191 | + | |
| 192 | + | |
| 193 | + $excerpt = blockspare_post_excerpt($post_id, $post); | |
| 194 | + | |
| 195 | + if (!$excerpt) { | |
| 196 | + $excerpt = null; | |
| 197 | + } | |
| 198 | + | |
| 199 | + if (!empty($excerpt)) { | |
| 200 | + $excerpt = explode(' ', $excerpt); | |
| 201 | + $trim_to_length = (int)$attributes['excerptLength'] ? (int)$attributes['excerptLength'] : 55; | |
| 202 | + if (count($excerpt) > $trim_to_length) { | |
| 203 | + $excerpt = implode(' ', array_slice($excerpt, 0, $trim_to_length)) . '...'; | |
| 204 | + } else { | |
| 205 | + $excerpt = implode(' ', $excerpt); | |
| 206 | + } | |
| 207 | + | |
| 208 | + } | |
| 209 | + | |
| 210 | + | |
| 211 | + if ((isset($attributes['displayPostExcerpt']) && $excerpt != null) || isset($attributes['displayPostLink'])) { | |
| 212 | + | |
| 213 | + | |
| 214 | + /* Wrap the excerpt content */ | |
| 215 | + $blockspare_content_markup .= sprintf( | |
| 216 | + '<div class="blockspare-block-post-grid-excerpt">' | |
| 217 | + | |
| 218 | + | |
| 219 | + ); | |
| 220 | + | |
| 221 | + if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) { | |
| 222 | + $blockspare_content_markup .= sprintf( | |
| 223 | + '<div class="blockspare-block-post-grid-excerpt-content">%s</div>', | |
| 224 | + wp_kses_post($excerpt) | |
| 225 | + ); | |
| 226 | + } | |
| 227 | + | |
| 228 | + /* Get the read more link */ | |
| 229 | + if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) { | |
| 230 | + $blockspare_content_markup .= sprintf( | |
| 231 | + '<p><a class="blockspare-block-post-grid-more-link blockspare-text-link" href="%1$s" rel="bookmark">%2$s <span class="screen-reader-text">%3$s</span></a></p>', | |
| 232 | + esc_url(get_permalink($post_id)), | |
| 233 | + esc_html($attributes['readMoreText']), | |
| 234 | + esc_html($title) | |
| 235 | + ); | |
| 236 | + } | |
| 237 | + | |
| 238 | + /* Close the excerpt content */ | |
| 239 | + $blockspare_content_markup .= sprintf( | |
| 240 | + '</div>' | |
| 241 | + ); | |
| 242 | + | |
| 243 | + } | |
| 244 | + | |
| 245 | + /* Close the text content */ | |
| 246 | + $blockspare_content_markup .= sprintf( | |
| 247 | + '</div>' | |
| 248 | + ); | |
| 172 | 249 | |
| 250 | + $blockspare_content_markup .= sprintf( | |
| 251 | + '</div>' | |
| 252 | + ); | |
| 253 | + | |
| 254 | + /* Close the post */ | |
| 255 | + $blockspare_content_markup .= "</div>\n"; | |
| 173 | 256 | } |
| 174 | - } | |
| 175 | - | |
| 176 | - if(!function_exists('blockspare_get_comment_count')){ | |
| 177 | - function blockspare_get_comment_count($object, $field_name, $reques){ | |
| 178 | - | |
| 179 | - return get_comments_number( $object['id'] ); | |
| 180 | - | |
| 257 | + | |
| 258 | + /* Restore original post data */ | |
| 259 | + wp_reset_postdata(); | |
| 260 | + | |
| 261 | + /* Build the block classes */ | |
| 262 | + $class = "wp-block-blockspare-block-blockspare-latest-posts blockspare-post-wrap align{$attributes['align']}"; | |
| 263 | + | |
| 264 | + if (isset($attributes['className'])) { | |
| 265 | + $class .= ' ' . $attributes['className']; | |
| 181 | 266 | } |
| 182 | - } | |
| 183 | - | |
| 184 | - if (!function_exists('blockspare_featured_image_urls')) { | |
| 185 | - /** | |
| 186 | - * Get the different featured image sizes that the blog will use. | |
| 187 | - * Used in the custom REST API endpoint. | |
| 188 | - * | |
| 189 | - * @since 1.7 | |
| 190 | - */ | |
| 191 | - function blockspare_featured_image_urls($object, $field_name, $request) | |
| 192 | - { | |
| 193 | - return blockspare_featured_image_urls_from_url(!empty($object['featured_media']) ? $object['featured_media'] : ''); | |
| 267 | + $col_class = ''; | |
| 268 | + | |
| 269 | + if ($attributes['design'] === 'blockspare-is-grid') { | |
| 270 | + $col_class = 'column-' . $attributes['columns']; | |
| 194 | 271 | } |
| 195 | - } | |
| 196 | - | |
| 197 | - | |
| 198 | - if (!function_exists('blockspare_featured_image_urls_from_url')) { | |
| 199 | - /** | |
| 200 | - * Get the different featured image sizes that the blog will use. | |
| 201 | - * | |
| 202 | - * @since 2.0 | |
| 203 | - */ | |
| 204 | - function blockspare_featured_image_urls_from_url($attachment_id) | |
| 205 | - { | |
| 206 | - | |
| 207 | - $image = wp_get_attachment_image_src($attachment_id, 'full', false); | |
| 208 | - $sizes = get_intermediate_image_sizes(); | |
| 209 | - | |
| 210 | - $imageSizes = array( | |
| 211 | - 'full' => is_array($image) ? $image : '', | |
| 212 | - ); | |
| 213 | - | |
| 214 | - foreach ($sizes as $size) { | |
| 215 | - $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : ''; | |
| 272 | + | |
| 273 | + | |
| 274 | + $list_layout_class = $attributes['design']; | |
| 275 | + | |
| 276 | + /* Layout orientation class */ | |
| 277 | + $grid_class = $blockuniqueclass .' blockspare-latest-post-wrap blockspare-is-list ' . $list_layout_class; | |
| 278 | + | |
| 279 | + | |
| 280 | + /* Post grid section title */ | |
| 281 | + if (isset($attributes['displaySectionTitle']) && $attributes['displaySectionTitle'] && !empty($attributes['sectionTitle'])) { | |
| 282 | + if (isset($attributes['sectionTitleTag'])) { | |
| 283 | + $section_title_tag = $attributes['sectionTitleTag']; | |
| 284 | + } else { | |
| 285 | + $section_title_tag = 'h4'; | |
| 216 | 286 | } |
| 217 | - | |
| 218 | - return $imageSizes; | |
| 287 | + | |
| 288 | + $section_title = '<' . esc_attr($section_title_tag) . '>' . esc_html($attributes['sectionTitle']) . '</' . esc_attr($section_title_tag) . '>'; | |
| 289 | + } else { | |
| 290 | + $section_title = null; | |
| 219 | 291 | } |
| 220 | - } | |
| 221 | - if(!function_exists('blockspare_post_style_css')){ | |
| 222 | - function blockspare_post_style_css($blockuniqueclass ,$attributes){ | |
| 292 | + | |
| 293 | + /* Post grid section tag */ | |
| 294 | + if (isset($attributes['sectionTag'])) { | |
| 295 | + $section_tag = $attributes['sectionTag']; | |
| 296 | + } else { | |
| 297 | + $section_tag = 'section'; | |
| 298 | + } | |
| 299 | + | |
| 300 | + /* Output the post markup */ | |
| 301 | + $block_content = sprintf( | |
| 302 | + '<%1$s class="%2$s"><div class="%4$s">%5$s</div></%1$s>', | |
| 303 | + $section_tag, | |
| 304 | + esc_attr($class), | |
| 305 | + $section_title, | |
| 306 | + esc_attr($grid_class), | |
| 307 | + $blockspare_content_markup | |
| 308 | + | |
| 309 | + | |
| 310 | + ); | |
| 223 | 311 | |
| 224 | - | |
| 225 | - $block_content =''; | |
| 226 | 312 | $block_content .= '<style type="text/css">'; |
| 227 | - $block_content .= ' .' . $blockuniqueclass . '.blockspare-posts-block-latest-post-wrap{ | |
| 228 | - margin-top:' . $attributes['marginTop'] . 'px; | |
| 229 | - margin-bottom:' . $attributes['marginBottom'] . 'px; | |
| 230 | - }'; | |
| 313 | + $block_content .= ' .' . $blockuniqueclass . '.blockspare-latest-post-wrap{ | |
| 314 | + margin-top:' . $attributes['marginTop'] . 'px; | |
| 315 | + margin-bottom:' . $attributes['marginBottom'] . 'px; | |
| 316 | + }'; | |
| 231 | 317 | |
| 232 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-content{ | |
| 233 | - padding-top:' . $attributes['contentPaddingTop'] . 'px; | |
| 234 | - padding-right:' . $attributes['contentPaddingRight'] . 'px; | |
| 235 | - padding-bottom:' . $attributes['contentPaddingBottom'] . 'px; | |
| 236 | - padding-left:' . $attributes['contentPaddingLeft'] . 'px; | |
| 237 | - }'; | |
| 318 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-category{ | |
| 319 | + color:' . $attributes['linkColor'] . '; | |
| 320 | + }'; | |
| 238 | 321 | |
| 239 | - | |
| 240 | - if ($attributes['categoryLayoutOption'] == 'solid') { | |
| 241 | - | |
| 242 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-category a{ | |
| 243 | - color:' . $attributes['categoryTextColor'] . "!important" . '; | |
| 244 | - background-color:' . $attributes['categoryBackgroundColor'] . "!important" . '; | |
| 245 | - border-radius:' . $attributes['categoryBorderRadius'] . "px" . '; | |
| 246 | - }'; | |
| 322 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-author a span{ | |
| 323 | + color:' . $attributes['linkColor'] . '; | |
| 324 | + }'; | |
| 247 | 325 | |
| 248 | - | |
| 249 | - } else if ($attributes['categoryLayoutOption'] == 'border') { | |
| 326 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-text-link a span{ | |
| 327 | + color:' . $attributes['linkColor'] . '; | |
| 328 | + }'; | |
| 250 | 329 | |
| 251 | - | |
| 252 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-category a{ | |
| 253 | - color:' . $attributes['categoryTextColor'] . "!important" . '; | |
| 254 | - background-color:' . "transparent" . '; | |
| 255 | - border:' . "1px solid" . $attributes['categoryBorderColor'] . '; | |
| 256 | - border-radius:' . $attributes['categoryBorderRadius'] . "px" . '; | |
| 257 | - border-width:' . $attributes['categoryBorderWidth'] . "px" . '; | |
| 258 | - }'; | |
| 259 | - | |
| 260 | - | |
| 261 | - | |
| 262 | - } else { | |
| 263 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-category a{ | |
| 264 | - color:' . $attributes['categoryTextColor'] . "!important" . '; | |
| 265 | - }'; | |
| 266 | - } | |
| 330 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a span{ | |
| 331 | + color: ' . $attributes['postTitleColor'] . '; | |
| 332 | + font-size: ' . $attributes['postTitleFontSize'] . $attributes['titleFontSizeType'] . '; | |
| 333 | + font-family: ' . $attributes['titleFontFamily'] . '; | |
| 334 | + font-weight: ' . $attributes['titleFontWeight'] . '; | |
| 335 | + }'; | |
| 267 | 336 | |
| 337 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-date{ | |
| 338 | + color:' . $attributes['generalColor'] . '; | |
| 339 | + }'; | |
| 268 | 340 | |
| 269 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-category{ | |
| 270 | - margin-top:' . $attributes['categoryMarginTop'] . 'px' . '; | |
| 271 | - margin-bottom:' . $attributes['categoryMarginBottom'] . 'px' . '; | |
| 272 | - }'; | |
| 341 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-excerpt{ | |
| 342 | + color:' . $attributes['generalColor'] . '; | |
| 343 | + }'; | |
| 273 | 344 | |
| 274 | 345 | |
| 275 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-byline{ | |
| 276 | - | |
| 277 | - margin-top:' . $attributes['metaMarginTop'] . 'px' . '; | |
| 278 | - margin-bottom:' . $attributes['metaMarginBottom'] . 'px' . '; | |
| 279 | - }'; | |
| 280 | 346 | |
| 281 | 347 | |
| 282 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-more-link{ | |
| 283 | - margin-top:' . $attributes['moreLinkMarginTop'] . 'px' . '; | |
| 284 | - margin-bottom:' . $attributes['moreLinkMarginBottom'] . 'px' . '; | |
| 285 | - }'; | |
| 286 | - | |
| 287 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title a span{ | |
| 288 | - color: ' . $attributes['postTitleColor'] . '; | |
| 289 | - | |
| 290 | - }'; | |
| 348 | + if ($attributes['design'] != 'blockspare-list-layout-4' && $attributes['design'] != 'blockspare-list-layout-5' && $attributes['design'] != 'blockspare-list-layout-6') { | |
| 349 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{ | |
| 350 | + border: 1px solid ' . $attributes['borderColor'] . '; | |
| 351 | + background-color:' . $attributes['backGroundColor'] . '; | |
| 352 | + border-radius:' . $attributes['borderRadius'] . '; | |
| 353 | + }'; | |
| 291 | 354 | |
| 292 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-author a span{ | |
| 293 | - color:' . $attributes['linkColor'] . '; | |
| 294 | - }'; | |
| 295 | - | |
| 296 | - | |
| 297 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-more-link span{ | |
| 298 | - color:' . $attributes['linkColor'] . '; | |
| 299 | - }'; | |
| 300 | - | |
| 301 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-date{ | |
| 302 | - color:' . $attributes['generalColor'] . '; | |
| 303 | - }'; | |
| 304 | - | |
| 305 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-excerpt-content,'.' .' . $blockuniqueclass . ' .comment_count{ | |
| 306 | - color:' . $attributes['generalColor'] . '; | |
| 307 | - | |
| 308 | - }'; | |
| 309 | - | |
| 355 | + } | |
| 310 | 356 | |
| 311 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title{ | |
| 312 | - margin-top:' . $attributes['titleMarginTop'] . 'px' . '; | |
| 313 | - margin-bottom:' . $attributes['titleMarginBottom'] . 'px' . '; | |
| 357 | + if ($attributes['enableBoxShadow']) { | |
| 358 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{ | |
| 359 | + box-shadow = ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . '; | |
| 314 | 360 | }'; |
| 361 | + } | |
| 315 | 362 | |
| 316 | 363 | |
| 317 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-excerpt-content{ | |
| 318 | - margin-top:' . $attributes['exceprtMarginTop'] . 'px' . '; | |
| 319 | - margin-bottom:' . $attributes['exceprtMarginBottom'] . 'px' . '; | |
| 320 | - }'; | |
| 321 | - | |
| 322 | - $layoutstyle = explode('-',$attributes['design']); | |
| 323 | - if($layoutstyle[5] >3){ | |
| 324 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-single .blockspare-posts-block-post-img img{ | |
| 325 | - border-radius:' . $attributes['borderRadius'] . 'px'.'; | |
| 326 | - | |
| 327 | - }'; | |
| 328 | - if($attributes['enableBoxShadow']){ | |
| 329 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-single .blockspare-posts-block-post-img img{ | |
| 330 | - box-shadow: ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . '; | |
| 331 | - }'; | |
| 332 | - } | |
| 333 | - }else{ | |
| 334 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-single{ | |
| 335 | - border-radius:' . $attributes['borderRadius'] . 'px'.'; | |
| 336 | - background-color:'.$attributes['backGroundColor'].' | |
| 337 | - }'; | |
| 364 | + $block_content .= '@media (max-width: 1025px) { '; | |
| 365 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{ | |
| 366 | + font-size: ' . $attributes['titleFontSizeTablet'] . $attributes['titleFontSizeType'] . '; | |
| 367 | + }'; | |
| 368 | + $block_content .= '}'; | |
| 338 | 369 | |
| 339 | - if($attributes['enableBoxShadow']){ | |
| 340 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-single{ | |
| 341 | - box-shadow: ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . '; | |
| 342 | - }'; | |
| 343 | - } | |
| 344 | - } | |
| 345 | - | |
| 346 | - | |
| 347 | - //Title Hover | |
| 348 | 370 | |
| 349 | - if($attributes['titleOnHover']=='lpc-title-hover') { | |
| 350 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-content .blockspare-posts-block-title-link:hover span{ | |
| 351 | - color: ' . $attributes['titleOnHoverColor'] . '; | |
| 352 | - | |
| 353 | - }'; | |
| 354 | - } | |
| 355 | - | |
| 356 | - if($attributes['titleOnHover']=='lpc-title-border') { | |
| 357 | - $block_content .= ' .' . $blockuniqueclass . ' .lpc-title-border .blockspare-posts-block-post-grid-title .blockspare-posts-block-title-link span:hover{ | |
| 358 | - box-shadow: inset 0 -2px 0 0 ' . $attributes['titleOnHoverColor'] . '; | |
| 359 | - | |
| 360 | - }'; | |
| 361 | - } | |
| 371 | + $block_content .= '@media (max-width: 768px) { '; | |
| 372 | + $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{ | |
| 373 | + font-size: ' . $attributes['titleFontSizeMobile'] . $attributes['titleFontSizeType'] . '; | |
| 374 | + }'; | |
| 375 | + $block_content .= '}'; | |
| 362 | 376 | |
| 363 | - | |
| 364 | - | |
| 365 | - //Font Settings | |
| 366 | 377 | |
| 367 | - | |
| 368 | - | |
| 369 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title a span{ | |
| 370 | - | |
| 371 | - font-size: ' . $attributes['postTitleFontSize'] . $attributes['titleFontSizeType'] . '; | |
| 372 | - | |
| 373 | - '.bscheckFontfamily($attributes['titleFontFamily']).'; | |
| 374 | - font-weight: ' . $attributes['titleFontWeight'] . '; | |
| 375 | - }'; | |
| 376 | - | |
| 377 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-content .blockspare-posts-block-post-grid-excerpt .blockspare-posts-block-post-grid-excerpt-content { | |
| 378 | - font-size:' . $attributes['descriptionFontSize'] . $attributes['descriptionFontSizeType'] . '; | |
| 379 | - | |
| 380 | - '.bscheckFontfamily($attributes['descriptionFontFamily']).'; | |
| 381 | - font-weight: ' . $attributes['descriptionFontWeight'] . '; | |
| 382 | - }'; | |
| 383 | - | |
| 384 | - $block_content .= '@media (max-width: 1025px) { '; | |
| 385 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title a span{ | |
| 386 | - font-size: ' . $attributes['titleFontSizeTablet'] . $attributes['titleFontSizeType'] . '; | |
| 387 | - }'; | |
| 388 | - $block_content .= ' .' . $blockuniqueclass .' .blockspare-posts-block-post-content .blockspare-posts-block-post-grid-excerpt .blockspare-posts-block-post-grid-excerpt-content { | |
| 389 | - font-size:' . $attributes['descriptionFontSizeTablet'].$attributes['descriptionFontSizeType'].' | |
| 390 | - }'; | |
| 391 | - $block_content .= '}'; | |
| 392 | - | |
| 393 | - | |
| 394 | - $block_content .= '@media (max-width: 768px) { '; | |
| 395 | - $block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title a span{ | |
| 396 | - font-size: ' . $attributes['titleFontSizeMobile'] . $attributes['titleFontSizeType'] . '; | |
| 397 | - }'; | |
| 398 | - $block_content .= ' .' . $blockuniqueclass .' .blockspare-posts-block-post-content .blockspare-posts-block-post-grid-excerpt .blockspare-posts-block-post-grid-excerpt-content { | |
| 399 | - font-size:' . $attributes['descriptionFontSizeMobile'].$attributes['descriptionFontSizeType'].' | |
| 400 | - }'; | |
| 401 | - $block_content .= '}'; | |
| 402 | - | |
| 403 | - | |
| 404 | 378 | $block_content .= '</style>'; |
| 405 | 379 | return $block_content; |
| 406 | 380 | } |
| 407 | 381 | } |
| 382 | + | |
| 383 | +/** | |
| 384 | + * Registers the post grid block on server | |
| 385 | + */ | |
| 386 | +function blockspare_blocks_register_block_core_latest_posts_list() | |
| 387 | +{ | |
| 388 | + | |
| 389 | + /* Check if the register function exists */ | |
| 390 | + if (!function_exists('register_block_type')) { | |
| 391 | + return; | |
| 392 | + } | |
| 393 | + | |
| 394 | + ob_start(); | |
| 395 | + include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/latest-posts-list/block.json'; | |
| 396 | + | |
| 397 | + $metadata = json_decode(ob_get_clean(), true); | |
| 398 | + | |
| 399 | + /* Block attributes */ | |
| 400 | + register_block_type( | |
| 401 | + 'blockspare/blockspare-latest-posts-list', | |
| 402 | + array( | |
| 403 | + 'attributes' => $metadata['attributes'], | |
| 404 | + 'render_callback' => 'blockspare_blocks_render_block_core_latest_posts_list', | |
| 405 | + ) | |
| 406 | + ); | |
| 407 | +} | |
| 408 | + | |
| 409 | +add_action('init', 'blockspare_blocks_register_block_core_latest_posts_list'); | |
| 410 | + | |
| 411 | + | |
| 412 | +/** | |
| 413 | + * Create API fields for additional info | |
| 414 | + */ | |
| 415 | +function blockspare_blocks_post_register_rest_fields() | |
| 416 | +{ | |
| 417 | + | |
| 418 | + register_rest_field('post', 'featured_image_urls', | |
| 419 | + array( | |
| 420 | + 'get_callback' => 'blockspare_featured_image_urls', | |
| 421 | + 'update_callback' => null, | |
| 422 | + 'schema' => array( | |
| 423 | + 'description' => __('Different sized featured images', 'blockspare'), | |
| 424 | + 'type' => 'array', | |
| 425 | + ), | |
| 426 | + ) | |
| 427 | + ); | |
| 428 | + | |
| 429 | + // Excer | |
| 430 | + | |
| 431 | + /* Add author info */ | |
| 432 | + register_rest_field( | |
| 433 | + 'post', | |
| 434 | + 'author_info', | |
| 435 | + array( | |
| 436 | + 'get_callback' => 'blockspare_blocks_get_author_infos', | |
| 437 | + 'update_callback' => null, | |
| 438 | + 'schema' => null, | |
| 439 | + ) | |
| 440 | + ); | |
| 441 | + | |
| 442 | + /* Add author info */ | |
| 443 | + register_rest_field( | |
| 444 | + 'post', | |
| 445 | + 'category_info', | |
| 446 | + array( | |
| 447 | + 'get_callback' => 'blockspare_blocks_get_category_infos', | |
| 448 | + 'update_callback' => null, | |
| 449 | + 'schema' => null, | |
| 450 | + ) | |
| 451 | + ); | |
| 452 | + | |
| 453 | + /* Add author info */ | |
| 454 | + register_rest_field( | |
| 455 | + 'post', | |
| 456 | + 'tag_info', | |
| 457 | + array( | |
| 458 | + 'get_callback' => 'blockspare_blocks_get_tag_infos', | |
| 459 | + 'update_callback' => null, | |
| 460 | + 'schema' => null, | |
| 461 | + ) | |
| 462 | + ); | |
| 463 | +} | |
| 464 | + | |
| 465 | +add_action('rest_api_init', 'blockspare_blocks_post_register_rest_fields'); | |
| 466 | + | |
| 467 | + | |
| 468 | +/** | |
| 469 | + * Get author info for the rest field | |
| 470 | + * | |
| 471 | + * @param String $object The object type. | |
| 472 | + * @param String $field_name Name of the field to retrieve. | |
| 473 | + * @param String $request The current request object. | |
| 474 | + */ | |
| 475 | +function blockspare_blocks_get_author_infos($object, $field_name, $request) | |
| 476 | +{ | |
| 477 | + /* Get the author name */ | |
| 478 | + $author_data['display_name'] = get_the_author_meta('display_name', $object['author']); | |
| 479 | + | |
| 480 | + /* Get the author link */ | |
| 481 | + $author_data['author_link'] = get_author_posts_url($object['author']); | |
| 482 | + | |
| 483 | + /* Return the author data */ | |
| 484 | + return $author_data; | |
| 485 | +} | |
| 486 | + | |
| 487 | +function blockspare_blocks_get_category_infos($object, $field_name, $request) | |
| 488 | +{ | |
| 489 | + | |
| 490 | + | |
| 491 | + return get_the_category_list(esc_html__(' ', 'blockspare'), '', $object['id']); | |
| 492 | + | |
| 493 | +} | |
| 494 | + | |
| 495 | +function blockspare_blocks_get_tag_infos($object, $field_name, $request) | |
| 496 | +{ | |
| 497 | + | |
| 498 | + ob_start(); | |
| 499 | + $cate_name = ''; | |
| 500 | + if (!empty($object)) { | |
| 501 | + foreach ($object['categories'] as $cat_id) { | |
| 502 | + $cate_name = get_cat_name($cat_id); | |
| 503 | + } | |
| 504 | + } | |
| 505 | + ob_clean(); | |
| 506 | + return $cate_name; | |
| 507 | + | |
| 508 | +} | |
| 509 | + | |
| 510 | +if (!function_exists('blockspare_featured_image_urls')) { | |
| 511 | + /** | |
| 512 | + * Get the different featured image sizes that the blog will use. | |
| 513 | + * Used in the custom REST API endpoint. | |
| 514 | + * | |
| 515 | + * @since 1.7 | |
| 516 | + */ | |
| 517 | + function blockspare_featured_image_urls($object, $field_name, $request) | |
| 518 | + { | |
| 519 | + return blockspare_featured_image_urls_from_url(!empty($object['featured_media']) ? $object['featured_media'] : ''); | |
| 520 | + } | |
| 521 | +} | |
| 522 | + | |
| 523 | + | |
| 524 | +if (!function_exists('blockspare_featured_image_urls_from_url')) { | |
| 525 | + /** | |
| 526 | + * Get the different featured image sizes that the blog will use. | |
| 527 | + * | |
| 528 | + * @since 2.0 | |
| 529 | + */ | |
| 530 | + function blockspare_featured_image_urls_from_url($attachment_id) | |
| 531 | + { | |
| 532 | + | |
| 533 | + $image = wp_get_attachment_image_src($attachment_id, 'full', false); | |
| 534 | + $sizes = get_intermediate_image_sizes(); | |
| 535 | + | |
| 536 | + $imageSizes = array( | |
| 537 | + 'full' => is_array($image) ? $image : '', | |
| 538 | + ); | |
| 539 | + | |
| 540 | + foreach ($sizes as $size) { | |
| 541 | + $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : ''; | |
| 542 | + } | |
| 543 | + | |
| 544 | + return $imageSizes; | |
| 545 | + } | |
| 546 | +} | |
| 547 | + | |
| 548 | + | |
| 549 | +if (!function_exists('blockspare_post_excerpt')) { | |
| 550 | + /** | |
| 551 | + * Get the post excerpt. | |
| 552 | + * | |
| 553 | + * @since 1.7 | |
| 554 | + */ | |
| 555 | + function blockspare_post_excerpt($object) | |
| 556 | + { | |
| 557 | + return blockspare_get_excerpt($object['id']); | |
| 558 | + } | |
| 559 | +} | |
| 560 | + | |
| 561 | +if (!function_exists('blockspare_get_excerpt')) { | |
| 562 | + /** | |
| 563 | + * Get the excerpt. | |
| 564 | + * | |
| 565 | + * @since 1.7 | |
| 566 | + */ | |
| 567 | + function blockspare_get_excerpt($post_id, $post = null) | |
| 568 | + { | |
| 569 | + $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id, 'display')); | |
| 570 | + if (!empty($excerpt)) { | |
| 571 | + return $excerpt; | |
| 572 | + } | |
| 573 | + | |
| 574 | + $max_excerpt = 100; // WP default is 55. | |
| 575 | + | |
| 576 | + if (!empty($post['post_content'])) { | |
| 577 | + return apply_filters('the_excerpt', wp_trim_words($post['post_content'], $max_excerpt)); | |
| 578 | + } | |
| 579 | + $post_content = apply_filters('the_content', get_post_field('post_content', $post_id)); | |
| 580 | + return apply_filters('the_excerpt', wp_trim_words($post_content, $max_excerpt)); | |
| 581 | + } | |
| 582 | +} | |
| 583 | + | |
| 408 | 584 | |
| 409 | - | |