CFF_Divi_Handler.php
190 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CustomFacebookFeed\Integrations\Divi; |
| 4 | |
| 5 | use CustomFacebookFeed\CFF_Utils; |
| 6 | use CustomFacebookFeed\Integrations\CFF_Integration; |
| 7 | |
| 8 | if (!defined('ABSPATH')) { |
| 9 | exit; // Exit if accessed directly |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Class Divi Handler. |
| 14 | * |
| 15 | * @since 4.3 |
| 16 | */ |
| 17 | class CFF_Divi_Handler |
| 18 | { |
| 19 | /** |
| 20 | * Constructor. |
| 21 | * |
| 22 | * @since 4.3 |
| 23 | */ |
| 24 | public function __construct() |
| 25 | { |
| 26 | $this->load(); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Indicate if current integration is allowed to load. |
| 32 | * |
| 33 | * @since 4.3 |
| 34 | * |
| 35 | * @return bool |
| 36 | */ |
| 37 | public function allow_load() |
| 38 | { |
| 39 | if (function_exists('et_divi_builder_init_plugin')) { |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | $allow_themes = ['Divi']; |
| 44 | $theme_name = get_template(); |
| 45 | |
| 46 | return in_array($theme_name, $allow_themes, true); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Load an integration. |
| 52 | * |
| 53 | * @since 4.3 |
| 54 | */ |
| 55 | public function load() |
| 56 | { |
| 57 | if ($this->allow_load()) { |
| 58 | $this->hooks(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Hooks. |
| 65 | * |
| 66 | * @since 4.3 |
| 67 | */ |
| 68 | public function hooks() |
| 69 | { |
| 70 | |
| 71 | add_action('et_builder_ready', [ $this, 'register_module' ]); |
| 72 | |
| 73 | if (wp_doing_ajax()) { |
| 74 | add_action('wp_ajax_sb_facebookfeed_divi_preview', [ $this, 'preview' ]); |
| 75 | } |
| 76 | |
| 77 | if ($this->is_divi_builder()) { |
| 78 | add_action('wp_enqueue_scripts', [ $this, 'builder_scripts' ]); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Load scripts. |
| 84 | * |
| 85 | * @since 4.3 |
| 86 | */ |
| 87 | public function builder_scripts() |
| 88 | { |
| 89 | |
| 90 | wp_enqueue_script( |
| 91 | 'sbfacebook-divi', |
| 92 | // The unminified version is not supported by the browser. |
| 93 | CFF_PLUGIN_URL . 'admin/assets/js/divi-handler.min.js', |
| 94 | [ 'react', 'react-dom', 'jquery' ], |
| 95 | CFFVER, |
| 96 | true |
| 97 | ); |
| 98 | |
| 99 | wp_enqueue_script( |
| 100 | 'cff-builders-handler', |
| 101 | // The unminified version is not supported by the browser. |
| 102 | CFF_PLUGIN_URL . 'admin/assets/js/builders-preview-handler.js', |
| 103 | ['jquery'], |
| 104 | CFFVER, |
| 105 | true |
| 106 | ); |
| 107 | |
| 108 | wp_localize_script( |
| 109 | 'sbfacebook-divi', |
| 110 | 'sb_divi_builder', |
| 111 | [ |
| 112 | 'ajax_handler' => admin_url('admin-ajax.php'), |
| 113 | 'nonce' => wp_create_nonce('cff-admin'), |
| 114 | 'feed_splash' => htmlspecialchars(CFF_Integration::get_widget_cta('button')) |
| 115 | ] |
| 116 | ); |
| 117 | wp_register_script( |
| 118 | 'cffscripts', |
| 119 | CFF_PLUGIN_URL . 'assets/js/cff-scripts.min.js', |
| 120 | array('jquery'), |
| 121 | CFFVER, |
| 122 | true |
| 123 | ); |
| 124 | wp_localize_script( |
| 125 | 'cffscripts', |
| 126 | 'cffOptions', |
| 127 | [ |
| 128 | 'placeholder' => CFF_PLUGIN_URL . 'assets/img/placeholder.png', |
| 129 | ] |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Register module. |
| 136 | * |
| 137 | * @since 4.3 |
| 138 | */ |
| 139 | public function register_module() |
| 140 | { |
| 141 | |
| 142 | if (! class_exists('ET_Builder_Module')) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | new SBFacebookFeed(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Ajax handler for the Feed preview. |
| 152 | * |
| 153 | * @since 4.3 |
| 154 | */ |
| 155 | public function preview() |
| 156 | { |
| 157 | |
| 158 | // check_ajax_referer('cff-admin', 'nonce'); |
| 159 | |
| 160 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 161 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 162 | if (! current_user_can($cap)) { |
| 163 | wp_send_json_error(); // This auto-dies. |
| 164 | } |
| 165 | |
| 166 | $feed_id = absint(filter_input(INPUT_POST, 'feed_id', FILTER_SANITIZE_NUMBER_INT)); |
| 167 | |
| 168 | wp_send_json_success( |
| 169 | do_shortcode( |
| 170 | sprintf( |
| 171 | '[custom-facebook-feed feed="%1$s" disable-js-loading="true"]', |
| 172 | absint($feed_id) |
| 173 | ) |
| 174 | ) |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Determine if a current page is opened in the Divi Builder. |
| 180 | * |
| 181 | * @since 4.3 |
| 182 | * |
| 183 | * @return bool |
| 184 | */ |
| 185 | private function is_divi_builder() |
| 186 | { |
| 187 | return ! empty($_GET['et_fb']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 188 | } |
| 189 | } |
| 190 |