Admin
3 years ago
Builder
3 years ago
Helpers
3 years ago
CFF_Autolink.php
3 years ago
CFF_Blocks.php
3 years ago
CFF_Cache.php
3 years ago
CFF_Education.php
3 years ago
CFF_Elementor_Base.php
3 years ago
CFF_Elementor_Widget.php
3 years ago
CFF_Error_Reporter.php
3 years ago
CFF_FB_Settings.php
3 years ago
CFF_Feed_Elementor_Control.php
3 years ago
CFF_Feed_Locator.php
3 years ago
CFF_Feed_Pro.php
3 years ago
CFF_GDPR_Integrations.php
3 years ago
CFF_Group_Posts.php
3 years ago
CFF_HTTP_Request.php
3 years ago
CFF_Oembed.php
3 years ago
CFF_Parse.php
3 years ago
CFF_Resizer.php
3 years ago
CFF_Response.php
3 years ago
CFF_Shortcode.php
3 years ago
CFF_Shortcode_Display.php
3 years ago
CFF_SiteHealth.php
3 years ago
CFF_Utils.php
3 years ago
CFF_View.php
3 years ago
Custom_Facebook_Feed.php
3 years ago
SB_Facebook_Data_Encryption.php
3 years ago
SB_Facebook_Data_Manager.php
3 years ago
CFF_Blocks.php
169 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Custom Facebook Feed block with live preview. |
| 4 | * |
| 5 | * @since 2.3 |
| 6 | */ |
| 7 | namespace CustomFacebookFeed; |
| 8 | |
| 9 | class CFF_Blocks { |
| 10 | |
| 11 | /** |
| 12 | * Indicates if current integration is allowed to load. |
| 13 | * |
| 14 | * @since 1.8 |
| 15 | * |
| 16 | * @return bool |
| 17 | */ |
| 18 | public function allow_load() { |
| 19 | return function_exists( 'register_block_type' ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Loads an integration. |
| 24 | * |
| 25 | * @since 2.3 |
| 26 | */ |
| 27 | public function load() { |
| 28 | $this->hooks(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Integration hooks. |
| 33 | * |
| 34 | * @since 2.3 |
| 35 | */ |
| 36 | protected function hooks() { |
| 37 | add_action( 'init', array( $this, 'register_block' ) ); |
| 38 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Register Custom Facebook Feed Gutenberg block on the backend. |
| 43 | * |
| 44 | * @since 2.3 |
| 45 | */ |
| 46 | public function register_block() { |
| 47 | |
| 48 | wp_register_style( |
| 49 | 'cff-blocks-styles', |
| 50 | trailingslashit( CFF_PLUGIN_URL ) . 'assets/css/cff-blocks.css', |
| 51 | array( 'wp-edit-blocks' ), |
| 52 | CFFVER |
| 53 | ); |
| 54 | |
| 55 | $attributes = array( |
| 56 | 'shortcodeSettings' => array( |
| 57 | 'type' => 'string', |
| 58 | ), |
| 59 | 'noNewChanges' => array( |
| 60 | 'type' => 'boolean', |
| 61 | ), |
| 62 | 'executed' => array( |
| 63 | 'type' => 'boolean', |
| 64 | ) |
| 65 | ); |
| 66 | |
| 67 | register_block_type( |
| 68 | 'cff/cff-feed-block', |
| 69 | array( |
| 70 | 'attributes' => $attributes, |
| 71 | 'render_callback' => array( $this, 'get_feed_html' ), |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Load Custom Facebook Feed Gutenberg block scripts. |
| 78 | * |
| 79 | * @since 2.3 |
| 80 | */ |
| 81 | public function enqueue_block_editor_assets() { |
| 82 | $access_token = get_option('cff_access_token'); |
| 83 | |
| 84 | \cff_main()->enqueue_styles_assets(); |
| 85 | \cff_main()->enqueue_scripts_assets(); |
| 86 | |
| 87 | #cff_add_my_stylesheet(); |
| 88 | #cff_scripts_method(); |
| 89 | |
| 90 | wp_enqueue_style( 'cff-blocks-styles' ); |
| 91 | wp_enqueue_script( |
| 92 | 'cff-feed-block', |
| 93 | trailingslashit( CFF_PLUGIN_URL ) . 'assets/js/cff-blocks.js', |
| 94 | array( 'wp-blocks', 'wp-i18n', 'wp-element' ), |
| 95 | CFFVER, |
| 96 | true |
| 97 | ); |
| 98 | |
| 99 | $shortcodeSettings = ''; |
| 100 | |
| 101 | $i18n = array( |
| 102 | 'addSettings' => esc_html__( 'Add Settings', 'custom-facebook-feed' ), |
| 103 | 'shortcodeSettings' => esc_html__( 'Shortcode Settings', 'custom-facebook-feed' ), |
| 104 | 'example' => esc_html__( 'Example', 'custom-facebook-feed' ), |
| 105 | 'preview' => esc_html__( 'Apply Changes', 'custom-facebook-feed' ), |
| 106 | |
| 107 | ); |
| 108 | |
| 109 | if ( ! empty( $_GET['cff_wizard'] ) ) { |
| 110 | $shortcodeSettings = 'feed="' . (int)$_GET['cff_wizard'] . '"'; |
| 111 | } |
| 112 | |
| 113 | wp_localize_script( |
| 114 | 'cff-feed-block', |
| 115 | 'cff_block_editor', |
| 116 | array( |
| 117 | 'wpnonce' => wp_create_nonce( 'facebook-blocks' ), |
| 118 | 'canShowFeed' => ! empty( $access_token ), |
| 119 | 'configureLink' => get_admin_url() . '?page=cff-settings', |
| 120 | 'shortcodeSettings' => $shortcodeSettings, |
| 121 | 'i18n' => $i18n, |
| 122 | ) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get form HTML to display in a Custom Facebook Feed Gutenberg block. |
| 128 | * |
| 129 | * @param array $attr Attributes passed by Custom Facebook Feed Gutenberg block. |
| 130 | * |
| 131 | * @since 2.3 |
| 132 | * |
| 133 | * @return string |
| 134 | */ |
| 135 | public function get_feed_html( $attr ) { |
| 136 | |
| 137 | $return = ''; |
| 138 | |
| 139 | $shortcode_settings = isset( $attr['shortcodeSettings'] ) ? $attr['shortcodeSettings'] : ''; |
| 140 | |
| 141 | if ( empty( $cff_statuses['support_legacy_shortcode'] ) ) { |
| 142 | if ( empty( $shortcode_settings ) || strpos( $shortcode_settings, 'feed=' ) === false ){ |
| 143 | $feeds = \CustomFacebookFeed\Builder\CFF_Feed_Builder::get_feed_list(); |
| 144 | $feed_id = $feeds[0]['id']; |
| 145 | $shortcode_settings .= ' feed="' . (int)$feed_id . '"'; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | $shortcode_settings = str_replace(array( '[custom-facebook-feed', ']' ), '', $shortcode_settings); |
| 150 | |
| 151 | $return .= do_shortcode( '[custom-facebook-feed '.$shortcode_settings.']' ); |
| 152 | |
| 153 | return $return; |
| 154 | |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Checking if is Gutenberg REST API call. |
| 159 | * |
| 160 | * @since 2.3 |
| 161 | * |
| 162 | * @return bool True if is Gutenberg REST API call. |
| 163 | */ |
| 164 | public static function is_gb_editor() { |
| 165 | return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore |
| 166 | } |
| 167 | |
| 168 | } |
| 169 |