Admin
2 weeks ago
Builder
2 weeks ago
Helpers
2 weeks ago
Integrations
2 weeks ago
CFF_Autolink.php
2 weeks ago
CFF_Blocks.php
2 weeks ago
CFF_Cache.php
2 weeks ago
CFF_Education.php
2 weeks ago
CFF_Elementor_Base.php
2 weeks ago
CFF_Elementor_Widget.php
2 weeks ago
CFF_Error_Reporter.php
2 weeks ago
CFF_FB_Settings.php
2 weeks ago
CFF_Feed_Elementor_Control.php
2 weeks ago
CFF_Feed_Locator.php
2 weeks ago
CFF_Feed_Pro.php
2 weeks ago
CFF_GDPR_Integrations.php
2 weeks ago
CFF_Group_Posts.php
2 weeks ago
CFF_HTTP_Request.php
2 weeks ago
CFF_Oembed.php
2 weeks ago
CFF_Parse.php
2 weeks ago
CFF_Resizer.php
2 weeks ago
CFF_Response.php
2 weeks ago
CFF_Shortcode.php
2 weeks ago
CFF_Shortcode_Display.php
2 weeks ago
CFF_SiteHealth.php
2 weeks ago
CFF_Utils.php
2 weeks ago
CFF_View.php
2 weeks ago
Custom_Facebook_Feed.php
2 weeks ago
Email_Notification.php
2 weeks ago
Platform_Data.php
2 weeks ago
SB_Facebook_Data_Encryption.php
2 weeks ago
SB_Facebook_Data_Manager.php
2 weeks ago
index.php
2 weeks ago
CFF_Blocks.php
390 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Custom Facebook Feed block with live preview. |
| 5 | * |
| 6 | * @since 2.3 |
| 7 | */ |
| 8 | |
| 9 | namespace CustomFacebookFeed; |
| 10 | |
| 11 | use CustomFacebookFeed\Helpers\Util; |
| 12 | use CustomFacebookFeed\Builder\CFF_Db; |
| 13 | use CustomFacebookFeed\CFF_Utils; |
| 14 | |
| 15 | if (!defined('ABSPATH')) { |
| 16 | exit; // Exit if accessed directly |
| 17 | } |
| 18 | |
| 19 | class CFF_Blocks |
| 20 | { |
| 21 | /** |
| 22 | * Indicates if current integration is allowed to load. |
| 23 | * |
| 24 | * @since 1.8 |
| 25 | * |
| 26 | * @return bool |
| 27 | */ |
| 28 | public function allow_load() |
| 29 | { |
| 30 | return function_exists('register_block_type'); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Loads an integration. |
| 35 | * |
| 36 | * @since 2.3 |
| 37 | */ |
| 38 | public function load() |
| 39 | { |
| 40 | $this->hooks(); |
| 41 | |
| 42 | require_once trailingslashit( CFF_PLUGIN_DIR ) . 'inc/Admin/Blocks/CFF_Modern_Feed_Block.php'; |
| 43 | $modern_block = new \CustomFacebookFeed\Admin\Blocks\CFF_Modern_Feed_Block(); |
| 44 | $modern_block->register_hooks(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Integration hooks. |
| 49 | * |
| 50 | * @since 2.3 |
| 51 | */ |
| 52 | protected function hooks() |
| 53 | { |
| 54 | add_action('init', array( $this, 'register_block' )); |
| 55 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 25 ); |
| 56 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_content_assets' ) ); |
| 57 | add_filter( 'block_editor_settings_all', array( $this, 'inject_iframe_styles' ) ); |
| 58 | |
| 59 | /* |
| 60 | * Add smashballoon category and Facebook Feed Block |
| 61 | * @since 4.1.9 |
| 62 | */ |
| 63 | add_filter('block_categories_all', array( $this, 'register_block_category' ), 10, 2); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Inject block UI and feed CSS into the WP 7.0+ iframed editor canvas. |
| 68 | * |
| 69 | * `block_editor_settings_all` exposes a `styles` array that WordPress renders |
| 70 | * inline inside the iframe `<head>`. wp_enqueue_style on the outer admin page |
| 71 | * does not propagate to the iframe for api_version 3 blocks, so we have to |
| 72 | * push the CSS contents through this filter for it to be visible inside the |
| 73 | * iframe (e.g. the license-expired notice rendered by get_feed_html()). |
| 74 | * |
| 75 | * @param array $settings Block editor settings. |
| 76 | * @return array |
| 77 | */ |
| 78 | public function inject_iframe_styles( $settings ) { |
| 79 | // Cache the CSS payload across the request lifecycle. block_editor_settings_all |
| 80 | // fires on every block-editor request (post editor, site editor, widget editor) |
| 81 | // and the CSS bytes on disk don't change between calls, so re-reading them is |
| 82 | // wasteful disk I/O on the hottest path of the editor. |
| 83 | // TODO: also scope this by screen so we only inject when the editor could host |
| 84 | // this plugin's blocks. Scoping is intentionally skipped for now because |
| 85 | // block_editor_settings_all fires in REST contexts where get_current_screen() |
| 86 | // is unreliable, and over-scoping would re-break the iframe styling fix. |
| 87 | static $cached = null; |
| 88 | |
| 89 | if ( null === $cached ) { |
| 90 | $files = array( |
| 91 | trailingslashit( CFF_PLUGIN_DIR ) . 'assets/css/cff-style.css', |
| 92 | trailingslashit( CFF_PLUGIN_DIR ) . 'assets/css/cff-blocks.css', |
| 93 | ); |
| 94 | |
| 95 | $cached = array(); |
| 96 | foreach ( $files as $file ) { |
| 97 | if ( ! file_exists( $file ) ) { |
| 98 | continue; |
| 99 | } |
| 100 | $css = file_get_contents( $file ); |
| 101 | if ( false === $css ) { |
| 102 | continue; |
| 103 | } |
| 104 | $cached[] = array( 'css' => $css ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if ( ! isset( $settings['styles'] ) || ! is_array( $settings['styles'] ) ) { |
| 109 | $settings['styles'] = array(); |
| 110 | } |
| 111 | |
| 112 | foreach ( $cached as $entry ) { |
| 113 | $settings['styles'][] = $entry; |
| 114 | } |
| 115 | |
| 116 | return $settings; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Register Custom Facebook Feed Gutenberg block on the backend. |
| 121 | * |
| 122 | * @since 2.3 |
| 123 | */ |
| 124 | public function register_block() |
| 125 | { |
| 126 | |
| 127 | wp_register_style( |
| 128 | 'cff-blocks-styles', |
| 129 | trailingslashit(CFF_PLUGIN_URL) . 'assets/css/cff-blocks.css', |
| 130 | array( 'wp-edit-blocks' ), |
| 131 | CFFVER |
| 132 | ); |
| 133 | |
| 134 | $attributes = array( |
| 135 | 'shortcodeSettings' => array( |
| 136 | 'type' => 'string', |
| 137 | ), |
| 138 | 'noNewChanges' => array( |
| 139 | 'type' => 'boolean', |
| 140 | ), |
| 141 | 'executed' => array( |
| 142 | 'type' => 'boolean', |
| 143 | ) |
| 144 | ); |
| 145 | |
| 146 | register_block_type( |
| 147 | 'cff/cff-feed-block', |
| 148 | array( |
| 149 | 'api_version' => 3, |
| 150 | 'attributes' => $attributes, |
| 151 | 'render_callback' => array( $this, 'get_feed_html' ), |
| 152 | 'supports' => array( 'inserter' => false ), |
| 153 | ) |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Enqueue feed frontend assets so the legacy block preview renders inside |
| 159 | * the WP 6.7+ iframe block editor. Mirrors SB_Feed_Block::enqueue_block_content_assets(). |
| 160 | * |
| 161 | * @since 4.5.0 |
| 162 | */ |
| 163 | public function enqueue_block_content_assets() { |
| 164 | if ( ! is_admin() ) { |
| 165 | return; |
| 166 | } |
| 167 | \cff_main()->enqueue_styles_assets(); |
| 168 | \cff_main()->enqueue_scripts_assets(); |
| 169 | // Force enqueue inside the iframe editor even when the "load assets only |
| 170 | // with shortcode" option is on — the enqueue_*_assets() methods only |
| 171 | // register the handles in that case. |
| 172 | wp_enqueue_style( 'cff' ); |
| 173 | wp_enqueue_script( 'cffscripts' ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Load Custom Facebook Feed Gutenberg block scripts. |
| 178 | * |
| 179 | * @since 2.3 |
| 180 | */ |
| 181 | public function enqueue_block_editor_assets() |
| 182 | { |
| 183 | $access_token = get_option('cff_access_token'); |
| 184 | |
| 185 | wp_enqueue_style('cff-blocks-styles'); |
| 186 | wp_enqueue_script( |
| 187 | 'cff-feed-block', |
| 188 | trailingslashit(CFF_PLUGIN_URL) . 'assets/js/cff-blocks.js', |
| 189 | array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor' ), |
| 190 | CFFVER, |
| 191 | true |
| 192 | ); |
| 193 | |
| 194 | $shortcodeSettings = ''; |
| 195 | |
| 196 | $i18n = array( |
| 197 | 'addSettings' => esc_html__('Add Settings', 'custom-facebook-feed'), |
| 198 | 'shortcodeSettings' => esc_html__('Shortcode Settings', 'custom-facebook-feed'), |
| 199 | 'example' => esc_html__('Example', 'custom-facebook-feed'), |
| 200 | 'preview' => esc_html__('Apply Changes', 'custom-facebook-feed'), |
| 201 | |
| 202 | ); |
| 203 | |
| 204 | if (! empty($_GET['cff_wizard'])) { |
| 205 | $shortcodeSettings = 'feed="' . (int)$_GET['cff_wizard'] . '"'; |
| 206 | } |
| 207 | |
| 208 | // CFF's Helpers\Util does not expose isDebugging()/is_script_debug() helpers |
| 209 | // like Instagram Feed does, so fall back to the SCRIPT_DEBUG constant. |
| 210 | $is_script_debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; |
| 211 | |
| 212 | $cff_js_file = $is_script_debug |
| 213 | ? 'assets/js/cff-scripts.js' |
| 214 | : 'assets/js/cff-scripts.min.js'; |
| 215 | |
| 216 | $jquery_file = 'js/jquery/jquery' . ( $is_script_debug ? '' : '.min' ) . '.js'; |
| 217 | |
| 218 | wp_localize_script( |
| 219 | 'cff-feed-block', |
| 220 | 'cff_block_editor', |
| 221 | array( |
| 222 | 'wpnonce' => wp_create_nonce('facebook-blocks'), |
| 223 | 'canShowFeed' => ! empty($access_token), |
| 224 | 'configureLink' => get_admin_url() . '?page=cff-settings', |
| 225 | 'shortcodeSettings' => $shortcodeSettings, |
| 226 | 'i18n' => $i18n, |
| 227 | 'iframeScriptUrl' => trailingslashit( CFF_PLUGIN_URL ) . $cff_js_file, |
| 228 | 'jqueryUrl' => includes_url( $jquery_file ), |
| 229 | ) |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get form HTML to display in a Custom Facebook Feed Gutenberg block. |
| 235 | * |
| 236 | * @param array $attr Attributes passed by Custom Facebook Feed Gutenberg block. |
| 237 | * |
| 238 | * @since 2.3 |
| 239 | * |
| 240 | * @return string |
| 241 | */ |
| 242 | public function get_feed_html($attr) |
| 243 | { |
| 244 | $cff_statuses = get_option('cff_statuses', array()); |
| 245 | |
| 246 | $return = ''; |
| 247 | |
| 248 | $shortcode_settings = isset($attr['shortcodeSettings']) ? $attr['shortcodeSettings'] : ''; |
| 249 | |
| 250 | if (empty($cff_statuses['support_legacy_shortcode'])) { |
| 251 | if (empty($shortcode_settings) || strpos($shortcode_settings, 'feed=') === false) { |
| 252 | $feeds = \CustomFacebookFeed\Builder\CFF_Feed_Builder::get_feed_list(); |
| 253 | $feed_id = $feeds[0]['id']; |
| 254 | $shortcode_settings .= ' feed="' . (int)$feed_id . '"'; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | $shortcode_settings = str_replace(array( '[custom-facebook-feed', ']' ), '', $shortcode_settings); |
| 259 | |
| 260 | $return .= do_shortcode('[custom-facebook-feed ' . $shortcode_settings . ']'); |
| 261 | |
| 262 | return $return; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Checking if is Gutenberg REST API call. |
| 267 | * |
| 268 | * @since 2.3 |
| 269 | * |
| 270 | * @return bool True if is Gutenberg REST API call. |
| 271 | */ |
| 272 | public static function is_gb_editor() |
| 273 | { |
| 274 | return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Register Block Category |
| 279 | * |
| 280 | * @since 4.1.9 |
| 281 | */ |
| 282 | public function register_block_category($categories, $context) |
| 283 | { |
| 284 | $exists = array_search('smashballoon', array_column($categories, 'slug')); |
| 285 | |
| 286 | if ($exists !== false) { |
| 287 | return $categories; |
| 288 | } |
| 289 | |
| 290 | return array_merge( |
| 291 | $categories, |
| 292 | array( |
| 293 | array( |
| 294 | 'slug' => 'smashballoon', |
| 295 | 'title' => __('Smash Balloon', 'custom-facebook-feed'), |
| 296 | ), |
| 297 | ) |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Register Block |
| 303 | * |
| 304 | * @since 4.1.9 |
| 305 | */ |
| 306 | public function register_facebook_feed_block() |
| 307 | { |
| 308 | register_block_type( |
| 309 | trailingslashit(CFF_PLUGIN_DIR) . 'assets/dist/sbf-feed', |
| 310 | array( |
| 311 | 'render_callback' => array( $this, 'render_facebook_feed_block' ), |
| 312 | ) |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Render Block |
| 318 | * |
| 319 | * @since 4.1.9 |
| 320 | */ |
| 321 | public function render_facebook_feed_block($attributes) |
| 322 | { |
| 323 | $content = ''; |
| 324 | |
| 325 | if (isset($attributes['feedId'])) { |
| 326 | $content = do_shortcode('[custom-facebook-feed feed=' . (int) $attributes['feedId'] . ']'); |
| 327 | } |
| 328 | |
| 329 | return $content; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Enqueue Block Assets |
| 334 | * |
| 335 | * @since 4.1.9 |
| 336 | */ |
| 337 | public function enqueue_facebook_feed_block_editor_assets() |
| 338 | { |
| 339 | $asset_file = include_once trailingslashit(CFF_PLUGIN_DIR) . 'assets/dist/blocks.asset.php'; |
| 340 | |
| 341 | wp_enqueue_script( |
| 342 | 'cff-feed-block-editor', |
| 343 | trailingslashit(CFF_PLUGIN_URL) . 'assets/dist/blocks.js', |
| 344 | $asset_file['dependencies'], |
| 345 | $asset_file['version'], |
| 346 | true |
| 347 | ); |
| 348 | |
| 349 | wp_enqueue_style( |
| 350 | 'cff-feed-block-editor', |
| 351 | trailingslashit(CFF_PLUGIN_URL) . 'assets/dist/blocks.css', |
| 352 | array(), |
| 353 | $asset_file['version'] |
| 354 | ); |
| 355 | |
| 356 | wp_localize_script( |
| 357 | 'cff-feed-block-editor', |
| 358 | 'cff_feed_block_editor', |
| 359 | array( |
| 360 | 'feeds' => CFF_Db::feeds_query(), |
| 361 | 'feed_url' => admin_url('admin.php?page=cff-feed-builder'), |
| 362 | 'plugins_info' => Util::get_smash_plugins_status_info(), |
| 363 | 'has_facebook_feed_block' => $this->has_facebook_feed_block(), |
| 364 | 'is_pro_active' => CFF_Utils::cff_is_pro_version(), |
| 365 | 'nonce' => wp_create_nonce('cff-admin'), |
| 366 | ) |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Set Script Translations |
| 372 | * |
| 373 | * @since 4.1.9 |
| 374 | */ |
| 375 | public function set_script_translations() |
| 376 | { |
| 377 | wp_set_script_translations('cff-feed-block-editor', 'custom-facebook-feed', CFF_PLUGIN_DIR . 'languages'); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Check if the post has a Facebook Feed block |
| 382 | * |
| 383 | * @since 4.1.9 |
| 384 | */ |
| 385 | public function has_facebook_feed_block() |
| 386 | { |
| 387 | return has_block('cff/cff-feed-block'); |
| 388 | } |
| 389 | } |
| 390 |