Admin
3 months ago
Builder
3 months ago
Helpers
3 months ago
Integrations
3 months ago
CFF_Autolink.php
3 months ago
CFF_Blocks.php
3 months ago
CFF_Cache.php
3 months ago
CFF_Education.php
3 months ago
CFF_Elementor_Base.php
3 months ago
CFF_Elementor_Widget.php
3 months ago
CFF_Error_Reporter.php
3 months ago
CFF_FB_Settings.php
3 months ago
CFF_Feed_Elementor_Control.php
3 months ago
CFF_Feed_Locator.php
3 months ago
CFF_Feed_Pro.php
3 months ago
CFF_GDPR_Integrations.php
3 months ago
CFF_Group_Posts.php
3 months ago
CFF_HTTP_Request.php
3 months ago
CFF_Oembed.php
3 months ago
CFF_Parse.php
3 months ago
CFF_Resizer.php
3 months ago
CFF_Response.php
3 months ago
CFF_Shortcode.php
3 months ago
CFF_Shortcode_Display.php
3 months ago
CFF_SiteHealth.php
3 months ago
CFF_Utils.php
3 months ago
CFF_View.php
3 months ago
Custom_Facebook_Feed.php
3 months ago
Email_Notification.php
3 months ago
Platform_Data.php
3 months ago
SB_Facebook_Data_Encryption.php
3 months ago
SB_Facebook_Data_Manager.php
3 months ago
index.php
3 months ago
CFF_Blocks.php
307 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 | |
| 43 | /** |
| 44 | * Integration hooks. |
| 45 | * |
| 46 | * @since 2.3 |
| 47 | */ |
| 48 | protected function hooks() |
| 49 | { |
| 50 | add_action('init', array( $this, 'register_block' )); |
| 51 | add_action('enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' )); |
| 52 | |
| 53 | /* |
| 54 | * Add smashballoon category and Facebook Feed Block |
| 55 | * @since 4.1.9 |
| 56 | */ |
| 57 | add_filter('block_categories_all', array( $this, 'register_block_category' ), 10, 2); |
| 58 | add_action('init', array( $this, 'register_facebook_feed_block' )); |
| 59 | add_action('enqueue_block_editor_assets', array( $this, 'enqueue_facebook_feed_block_editor_assets' )); |
| 60 | add_action('enqueue_block_editor_assets', array( $this, 'set_script_translations' )); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Register Custom Facebook Feed Gutenberg block on the backend. |
| 65 | * |
| 66 | * @since 2.3 |
| 67 | */ |
| 68 | public function register_block() |
| 69 | { |
| 70 | |
| 71 | wp_register_style( |
| 72 | 'cff-blocks-styles', |
| 73 | trailingslashit(CFF_PLUGIN_URL) . 'assets/css/cff-blocks.css', |
| 74 | array( 'wp-edit-blocks' ), |
| 75 | CFFVER |
| 76 | ); |
| 77 | |
| 78 | $attributes = array( |
| 79 | 'shortcodeSettings' => array( |
| 80 | 'type' => 'string', |
| 81 | ), |
| 82 | 'noNewChanges' => array( |
| 83 | 'type' => 'boolean', |
| 84 | ), |
| 85 | 'executed' => array( |
| 86 | 'type' => 'boolean', |
| 87 | ) |
| 88 | ); |
| 89 | |
| 90 | register_block_type( |
| 91 | 'cff/cff-feed-block', |
| 92 | array( |
| 93 | 'attributes' => $attributes, |
| 94 | 'render_callback' => array( $this, 'get_feed_html' ), |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Load Custom Facebook Feed Gutenberg block scripts. |
| 101 | * |
| 102 | * @since 2.3 |
| 103 | */ |
| 104 | public function enqueue_block_editor_assets() |
| 105 | { |
| 106 | $access_token = get_option('cff_access_token'); |
| 107 | |
| 108 | \cff_main()->enqueue_styles_assets(); |
| 109 | \cff_main()->enqueue_scripts_assets(); |
| 110 | |
| 111 | // cff_add_my_stylesheet(); |
| 112 | // cff_scripts_method(); |
| 113 | |
| 114 | wp_enqueue_style('cff-blocks-styles'); |
| 115 | wp_enqueue_script( |
| 116 | 'cff-feed-block', |
| 117 | trailingslashit(CFF_PLUGIN_URL) . 'assets/js/cff-blocks.js', |
| 118 | array( 'wp-blocks', 'wp-i18n', 'wp-element' ), |
| 119 | CFFVER, |
| 120 | true |
| 121 | ); |
| 122 | |
| 123 | $shortcodeSettings = ''; |
| 124 | |
| 125 | $i18n = array( |
| 126 | 'addSettings' => esc_html__('Add Settings', 'custom-facebook-feed'), |
| 127 | 'shortcodeSettings' => esc_html__('Shortcode Settings', 'custom-facebook-feed'), |
| 128 | 'example' => esc_html__('Example', 'custom-facebook-feed'), |
| 129 | 'preview' => esc_html__('Apply Changes', 'custom-facebook-feed'), |
| 130 | |
| 131 | ); |
| 132 | |
| 133 | if (! empty($_GET['cff_wizard'])) { |
| 134 | $shortcodeSettings = 'feed="' . (int)$_GET['cff_wizard'] . '"'; |
| 135 | } |
| 136 | |
| 137 | wp_localize_script( |
| 138 | 'cff-feed-block', |
| 139 | 'cff_block_editor', |
| 140 | array( |
| 141 | 'wpnonce' => wp_create_nonce('facebook-blocks'), |
| 142 | 'canShowFeed' => ! empty($access_token), |
| 143 | 'configureLink' => get_admin_url() . '?page=cff-settings', |
| 144 | 'shortcodeSettings' => $shortcodeSettings, |
| 145 | 'i18n' => $i18n, |
| 146 | ) |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get form HTML to display in a Custom Facebook Feed Gutenberg block. |
| 152 | * |
| 153 | * @param array $attr Attributes passed by Custom Facebook Feed Gutenberg block. |
| 154 | * |
| 155 | * @since 2.3 |
| 156 | * |
| 157 | * @return string |
| 158 | */ |
| 159 | public function get_feed_html($attr) |
| 160 | { |
| 161 | $cff_statuses = get_option('cff_statuses', array()); |
| 162 | |
| 163 | $return = ''; |
| 164 | |
| 165 | $shortcode_settings = isset($attr['shortcodeSettings']) ? $attr['shortcodeSettings'] : ''; |
| 166 | |
| 167 | if (empty($cff_statuses['support_legacy_shortcode'])) { |
| 168 | if (empty($shortcode_settings) || strpos($shortcode_settings, 'feed=') === false) { |
| 169 | $feeds = \CustomFacebookFeed\Builder\CFF_Feed_Builder::get_feed_list(); |
| 170 | $feed_id = $feeds[0]['id']; |
| 171 | $shortcode_settings .= ' feed="' . (int)$feed_id . '"'; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | $shortcode_settings = str_replace(array( '[custom-facebook-feed', ']' ), '', $shortcode_settings); |
| 176 | |
| 177 | $return .= do_shortcode('[custom-facebook-feed ' . $shortcode_settings . ']'); |
| 178 | |
| 179 | return $return; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Checking if is Gutenberg REST API call. |
| 184 | * |
| 185 | * @since 2.3 |
| 186 | * |
| 187 | * @return bool True if is Gutenberg REST API call. |
| 188 | */ |
| 189 | public static function is_gb_editor() |
| 190 | { |
| 191 | return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Register Block Category |
| 196 | * |
| 197 | * @since 4.1.9 |
| 198 | */ |
| 199 | public function register_block_category($categories, $context) |
| 200 | { |
| 201 | $exists = array_search('smashballoon', array_column($categories, 'slug')); |
| 202 | |
| 203 | if ($exists !== false) { |
| 204 | return $categories; |
| 205 | } |
| 206 | |
| 207 | return array_merge( |
| 208 | $categories, |
| 209 | array( |
| 210 | array( |
| 211 | 'slug' => 'smashballoon', |
| 212 | 'title' => __('Smash Balloon', 'custom-facebook-feed'), |
| 213 | ), |
| 214 | ) |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Register Block |
| 220 | * |
| 221 | * @since 4.1.9 |
| 222 | */ |
| 223 | public function register_facebook_feed_block() |
| 224 | { |
| 225 | register_block_type( |
| 226 | trailingslashit(CFF_PLUGIN_DIR) . 'assets/dist/sbf-feed', |
| 227 | array( |
| 228 | 'render_callback' => array( $this, 'render_facebook_feed_block' ), |
| 229 | ) |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Render Block |
| 235 | * |
| 236 | * @since 4.1.9 |
| 237 | */ |
| 238 | public function render_facebook_feed_block($attributes) |
| 239 | { |
| 240 | $content = ''; |
| 241 | |
| 242 | if (isset($attributes['feedId'])) { |
| 243 | $content = do_shortcode('[custom-facebook-feed feed=' . (int) $attributes['feedId'] . ']'); |
| 244 | } |
| 245 | |
| 246 | return $content; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Enqueue Block Assets |
| 251 | * |
| 252 | * @since 4.1.9 |
| 253 | */ |
| 254 | public function enqueue_facebook_feed_block_editor_assets() |
| 255 | { |
| 256 | $asset_file = include_once trailingslashit(CFF_PLUGIN_DIR) . 'assets/dist/blocks.asset.php'; |
| 257 | |
| 258 | wp_enqueue_script( |
| 259 | 'cff-feed-block-editor', |
| 260 | trailingslashit(CFF_PLUGIN_URL) . 'assets/dist/blocks.js', |
| 261 | $asset_file['dependencies'], |
| 262 | $asset_file['version'], |
| 263 | true |
| 264 | ); |
| 265 | |
| 266 | wp_enqueue_style( |
| 267 | 'cff-feed-block-editor', |
| 268 | trailingslashit(CFF_PLUGIN_URL) . 'assets/dist/blocks.css', |
| 269 | array(), |
| 270 | $asset_file['version'] |
| 271 | ); |
| 272 | |
| 273 | wp_localize_script( |
| 274 | 'cff-feed-block-editor', |
| 275 | 'cff_feed_block_editor', |
| 276 | array( |
| 277 | 'feeds' => CFF_Db::feeds_query(), |
| 278 | 'feed_url' => admin_url('admin.php?page=cff-feed-builder'), |
| 279 | 'plugins_info' => Util::get_smash_plugins_status_info(), |
| 280 | 'has_facebook_feed_block' => $this->has_facebook_feed_block(), |
| 281 | 'is_pro_active' => CFF_Utils::cff_is_pro_version(), |
| 282 | 'nonce' => wp_create_nonce('cff-admin'), |
| 283 | ) |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Set Script Translations |
| 289 | * |
| 290 | * @since 4.1.9 |
| 291 | */ |
| 292 | public function set_script_translations() |
| 293 | { |
| 294 | wp_set_script_translations('cff-feed-block-editor', 'custom-facebook-feed', CFF_PLUGIN_DIR . 'languages'); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Check if the post has a Facebook Feed block |
| 299 | * |
| 300 | * @since 4.1.9 |
| 301 | */ |
| 302 | public function has_facebook_feed_block() |
| 303 | { |
| 304 | return has_block('cff/cff-feed-block'); |
| 305 | } |
| 306 | } |
| 307 |