class-ctf-blocks.php
165 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Instagram Feed block with live preview. |
| 4 | * |
| 5 | * @since 1.7.1 |
| 6 | */ |
| 7 | class CTF_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 1.7.1 |
| 24 | */ |
| 25 | public function load() { |
| 26 | $this->hooks(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Integration hooks. |
| 31 | * |
| 32 | * @since 1.7.1 |
| 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' ), 25 ); |
| 37 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_content_assets' ) ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Register Instagram Feed Gutenberg block on the backend. |
| 42 | * |
| 43 | * @since 1.7.1 |
| 44 | */ |
| 45 | public function register_block() { |
| 46 | |
| 47 | wp_register_style( |
| 48 | 'ctf-blocks-styles', |
| 49 | trailingslashit( CTF_PLUGIN_URL ) . 'css/ctf-blocks.css', |
| 50 | array( 'wp-edit-blocks' ), |
| 51 | CTF_VERSION |
| 52 | ); |
| 53 | |
| 54 | $attributes = array( |
| 55 | 'shortcodeSettings' => array( |
| 56 | 'type' => 'string', |
| 57 | ), |
| 58 | 'noNewChanges' => array( |
| 59 | 'type' => 'boolean', |
| 60 | ), |
| 61 | 'executed' => array( |
| 62 | 'type' => 'boolean', |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | register_block_type( |
| 67 | 'ctf/ctf-feed-block', |
| 68 | array( |
| 69 | 'api_version' => 3, |
| 70 | 'attributes' => $attributes, |
| 71 | 'render_callback' => array( $this, 'get_feed_html' ), |
| 72 | 'supports' => array( 'inserter' => false ), |
| 73 | ) |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Enqueue feed frontend assets so the legacy block preview renders inside |
| 79 | * the WP 6.7+ iframe block editor. Mirrors SB_Feed_Block::enqueue_block_content_assets(). |
| 80 | * |
| 81 | * @since 2.6.0 |
| 82 | */ |
| 83 | public function enqueue_block_content_assets() { |
| 84 | if ( ! is_admin() ) { |
| 85 | return; |
| 86 | } |
| 87 | ctf_scripts_and_styles( true ); |
| 88 | wp_enqueue_style( 'ctf-blocks-styles' ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Load Instagram Feed Gutenberg block scripts. |
| 93 | * |
| 94 | * @since 1.7.1 |
| 95 | */ |
| 96 | public function enqueue_block_editor_assets() { |
| 97 | wp_enqueue_script( |
| 98 | 'ctf-feed-block', |
| 99 | trailingslashit( CTF_PLUGIN_URL ) . 'js/ctf-blocks.js', |
| 100 | array( 'wp-blocks', 'wp-i18n', 'wp-element' ), |
| 101 | CTF_VERSION, |
| 102 | true |
| 103 | ); |
| 104 | |
| 105 | $shortcodeSettings = ''; |
| 106 | |
| 107 | $i18n = array( |
| 108 | 'addSettings' => esc_html__( 'Add Settings', 'custom-twitter-feeds' ), |
| 109 | 'shortcodeSettings' => esc_html__( 'Shortcode Settings', 'custom-twitter-feeds' ), |
| 110 | 'example' => esc_html__( 'Example', 'custom-twitter-feeds' ), |
| 111 | 'preview' => esc_html__( 'Apply Changes', 'custom-twitter-feeds' ), |
| 112 | |
| 113 | ); |
| 114 | |
| 115 | wp_localize_script( |
| 116 | 'ctf-feed-block', |
| 117 | 'ctf_block_editor', |
| 118 | array( |
| 119 | 'wpnonce' => wp_create_nonce( 'ctf-blocks' ), |
| 120 | 'canShowFeed' => true, |
| 121 | 'configureLink' => get_admin_url() . '?page=custom-twitter-feeds', |
| 122 | 'shortcodeSettings' => $shortcodeSettings, |
| 123 | 'i18n' => $i18n, |
| 124 | ) |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get form HTML to display in a Instagram Feed Gutenberg block. |
| 130 | * |
| 131 | * @param array $attr Attributes passed by Instagram Feed Gutenberg block. |
| 132 | * |
| 133 | * @since 1.7.1 |
| 134 | * |
| 135 | * @return string |
| 136 | */ |
| 137 | public function get_feed_html( $attr ) { |
| 138 | |
| 139 | $return = ''; |
| 140 | |
| 141 | $shortcode_settings = isset( $attr['shortcodeSettings'] ) ? $attr['shortcodeSettings'] : ''; |
| 142 | |
| 143 | $shortcode_settings = str_replace(array( '[custom-twitter-feeds', ']' ), '', $shortcode_settings ); |
| 144 | |
| 145 | $return .= do_shortcode( '[custom-twitter-feeds '.$shortcode_settings.']' ); |
| 146 | |
| 147 | return $return; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Checking if is Gutenberg REST API call. |
| 153 | * |
| 154 | * @since 1.7.1 |
| 155 | * |
| 156 | * @return bool True if is Gutenberg REST API call. |
| 157 | */ |
| 158 | public static function is_gb_editor() { |
| 159 | |
| 160 | // TODO: Find a better way to check if is GB editor API call. |
| 161 | return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore |
| 162 | } |
| 163 | |
| 164 | } |
| 165 |