class-cff-blocks.php
| 1 | <?php |
| 2 | /** |
| 3 | * Custom Facebook Feed block with live preview. |
| 4 | * |
| 5 | * @since 2.3 |
| 6 | */ |
| 7 | class CFF_Blocks { |
| 8 | |
| 9 | /** |
| 10 | * Indicates if current integration is allowed to load. |
| 11 | * |
| 12 | * @since 1.8 |
| 13 | * |
| 14 | * @return bool |
| 15 | */ |
| 16 | public function allow_load() { |
| 17 | return function_exists( 'register_block_type' ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Loads an integration. |
| 22 | * |
| 23 | * @since 2.3 |
| 24 | */ |
| 25 | public function load() { |
| 26 | $this->hooks(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Integration hooks. |
| 31 | * |
| 32 | * @since 2.3 |
| 33 | */ |
| 34 | protected function hooks() { |
| 35 | add_action( 'init', array( $this, 'register_block' ) ); |
| 36 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Register Custom Facebook Feed Gutenberg block on the backend. |
| 41 | * |
| 42 | * @since 2.3 |
| 43 | */ |
| 44 | public function register_block() { |
| 45 | |
| 46 | wp_register_style( |
| 47 | 'cff-blocks-styles', |
| 48 | trailingslashit( CFF_PLUGIN_URL ) . 'css/cff-blocks.css', |
| 49 | array( 'wp-edit-blocks' ), |
| 50 | CFFVER |
| 51 | ); |
| 52 | |
| 53 | $attributes = array( |
| 54 | 'shortcodeSettings' => array( |
| 55 | 'type' => 'string', |
| 56 | ), |
| 57 | 'noNewChanges' => array( |
| 58 | 'type' => 'boolean', |
| 59 | ) |
| 60 | ); |
| 61 | |
| 62 | register_block_type( |
| 63 | 'cff/cff-feed-block', |
| 64 | array( |
| 65 | 'attributes' => $attributes, |
| 66 | 'render_callback' => array( $this, 'get_feed_html' ), |
| 67 | ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Load Custom Facebook Feed Gutenberg block scripts. |
| 73 | * |
| 74 | * @since 2.3 |
| 75 | */ |
| 76 | public function enqueue_block_editor_assets() { |
| 77 | $access_token = get_option('cff_access_token'); |
| 78 | |
| 79 | cff_add_my_stylesheet(); |
| 80 | cff_scripts_method(); |
| 81 | |
| 82 | wp_enqueue_style( 'cff-blocks-styles' ); |
| 83 | wp_enqueue_script( |
| 84 | 'cff-feed-block', |
| 85 | trailingslashit( CFF_PLUGIN_URL ) . 'js/cff-blocks.js', |
| 86 | array( 'wp-blocks', 'wp-i18n', 'wp-element' ), |
| 87 | CFFVER, |
| 88 | true |
| 89 | ); |
| 90 | |
| 91 | $shortcodeSettings = ''; |
| 92 | |
| 93 | $i18n = array( |
| 94 | 'addSettings' => esc_html__( 'Add Settings', 'instagram-feed' ), |
| 95 | 'shortcodeSettings' => esc_html__( 'Shortcode Settings', 'instagram-feed' ), |
| 96 | 'example' => esc_html__( 'Example', 'instagram-feed' ), |
| 97 | 'preview' => esc_html__( 'Apply Changes', 'instagram-feed' ), |
| 98 | |
| 99 | ); |
| 100 | |
| 101 | wp_localize_script( |
| 102 | 'cff-feed-block', |
| 103 | 'cff_block_editor', |
| 104 | array( |
| 105 | 'wpnonce' => wp_create_nonce( 'facebook-blocks' ), |
| 106 | 'canShowFeed' => ! empty( $access_token ), |
| 107 | 'configureLink' => get_admin_url() . '?page=cff-top', |
| 108 | 'shortcodeSettings' => $shortcodeSettings, |
| 109 | 'i18n' => $i18n, |
| 110 | ) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get form HTML to display in a Custom Facebook Feed Gutenberg block. |
| 116 | * |
| 117 | * @param array $attr Attributes passed by Custom Facebook Feed Gutenberg block. |
| 118 | * |
| 119 | * @since 2.3 |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | public function get_feed_html( $attr ) { |
| 124 | |
| 125 | $return = ''; |
| 126 | |
| 127 | $shortcode_settings = isset( $attr['shortcodeSettings'] ) ? $attr['shortcodeSettings'] : ''; |
| 128 | |
| 129 | $shortcode_settings = str_replace(array( '[custom-facebook-feed', ']' ), '', $shortcode_settings); |
| 130 | |
| 131 | $return .= do_shortcode( '[custom-facebook-feed '.$shortcode_settings.']' ); |
| 132 | |
| 133 | return $return; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Checking if is Gutenberg REST API call. |
| 139 | * |
| 140 | * @since 2.3 |
| 141 | * |
| 142 | * @return bool True if is Gutenberg REST API call. |
| 143 | */ |
| 144 | public static function is_gb_editor() { |
| 145 | |
| 146 | // TODO: Find a better way to check if is GB editor API call. |
| 147 | return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore |
| 148 | } |
| 149 | |
| 150 | } |
| 151 |