| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Magazine Blocks |
| 4 |
* Description: Craft your beautifully unique and dynamic Magazine, Newspaper website with various beautiful and advanced posts related blocks like Featured Posts, Banner Posts, Grid Module, Tab Posts, and more. |
| 5 |
* Author: WPBlockArt |
| 6 |
* Author URI: https://wpblockart.com/ |
| 7 |
* Version: 1.8.8 |
| 8 |
* Requires at least: 6.3 |
| 9 |
* Requires PHP: 7.0 |
| 10 |
* Text Domain: magazine-blocks |
| 11 |
* Domain Path: /languages |
| 12 |
* License: GNU General Public License v3.0 |
| 13 |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html |
| 14 |
* WordPress Available: yes |
| 15 |
* Requires License: no |
| 16 |
* |
| 17 |
* @package Magazine Blocks |
| 18 |
*/ |
| 19 |
|
| 20 |
use MagazineBlocks\Blocks; |
| 21 |
use MagazineBlocks\Helpers\PostHelper; |
| 22 |
use MagazineBlocks\MagazineBlocks; |
| 23 |
|
| 24 |
defined( 'ABSPATH' ) || exit; |
| 25 |
|
| 26 |
! defined( 'MAGAZINE_BLOCKS_VERSION' ) && define( 'MAGAZINE_BLOCKS_VERSION', '1.8.8' ); |
| 27 |
! defined( 'MAGAZINE_BLOCKS_PLUGIN_FILE' ) && define( 'MAGAZINE_BLOCKS_PLUGIN_FILE', __FILE__ ); |
| 28 |
! defined( 'MAGAZINE_BLOCKS_PLUGIN_DIR' ) && define( 'MAGAZINE_BLOCKS_PLUGIN_DIR', __DIR__ ); |
| 29 |
! defined( 'MAGAZINE_BLOCKS_PLUGIN_DIR_URL' ) && define( 'MAGAZINE_BLOCKS_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) ); |
| 30 |
! defined( 'MAGAZINE_BLOCKS_ASSETS' ) && define( 'MAGAZINE_BLOCKS_ASSETS', __DIR__ . '/assets' ); |
| 31 |
! defined( 'MAGAZINE_BLOCKS_ASSETS_DIR_URL' ) && define( 'MAGAZINE_BLOCKS_ASSETS_DIR_URL', MAGAZINE_BLOCKS_PLUGIN_DIR_URL . 'assets' ); |
| 32 |
! defined( 'MAGAZINE_BLOCKS_DIST_DIR_URL' ) && define( 'MAGAZINE_BLOCKS_DIST_DIR_URL', MAGAZINE_BLOCKS_PLUGIN_DIR_URL . 'dist' ); |
| 33 |
! defined( 'MAGAZINE_BLOCKS_LANGUAGES' ) && define( 'MAGAZINE_BLOCKS_LANGUAGES', __DIR__ . '/languages' ); |
| 34 |
! defined( 'MAGAZINE_BLOCKS_UPLOAD_DIR' ) && define( 'MAGAZINE_BLOCKS_UPLOAD_DIR', wp_upload_dir()['basedir'] . '/magazine-blocks' ); |
| 35 |
! defined( 'MAGAZINE_BLOCKS_UPLOAD_DIR_URL' ) && define( 'MAGAZINE_BLOCKS_UPLOAD_DIR_URL', wp_upload_dir()['baseurl'] . '/magazine-blocks' ); |
| 36 |
|
| 37 |
// Load the autoloader. |
| 38 |
require_once __DIR__ . '/vendor/autoload.php'; |
| 39 |
|
| 40 |
if ( ! function_exists( 'magazine_blocks' ) ) { |
| 41 |
/** |
| 42 |
* Returns the main instance of Magazine Blocks to prevent the need to use globals. |
| 43 |
* |
| 44 |
* @return MagazineBlocks |
| 45 |
*/ |
| 46 |
function magazine_blocks() { |
| 47 |
return MagazineBlocks::init(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
magazine_blocks(); |
| 52 |
|
| 53 |
/** |
| 54 |
* Create API fields for additional info |
| 55 |
* |
| 56 |
* @since 1.0.9 |
| 57 |
*/ |
| 58 |
function magazine_blocks_register_rest_fields() { |
| 59 |
$post_type = PostHelper::get_post_types(); |
| 60 |
|
| 61 |
foreach ( $post_type as $key => $value ) { |
| 62 |
// Featured image. |
| 63 |
register_rest_field( |
| 64 |
$value['value'], |
| 65 |
'magazine_blocks_featured_image_url', |
| 66 |
array( |
| 67 |
'get_callback' => 'magazine_blocks_get_featured_image_url', |
| 68 |
'update_callback' => null, |
| 69 |
'schema' => null, |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
// Author info. |
| 74 |
register_rest_field( |
| 75 |
$value['value'], |
| 76 |
'magazine_blocks_author', |
| 77 |
array( |
| 78 |
'get_callback' => 'magazine_blocks_get_author_info', |
| 79 |
'update_callback' => null, |
| 80 |
'schema' => null, |
| 81 |
) |
| 82 |
); |
| 83 |
|
| 84 |
// Add comment info. |
| 85 |
register_rest_field( |
| 86 |
$value['value'], |
| 87 |
'magazine_blocks_comment', |
| 88 |
array( |
| 89 |
'get_callback' => 'magazine_blocks_get_comment_info', |
| 90 |
'update_callback' => null, |
| 91 |
'schema' => null, |
| 92 |
) |
| 93 |
); |
| 94 |
|
| 95 |
// Add comment info. |
| 96 |
register_rest_field( |
| 97 |
$value['value'], |
| 98 |
'magazine_blocks_author_image', |
| 99 |
array( |
| 100 |
'get_callback' => 'magazine_blocks_get_author_image', |
| 101 |
'update_callback' => null, |
| 102 |
'schema' => null, |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
// Category links. |
| 107 |
register_rest_field( |
| 108 |
$value['value'], |
| 109 |
'magazine_blocks_category', |
| 110 |
array( |
| 111 |
'get_callback' => 'magazine_blocks_get_category_list', |
| 112 |
'update_callback' => null, |
| 113 |
'schema' => array( |
| 114 |
'description' => esc_html__( 'Category list links', 'magazine-blocks' ), |
| 115 |
'type' => 'string', |
| 116 |
), |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
// Video post format URL. |
| 121 |
register_rest_field( |
| 122 |
$value['value'], |
| 123 |
'videoUrl', |
| 124 |
array( |
| 125 |
'get_callback' => 'magazine_blocks_get_video_url', |
| 126 |
'update_callback' => null, |
| 127 |
'schema' => array( |
| 128 |
'description' => esc_html__( 'Video URL from the video post format meta', 'magazine-blocks' ), |
| 129 |
'type' => 'string', |
| 130 |
), |
| 131 |
) |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// Feature image. |
| 137 |
function magazine_blocks_get_featured_image_url( $object ) { |
| 138 |
|
| 139 |
$featured_images = array(); |
| 140 |
if ( ! isset( $object['featured_media'] ) ) { |
| 141 |
return $featured_images; |
| 142 |
} else { |
| 143 |
|
| 144 |
$full = wp_get_attachment_image_src( $object['featured_media'], 'full', false ); |
| 145 |
$medium = wp_get_attachment_image_src( $object['featured_media'], 'medium', false ); |
| 146 |
$thumbnail = wp_get_attachment_image_src( $object['featured_media'], 'thumbnail', false ); |
| 147 |
$featured_images['full'] = $full; |
| 148 |
$featured_images['medium'] = $medium; |
| 149 |
$featured_images['thumbnail'] = $thumbnail; |
| 150 |
return $featured_images; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Author. |
| 155 |
function magazine_blocks_get_author_info( $object ) { |
| 156 |
$author = ( isset( $object['author'] ) ) ? $object['author'] : ''; |
| 157 |
|
| 158 |
$author_data['display_name'] = get_the_author_meta( 'display_name', $author ); |
| 159 |
$author_data['author_link'] = get_author_posts_url( $author ); |
| 160 |
|
| 161 |
return $author_data; |
| 162 |
} |
| 163 |
|
| 164 |
// Comment. |
| 165 |
function magazine_blocks_get_comment_info( $object ) { |
| 166 |
$comments_count = wp_count_comments( $object['id'] ); |
| 167 |
return $comments_count->total_comments; |
| 168 |
} |
| 169 |
|
| 170 |
// Author Image. |
| 171 |
function magazine_blocks_get_author_image( $object ) { |
| 172 |
$author = ( isset( $object['author'] ) ) ? $object['author'] : ''; |
| 173 |
|
| 174 |
$author_image = get_avatar_url( $author ); |
| 175 |
|
| 176 |
return $author_image; |
| 177 |
} |
| 178 |
|
| 179 |
// Video post format URL. |
| 180 |
function magazine_blocks_get_video_url( $object ) { |
| 181 |
return get_post_meta( $object['id'], 'video_url', true ); |
| 182 |
} |
| 183 |
|
| 184 |
// Category list. |
| 185 |
if ( ! function_exists( 'magazine_blocks_render_category_link' ) ) { |
| 186 |
/** |
| 187 |
* Render a single category/term as a link, matching the frontend's own |
| 188 |
* category markup so the block editor preview doesn't diverge from it: |
| 189 |
* only add the ColorMag override class + inline color when the theme's |
| 190 |
* "Override Category Color" setting is on and a color actually resolves. |
| 191 |
* |
| 192 |
* @param \WP_Term $term Category or term. |
| 193 |
* @param bool $use_override Whether ColorMag's category color override is active. |
| 194 |
* @return string Rendered anchor markup. |
| 195 |
*/ |
| 196 |
function magazine_blocks_render_category_link( $term, $use_override ) { |
| 197 |
$link = get_term_link( $term ); |
| 198 |
$url = is_wp_error( $link ) ? '#' : esc_url( $link ); |
| 199 |
|
| 200 |
if ( $use_override ) { |
| 201 |
$color = colormag_category_color( $term->term_id ); |
| 202 |
if ( $color ) { |
| 203 |
return sprintf( |
| 204 |
'<a href="%s" class="category-link category-link-%d" style="color: %s;">%s</a>', |
| 205 |
$url, |
| 206 |
(int) $term->term_id, |
| 207 |
esc_attr( $color ), |
| 208 |
esc_html( $term->name ) |
| 209 |
); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return sprintf( '<a href="%s">%s</a>', $url, esc_html( $term->name ) ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if ( ! function_exists( 'magazine_blocks_get_category_list' ) ) { |
| 218 |
/** |
| 219 |
* REST field callback returning a post's category/term links as HTML, |
| 220 |
* used by the block editor's live post-listing previews. |
| 221 |
* |
| 222 |
* @param array $object REST response object array. |
| 223 |
* @return string Category links HTML. |
| 224 |
*/ |
| 225 |
function magazine_blocks_get_category_list( $object ) { |
| 226 |
$taxonomies = get_post_taxonomies( $object['id'] ); |
| 227 |
$post_id = $object['id']; |
| 228 |
$output = ''; |
| 229 |
$use_override = get_theme_mod( 'colormag_enable_override_category_color', false ) && function_exists( 'colormag_category_color' ); |
| 230 |
|
| 231 |
if ( 'post' === get_post_type( $post_id ) ) { |
| 232 |
$categories = get_the_category( $post_id ); |
| 233 |
if ( ! empty( $categories ) ) { |
| 234 |
foreach ( $categories as $category ) { |
| 235 |
$output .= magazine_blocks_render_category_link( $category, $use_override ) . ' '; |
| 236 |
} |
| 237 |
} |
| 238 |
} elseif ( ! empty( $taxonomies ) ) { |
| 239 |
$terms = get_the_terms( $post_id, $taxonomies[0] ); |
| 240 |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 241 |
foreach ( $terms as $term ) { |
| 242 |
$output .= magazine_blocks_render_category_link( $term, $use_override ) . ' '; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
return trim( $output ); |
| 247 |
} |
| 248 |
} |
| 249 |
add_action( 'rest_api_init', 'magazine_blocks_register_rest_fields' ); |
| 250 |
|
| 251 |
add_action( |
| 252 |
'enqueue_block_assets', |
| 253 |
function () { |
| 254 |
// Frontend output is handled separately via the wp_head callback below. |
| 255 |
if ( ! is_admin() ) { |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
$categories = get_categories(); |
| 260 |
$enable_override_category_color = get_theme_mod( 'colormag_enable_override_category_color', false ); |
| 261 |
$css = ''; |
| 262 |
|
| 263 |
if ( function_exists( 'colormag_category_color' ) && $enable_override_category_color ) { |
| 264 |
foreach ( $categories as $category ) { |
| 265 |
$color = colormag_category_color( $category->term_id ); |
| 266 |
if ( $color ) { |
| 267 |
$css .= '.mzb-post-meta .mzb-post-categories .category-link-' . esc_attr( $category->term_id ) . '{background-color: ' . esc_attr( $color ) . ';}'; |
| 268 |
} |
| 269 |
} |
| 270 |
} else { |
| 271 |
foreach ( $categories as $category ) { |
| 272 |
$css .= '.mzb-post-meta .mzb-post-categories .category-link-' . esc_attr( $category->term_id ) . '{background-color: var(--mzb-categories-colors-' . esc_attr( $category->term_id ) . ');}'; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if ( $css && wp_style_is( 'magazine-blocks-blocks-editor', 'registered' ) ) { |
| 277 |
wp_add_inline_style( 'magazine-blocks-blocks-editor', $css ); |
| 278 |
} |
| 279 |
} |
| 280 |
); |
| 281 |
|
| 282 |
add_action( |
| 283 |
'wp_head', |
| 284 |
function () { |
| 285 |
$categories = get_categories(); |
| 286 |
$enable_override_category_color = get_theme_mod( 'colormag_enable_override_category_color', false ); |
| 287 |
$css = ''; |
| 288 |
|
| 289 |
if ( function_exists( 'colormag_category_color' ) && $enable_override_category_color ) { |
| 290 |
foreach ( $categories as $category ) { |
| 291 |
$color = colormag_category_color( $category->term_id ); |
| 292 |
if ( $color ) { |
| 293 |
$css .= '.mzb-post-meta .mzb-post-categories .category-link-' . esc_attr( $category->term_id ) . '{background-color: ' . esc_attr( $color ) . ';}'; |
| 294 |
} |
| 295 |
} |
| 296 |
} else { |
| 297 |
foreach ( $categories as $category ) { |
| 298 |
$settings = magazine_blocks_get_setting( 'global-styles' ); |
| 299 |
$settings = json_decode( $settings, true ); |
| 300 |
$color = $settings['categories_color'] ?? array(); |
| 301 |
if ( ! empty( $color ) ) { |
| 302 |
foreach ( $color as $setting => $value ) { |
| 303 |
if ( isset( $value['id'], $value['value'] ) && $value['id'] == $category->term_id ) { |
| 304 |
$css .= '.mzb-post-meta .mzb-post-categories .category-link-' . esc_attr( $category->term_id ) . '{background-color: ' . esc_attr( $value['value'] ) . ';}'; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if ( $css ) { |
| 312 |
echo '<style id="magazine-blocks-category-colors">' . $css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 313 |
} |
| 314 |
} |
| 315 |
); |
| 316 |
|
| 317 |
add_action( 'wp_ajax_magazine_blocks_pagination_load', 'magazine_blocks_pagination_load' ); |
| 318 |
add_action( 'wp_ajax_nopriv_magazine_blocks_pagination_load', 'magazine_blocks_pagination_load' ); |
| 319 |
|
| 320 |
function magazine_blocks_pagination_load() { |
| 321 |
$page = intval( $_GET['page'] ); |
| 322 |
|
| 323 |
$att = $_POST['att']; // Pass the attributes needed for rendering |
| 324 |
|
| 325 |
// Modify the attributes with the new page number |
| 326 |
$att['paged'] = $page; |
| 327 |
|
| 328 |
// Render the posts using your existing function |
| 329 |
$html = Blocks::render_block_magazine_blocks_featured_posts( $att ); |
| 330 |
|
| 331 |
// Return the HTML as the response |
| 332 |
wp_send_json_success( array( 'html' => $html ) ); |
| 333 |
|
| 334 |
wp_die(); |
| 335 |
} |
| 336 |
|