block-embeds.php
3 years ago
block-popup-trigger.php
3 years ago
block-slidein-trigger.php
3 years ago
block-social-share.php
3 years ago
block-unsubscribe.php
3 years ago
block-embeds.php
181 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_GHBlock_Embeds class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class Hustle_GHBlock_Embeds |
| 10 | * |
| 11 | * @since 1.0 Gutenberg Addon |
| 12 | */ |
| 13 | class Hustle_GHBlock_Embeds extends Hustle_GHBlock_Abstract { |
| 14 | |
| 15 | /** |
| 16 | * Block identifier |
| 17 | * |
| 18 | * @since 1.0 Gutenberg Addon |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $slug = 'embedded'; |
| 23 | |
| 24 | /** |
| 25 | * Hustle_GHBlock_Embeds constructor. |
| 26 | * |
| 27 | * @since 1.0 Gutenberg Addon |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | // Initialize block. |
| 31 | $this->init(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Render block markup on front-end |
| 36 | * |
| 37 | * @since 1.0 Gutenberg Addon |
| 38 | * @param array $properties Block properties. |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public function render_block( $properties = array() ) { |
| 43 | $css_class = isset( $properties['css_class'] ) ? $properties['css_class'] : ''; |
| 44 | |
| 45 | if ( isset( $properties['id'] ) ) { |
| 46 | return '[wd_hustle id="' . esc_attr( $properties['id'] ) . '" type="embedded" css_class="' . esc_attr( $css_class ) . '"/]'; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Enqueue assets ( scritps / styles ) |
| 52 | * Should be overriden in block class |
| 53 | * |
| 54 | * @since 1.0 Gutenberg Addon |
| 55 | */ |
| 56 | public function load_assets() { |
| 57 | |
| 58 | Hustle_Module_Front::add_hui_scripts(); |
| 59 | |
| 60 | // Scripts. |
| 61 | wp_enqueue_script( |
| 62 | 'hustle-block-embeds', |
| 63 | Hustle_Gutenberg::get_plugin_url() . '/js/embeds-block.min.js', |
| 64 | array( 'wp-blocks', 'wp-i18n', 'wp-element', 'hui_scripts' ), |
| 65 | filemtime( Hustle_Gutenberg::get_plugin_dir() . '/js/embeds-block.min.js' ), |
| 66 | true |
| 67 | ); |
| 68 | |
| 69 | // Localize scripts. |
| 70 | wp_localize_script( |
| 71 | 'hustle-block-embeds', |
| 72 | 'hustle_embed_data', |
| 73 | array( |
| 74 | 'modules' => $this->get_modules(), |
| 75 | 'admin_url' => admin_url( 'admin.php' ), |
| 76 | 'nonce' => wp_create_nonce( 'hustle_gutenberg_get_module' ), |
| 77 | 'shortcode_tag' => Hustle_Module_Front::SHORTCODE, |
| 78 | 'l10n' => $this->localize(), |
| 79 | ) |
| 80 | ); |
| 81 | |
| 82 | if ( isset( $this->dependencies['recaptcha'] ) ) { |
| 83 | $language = ''; |
| 84 | |
| 85 | if ( ! empty( $this->dependencies['recaptcha']['language'] ) ) { |
| 86 | $language = $this->dependencies['recaptcha']['language']; |
| 87 | } |
| 88 | |
| 89 | Hustle_Module_Front::add_recaptcha_script( $language, true ); |
| 90 | } |
| 91 | |
| 92 | wp_enqueue_style( |
| 93 | 'hustle_icons', |
| 94 | Opt_In::$plugin_url . 'assets/hustle-ui/css/hustle-icons.min.css', |
| 95 | array(), |
| 96 | Opt_In::VERSION |
| 97 | ); |
| 98 | |
| 99 | wp_enqueue_style( |
| 100 | 'hustle_optin', |
| 101 | Opt_In::$plugin_url . 'assets/hustle-ui/css/hustle-optin.min.css', |
| 102 | array(), |
| 103 | Opt_In::VERSION |
| 104 | ); |
| 105 | |
| 106 | wp_enqueue_style( |
| 107 | 'hustle_info', |
| 108 | Opt_In::$plugin_url . 'assets/hustle-ui/css/hustle-info.min.css', |
| 109 | array(), |
| 110 | Opt_In::VERSION |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get modules |
| 116 | * |
| 117 | * @return array |
| 118 | */ |
| 119 | public function get_modules() { |
| 120 | $module_list = $this->get_modules_by_type( 'embedded' ); |
| 121 | return $module_list; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get texts for localize |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | private function localize() { |
| 130 | return array( |
| 131 | 'name' => esc_html__( 'Name', 'hustle' ), |
| 132 | 'additional_css_classes' => esc_html__( 'Additional CSS Classes', 'hustle' ), |
| 133 | 'advanced' => esc_html__( 'Advanced', 'hustle' ), |
| 134 | 'module' => esc_html__( 'Module', 'hustle' ), |
| 135 | 'customize_module' => esc_html__( 'Customize embed', 'hustle' ), |
| 136 | 'rendering' => esc_html__( 'Rendering...', 'hustle' ), |
| 137 | 'block_name' => esc_html__( 'Embeds', 'hustle' ), |
| 138 | /* translators: Plugin name */ |
| 139 | 'block_description' => esc_html( sprintf( __( 'Display your %s Embed module in this block.', 'hustle' ), Opt_In_Utils::get_plugin_name() ) ), |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Check the modules' dependencies to be queued. |
| 145 | * |
| 146 | * @param Hustle_Model $module Module to be checked. |
| 147 | * @return void |
| 148 | */ |
| 149 | protected function check_dependencies( Hustle_Model $module ) { |
| 150 | |
| 151 | // Do check if recaptcha wasn't required already. |
| 152 | if ( ! isset( $this->dependencies['recaptcha'] ) ) { |
| 153 | |
| 154 | $fields = $module->get_form_fields(); |
| 155 | |
| 156 | // Check if the module has a recaptcha field. |
| 157 | if ( isset( $fields['recaptcha'] ) ) { |
| 158 | |
| 159 | $this->dependencies['recaptcha'] = array(); |
| 160 | |
| 161 | // Set the language of the first module to require recaptcha as the lang for the script. |
| 162 | if ( ! empty( $fields['recaptcha']['recaptcha_language'] ) && 'automatic' !== $fields['recaptcha']['recaptcha_language'] ) { |
| 163 | $this->dependencies['recaptcha']['language'] = $fields['recaptcha']['recaptcha_language']; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Is module included |
| 171 | * |
| 172 | * @param Hustle_Model $module Module. |
| 173 | * @return bool |
| 174 | */ |
| 175 | protected function is_module_included( Hustle_Model $module ) { |
| 176 | return $module->is_display_type_active( Hustle_Module_Model::SHORTCODE_MODULE ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | new Hustle_GHBlock_Embeds(); |
| 181 |