CTF_Modern_Feed_Block.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TwitterFeed\Admin\Blocks; |
| 4 | |
| 5 | use Smashballoon\TwitterFeed\Vendor\Smashballoon\Framework\Packages\Blocks\SB_Feed_Block; |
| 6 | use TwitterFeed\Builder\CTF_Db; |
| 7 | |
| 8 | // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps -- Follows codebase CTF_ prefix convention. |
| 9 | class CTF_Modern_Feed_Block extends SB_Feed_Block |
| 10 | { |
| 11 | /** |
| 12 | * Get the block name. |
| 13 | * |
| 14 | * @return string |
| 15 | */ |
| 16 | protected function get_block_name() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 17 | { |
| 18 | return 'smashballoon/twitter-feed'; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Get the shortcode tag. |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | protected function get_shortcode_tag() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 27 | { |
| 28 | return 'custom-twitter-feeds'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get the script handle for the block. |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | protected function get_script_handle() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 37 | { |
| 38 | return 'sb-feed-blocks'; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the text domain. |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | protected function get_text_domain() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 47 | { |
| 48 | return 'custom-twitter-feeds'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the plugin directory path. |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | protected function get_plugin_dir() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 57 | { |
| 58 | return trailingslashit(CTF_PLUGIN_DIR); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the action hook name for enqueuing scripts. |
| 63 | * |
| 64 | * @return string |
| 65 | */ |
| 66 | protected function get_enqueue_scripts_action() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 67 | { |
| 68 | return 'ctf_enqueue_scripts'; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the JavaScript variable name for localized block data. |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | protected function get_localize_var_name() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 77 | { |
| 78 | return 'ctfTwitterFeedBlock'; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the feed block identifier. |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | protected function get_feed_block_id() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 87 | { |
| 88 | return 'twitter'; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the JavaScript initialization function name. |
| 93 | * |
| 94 | * @return string |
| 95 | */ |
| 96 | protected function get_init_function() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 97 | { |
| 98 | return 'ctf_init'; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the block directory path. |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | protected function get_block_dir() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 107 | { |
| 108 | return $this->get_plugin_dir() . 'vendor/smashballoon/framework/Packages/Blocks/dist/feed-blocks/twitter'; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the localized data for the block editor. |
| 113 | * |
| 114 | * @return array |
| 115 | */ |
| 116 | protected function get_editor_localize_data() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 117 | { |
| 118 | $feeds = CTF_Db::feeds_query(); |
| 119 | |
| 120 | return array( |
| 121 | 'feeds' => ! empty($feeds) ? $feeds : array(), |
| 122 | 'feed_url' => admin_url( 'admin.php?page=ctf-feed-builder' ), |
| 123 | 'nonce' => wp_create_nonce('ctf-blocks'), |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Register hooks for the block. |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | public function register_hooks() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 133 | { |
| 134 | add_action('ctf_enqueue_scripts', 'ctf_scripts_and_styles'); |
| 135 | parent::register_hooks(); |
| 136 | |
| 137 | // Parent hooks register_block to init@10, but we're called during init@10 |
| 138 | // so that hook may be skipped. Call directly. |
| 139 | if (doing_action('init') || did_action('init')) { |
| 140 | $this->register_block(); |
| 141 | } |
| 142 | |
| 143 | remove_action('enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' )); |
| 144 | add_action('enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ), 25); |
| 145 | } |
| 146 | } |
| 147 |