EmbedpressSettings.php
723 lines
| 1 | <?php |
| 2 | namespace EmbedPress\Ends\Back\Settings; |
| 3 | |
| 4 | use EmbedPress\Includes\Classes\Helper; |
| 5 | |
| 6 | class EmbedpressSettings { |
| 7 | var $page_slug = ''; |
| 8 | /** |
| 9 | * @var int|string |
| 10 | */ |
| 11 | protected $file_version; |
| 12 | |
| 13 | public function __construct($page_slug = 'embedpress') { |
| 14 | $this->page_slug = $page_slug; |
| 15 | $this->file_version = defined( 'WP_DEBUG') && WP_DEBUG ? time() : EMBEDPRESS_VERSION; |
| 16 | add_action('admin_enqueue_scripts', [$this, 'handle_scripts_and_styles']); |
| 17 | add_action('admin_menu', [$this, 'register_menu']); |
| 18 | add_action( 'init', [$this, 'save_settings']); |
| 19 | |
| 20 | // Add activation redirect hook |
| 21 | add_action( 'admin_init', [$this, 'embedpress_maybe_redirect_to_settings']); |
| 22 | |
| 23 | // ajax |
| 24 | add_action( 'wp_ajax_embedpress_elements_action', [$this, 'update_elements_list']); |
| 25 | add_action( 'wp_ajax_embedpress_settings_action', [$this, 'save_settings']); |
| 26 | add_action( 'wp_ajax_save_global_brand_image', [$this, 'save_global_brand_image']); |
| 27 | add_action( 'wp_ajax_embedpress_dismiss_element', [$this, 'dismiss_element']); |
| 28 | |
| 29 | $g_settings = get_option( EMBEDPRESS_PLG_NAME, [] ); |
| 30 | |
| 31 | if(!isset($g_settings['turn_off_rating_help'])){ |
| 32 | $g_settings['turn_off_rating_help'] = true; |
| 33 | update_option(EMBEDPRESS_PLG_NAME, $g_settings); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | // Migration |
| 38 | $option = 'embedpress_elements_updated'; // to update initially for backward compatibility |
| 39 | if ( !get_option( $option, false) ) { |
| 40 | $elements_initial_states = [ |
| 41 | 'gutenberg' => [ |
| 42 | 'google-docs-block' => 'google-docs-block', |
| 43 | 'document' => 'document', |
| 44 | 'embedpress' => 'embedpress', |
| 45 | 'embedpress-pdf' => 'embedpress-pdf', |
| 46 | 'google-sheets-block' => 'google-sheets-block', |
| 47 | 'google-slides-block' => 'google-slides-block', |
| 48 | 'youtube-block' => 'youtube-block', |
| 49 | 'google-forms-block' => 'google-forms-block', |
| 50 | 'google-drawings-block' => 'google-drawings-block', |
| 51 | 'google-maps-block' => 'google-maps-block', |
| 52 | 'twitch-block' => 'twitch-block', |
| 53 | 'wistia-block' => 'wistia-block', |
| 54 | 'vimeo-block' => 'vimeo-block', |
| 55 | ], |
| 56 | 'elementor' => [ |
| 57 | 'embedpress-document' => 'embedpress-document', |
| 58 | 'embedpress' => 'embedpress', |
| 59 | 'embedpress-pdf' => 'embedpress-pdf', |
| 60 | ] |
| 61 | ]; |
| 62 | |
| 63 | |
| 64 | $settings = get_option( EMBEDPRESS_PLG_NAME, [] ); |
| 65 | $yt = get_option( EMBEDPRESS_PLG_NAME.':youtube' ); |
| 66 | if ( empty( $settings) && empty( $yt) ) { |
| 67 | $settings['need_first_time_redirect'] = true; |
| 68 | } |
| 69 | if ( !isset( $settings['enablePluginInAdmin']) ) { |
| 70 | $settings['enablePluginInAdmin'] = 1; |
| 71 | } |
| 72 | if ( !isset( $settings['enablePluginInFront']) ) { |
| 73 | $settings['enablePluginInFront'] = 1; |
| 74 | } |
| 75 | |
| 76 | update_option( EMBEDPRESS_PLG_NAME.":elements", $elements_initial_states); |
| 77 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 78 | update_option( $option, true); |
| 79 | } |
| 80 | $migration_v_320 = 'embedpress_v_320_migration'; |
| 81 | if ( !get_option( $migration_v_320, false) ) { |
| 82 | $elements = (array) get_option( EMBEDPRESS_PLG_NAME.":elements", []); |
| 83 | $elements['gutenberg']['embedpress-pdf'] = 'embedpress-pdf'; |
| 84 | $elements['elementor']['embedpress-pdf'] = 'embedpress-pdf'; |
| 85 | update_option( EMBEDPRESS_PLG_NAME.":elements", $elements); |
| 86 | update_option( $migration_v_320, true); |
| 87 | } |
| 88 | |
| 89 | $migration_v_330 = 'embedpress_v_330_migration'; |
| 90 | if ( !get_option( $migration_v_330, false) ) { |
| 91 | $elements = (array) get_option( EMBEDPRESS_PLG_NAME.":elements", []); |
| 92 | $elements['gutenberg']['embedpress-calendar'] = 'embedpress-calendar'; |
| 93 | $elements['elementor']['embedpress-calendar'] = 'embedpress-calendar'; |
| 94 | update_option( EMBEDPRESS_PLG_NAME.":elements", $elements); |
| 95 | update_option( $migration_v_330, true); |
| 96 | } |
| 97 | |
| 98 | add_action( 'admin_init', [$this, 'embedpress_maybe_redirect_to_settings'] ); |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | } |
| 104 | function embedpress_maybe_redirect_to_settings() { |
| 105 | $settings = get_option( EMBEDPRESS_PLG_NAME, [] ); |
| 106 | if ( isset( $settings['need_first_time_redirect']) && $settings['need_first_time_redirect'] ) { |
| 107 | // Skip redirect if already done, doing AJAX, or in certain admin contexts |
| 108 | if ( get_option( 'embedpress_activation_redirect_done' ) || wp_doing_ajax() || wp_doing_cron() ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | // Skip redirect for bulk activations, network admin, or CLI |
| 113 | if ( is_network_admin() || isset( $_GET['activate-multi'] ) || defined( 'WP_CLI' ) ) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // Skip redirect if not in admin area or if user doesn't have proper capabilities |
| 118 | if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // Skip redirect if we're already on the EmbedPress settings page |
| 123 | if ( isset( $_GET['page'] ) && $_GET['page'] === $this->page_slug ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | // Set redirect done flag and clear the redirect trigger |
| 128 | update_option( 'embedpress_activation_redirect_done', true ); |
| 129 | $settings['need_first_time_redirect'] = false; |
| 130 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 131 | |
| 132 | // Perform the redirect |
| 133 | wp_safe_redirect( admin_url('admin.php?page='.$this->page_slug) ); |
| 134 | exit; |
| 135 | } |
| 136 | } |
| 137 | public function update_elements_list() { |
| 138 | |
| 139 | if (!current_user_can('manage_options')) { |
| 140 | wp_send_json_error(array('message' => 'You do not have sufficient permissions to access this functionality.')); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | if ( !empty($_POST['_wpnonce'] && wp_verify_nonce( $_POST['_wpnonce'], 'embedpress_elements_action')) ) { |
| 145 | $option = EMBEDPRESS_PLG_NAME.":elements"; |
| 146 | $elements = (array) get_option( $option, []); |
| 147 | $settings = (array) get_option( EMBEDPRESS_PLG_NAME, []); |
| 148 | |
| 149 | $type = !empty( $_POST['element_type']) ? sanitize_text_field( $_POST['element_type']) : ''; |
| 150 | $name = !empty( $_POST['element_name']) ? sanitize_text_field( $_POST['element_name']) : ''; |
| 151 | $checked = !empty( $_POST['checked']) ? $_POST['checked'] : false; |
| 152 | if ( 'false' != $checked ) { |
| 153 | $elements[$type][$name] = $name; |
| 154 | if ( $type === 'classic' ) { |
| 155 | $settings[$name] = 1; |
| 156 | } |
| 157 | }else{ |
| 158 | if( isset( $elements[$type]) && isset( $elements[$type][$name])){ |
| 159 | unset( $elements[$type][$name]); |
| 160 | } |
| 161 | if ( $type === 'classic' ) { |
| 162 | $settings[$name] = 0; |
| 163 | } |
| 164 | } |
| 165 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 166 | update_option( $option, $elements); |
| 167 | wp_send_json_success(); |
| 168 | } |
| 169 | wp_send_json_error(); |
| 170 | } |
| 171 | |
| 172 | public function register_menu() { |
| 173 | add_menu_page( __('EmbedPress Settings', 'embedpress'), 'EmbedPress', 'manage_options', $this->page_slug, |
| 174 | [ $this, 'render_settings_page' ], EMBEDPRESS_URL_ASSETS.'images/menu-icon.svg', 64 ); |
| 175 | |
| 176 | // Add Dashboard submenu (replaces the default first submenu item) |
| 177 | add_submenu_page( $this->page_slug, __('EmbedPress Dashboard', 'embedpress'), __('Dashboard', 'embedpress'), 'manage_options', $this->page_slug, |
| 178 | [ $this, 'render_settings_page' ] ); |
| 179 | |
| 180 | // Add Branding submenu |
| 181 | add_submenu_page( $this->page_slug, __('EmbedPress Branding', 'embedpress'), __('Branding', 'embedpress'), 'manage_options', $this->page_slug . '&page_type=custom-logo', |
| 182 | [ $this, 'render_settings_page' ] ); |
| 183 | |
| 184 | // Add Custom Ads submenu |
| 185 | add_submenu_page( $this->page_slug, __('EmbedPress Custom Ads', 'embedpress'), __('Custom Ads', 'embedpress'), 'manage_options', $this->page_slug . '&page_type=ads', |
| 186 | [ $this, 'render_settings_page' ] ); |
| 187 | |
| 188 | |
| 189 | // Add Shortcode submenu |
| 190 | add_submenu_page( $this->page_slug, __('EmbedPress Shortcode', 'embedpress'), __('Shortcode', 'embedpress'), 'manage_options', $this->page_slug . '&page_type=shortcode', |
| 191 | [ $this, 'render_settings_page' ] ); |
| 192 | |
| 193 | // Add Settings submenu |
| 194 | add_submenu_page( $this->page_slug, __('EmbedPress Settings', 'embedpress'), __('Settings', 'embedpress'), 'manage_options', $this->page_slug . '&page_type=settings', |
| 195 | [ $this, 'render_settings_page' ] ); |
| 196 | |
| 197 | |
| 198 | // Add License submenu (only if pro is active) |
| 199 | if ( apply_filters('embedpress/is_allow_rander', false) ) { |
| 200 | add_submenu_page( $this->page_slug, __('EmbedPress License', 'embedpress'), __('License', 'embedpress'), 'manage_options', $this->page_slug . '&page_type=license', |
| 201 | [ $this, 'render_settings_page' ] ); |
| 202 | } |
| 203 | |
| 204 | // Add admin footer script to handle menu highlighting |
| 205 | add_action('admin_footer', [$this, 'admin_menu_highlight_script']); |
| 206 | |
| 207 | // Add filter to reorder menu items - License should be last |
| 208 | add_filter('admin_menu', [$this, 'reorder_submenu_items'], 999); |
| 209 | } |
| 210 | |
| 211 | public function handle_scripts_and_styles() { |
| 212 | if ( !empty( $_REQUEST['page']) && $this->page_slug === $_REQUEST['page'] ) { |
| 213 | $this->enqueue_styles(); |
| 214 | $this->enqueue_scripts(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | public function admin_menu_highlight_script() { |
| 219 | // Only load on EmbedPress admin pages |
| 220 | $current_page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : ''; |
| 221 | if ($current_page !== $this->page_slug) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | $page_type = isset($_GET['page_type']) ? sanitize_text_field($_GET['page_type']) : ''; |
| 226 | // Fixed: Evaluate PHP filter on server-side before passing to JavaScript |
| 227 | $is_pro_active = apply_filters('embedpress/is_allow_rander', false); |
| 228 | |
| 229 | ?> |
| 230 | <script type="text/javascript"> |
| 231 | /* EmbedPress Admin Menu Highlight Script - Fixed apply_filters issue */ |
| 232 | jQuery(document).ready(function($) { |
| 233 | // Remove current highlighting |
| 234 | $('#adminmenu .wp-submenu li').removeClass('current'); |
| 235 | $('#adminmenu .wp-submenu a').removeClass('current'); |
| 236 | |
| 237 | var pageType = '<?php echo esc_js($page_type); ?>'; |
| 238 | var isProActive = <?php echo $is_pro_active ? 'true' : 'false'; ?>; |
| 239 | var menuSelector = ''; |
| 240 | |
| 241 | // Map page types to menu selectors |
| 242 | switch(pageType) { |
| 243 | case 'settings': |
| 244 | menuSelector = 'a[href*="page_type=settings"]'; |
| 245 | break; |
| 246 | case 'shortcode': |
| 247 | menuSelector = 'a[href*="page_type=shortcode"]'; |
| 248 | break; |
| 249 | case 'sources': |
| 250 | menuSelector = 'a[href*="page_type=sources"]'; |
| 251 | break; |
| 252 | case 'elements': |
| 253 | menuSelector = 'a[href*="page_type=elements"]'; |
| 254 | break; |
| 255 | case 'custom-logo': |
| 256 | menuSelector = 'a[href*="page_type=custom-logo"]'; |
| 257 | break; |
| 258 | case 'ads': |
| 259 | menuSelector = 'a[href*="page_type=ads"]'; |
| 260 | break; |
| 261 | case 'license': |
| 262 | menuSelector = 'a[href*="page_type=license"]'; |
| 263 | break; |
| 264 | default: |
| 265 | // Default to Dashboard (no page_type or hub) |
| 266 | menuSelector = 'a[href="admin.php?page=<?php echo esc_js($this->page_slug); ?>"]'; |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | // Highlight the correct menu item |
| 271 | if (menuSelector) { |
| 272 | var $menuItem = $('#adminmenu .wp-submenu ' + menuSelector); |
| 273 | if ($menuItem.length) { |
| 274 | $menuItem.addClass('current').parent().addClass('current'); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Scroll to embedpress-body section when page_type is present |
| 279 | if (pageType && pageType !== '' && !isProActive) { |
| 280 | var $embedpressBody = $('.embedpress-body'); |
| 281 | if ($embedpressBody.length) { |
| 282 | // Get the scroll container (could be window, body, or template wrapper) |
| 283 | var scrollTop = $embedpressBody.offset().top - 60; // 20px offset from top |
| 284 | |
| 285 | // Function to find the scrollable parent |
| 286 | function findScrollableParent(element) { |
| 287 | var $element = $(element); |
| 288 | var $parents = $element.parents().addBack(); |
| 289 | |
| 290 | for (var i = 0; i < $parents.length; i++) { |
| 291 | var $parent = $($parents[i]); |
| 292 | if ($parent[0] === document.documentElement || $parent[0] === document.body) { |
| 293 | return $('html, body'); |
| 294 | } |
| 295 | |
| 296 | var overflow = $parent.css('overflow-y'); |
| 297 | if (overflow === 'scroll' || overflow === 'auto') { |
| 298 | if ($parent[0].scrollHeight > $parent[0].clientHeight) { |
| 299 | return $parent; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | return $('html, body'); |
| 304 | } |
| 305 | |
| 306 | // Find and scroll the appropriate container |
| 307 | var $scrollContainer = findScrollableParent($embedpressBody); |
| 308 | $scrollContainer.animate({ |
| 309 | scrollTop: scrollTop |
| 310 | }, 100); |
| 311 | } |
| 312 | } |
| 313 | }); |
| 314 | </script> |
| 315 | <?php |
| 316 | } |
| 317 | |
| 318 | public function enqueue_scripts() { |
| 319 | if ( !did_action( 'wp_enqueue_media') ) { |
| 320 | wp_enqueue_media(); |
| 321 | } |
| 322 | // Settings assets and localization are now handled by AssetManager and LocalizationManager |
| 323 | // This method is kept for backward compatibility but functionality has been moved |
| 324 | } |
| 325 | |
| 326 | public function enqueue_styles() { |
| 327 | // Settings styles are now handled by AssetManager |
| 328 | // Keep only WordPress core styles that are needed |
| 329 | wp_enqueue_style( 'wp-color-picker' ); |
| 330 | } |
| 331 | |
| 332 | public function render_settings_page( ) { |
| 333 | global $template, $page_slug, $nonce_field, $ep_page, $gen_menu_template_names, $brand_menu_template_names, $pro_active, $coming_soon, $success_message, $error_message, $platform_menu_template_names; |
| 334 | |
| 335 | $page_slug = $this->page_slug; // make this available for included template |
| 336 | $template = !empty( $_GET['page_type'] ) ? sanitize_file_name( $_GET['page_type']) : 'hub'; |
| 337 | |
| 338 | $nonce_field = wp_nonce_field('ep_settings_nonce', 'ep_settings_nonce', true, false); |
| 339 | $ep_page = admin_url('admin.php?page='.$this->page_slug); |
| 340 | $gen_menu_template_names = apply_filters('ep_general_menu_tmpl_names', ['settings', 'shortcode',]); |
| 341 | $platform_menu_template_names = apply_filters('ep_platform_menu_tmpl_names', [ 'youtube', 'vimeo', 'wistia', 'twitch','dailymotion', 'soundcloud' ,'spotify','google-calendar','opensea']); |
| 342 | $brand_menu_template_names = apply_filters('ep_brand_menu_templates', ['custom-logo', 'branding',]); |
| 343 | $pro_active = apply_filters('embedpress/is_allow_rander', false); |
| 344 | $coming_soon = "<span class='ep-coming-soon'>". esc_html__( '(Coming soon)', 'embedpress'). "</span>"; |
| 345 | $success_message = esc_html__( "Settings Updated", "embedpress" ); |
| 346 | $error_message = esc_html__( "Ops! Something went wrong.", "embedpress" ); |
| 347 | include_once EMBEDPRESS_SETTINGS_PATH . 'templates/main-template.php'; |
| 348 | } |
| 349 | |
| 350 | public function save_settings() { |
| 351 | // needs to check for ajax and return response accordingly. |
| 352 | if ( !empty( $_POST['ep_settings_nonce']) && wp_verify_nonce( $_POST['ep_settings_nonce'], 'ep_settings_nonce') ) { |
| 353 | $submit_type = !empty( $_POST['submit'] ) ? $_POST['submit'] : ''; |
| 354 | $save_handler_method = "save_{$submit_type}_settings"; |
| 355 | do_action( "before_{$save_handler_method}"); |
| 356 | do_action( "before_embedpress_settings_save"); |
| 357 | if ( method_exists( $this, $save_handler_method ) ) { |
| 358 | $this->$save_handler_method(); |
| 359 | } |
| 360 | do_action( "after_embedpress_settings_save"); |
| 361 | $return_url = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url(); |
| 362 | $return_url = add_query_arg( 'success', 1, $return_url ); |
| 363 | if ( wp_doing_ajax() ) { |
| 364 | wp_send_json_success(); |
| 365 | } |
| 366 | wp_safe_redirect( $return_url); |
| 367 | exit(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | public function save_settings_settings() { |
| 372 | $settings = (array) get_option( EMBEDPRESS_PLG_NAME, []); |
| 373 | $settings ['enableEmbedResizeWidth'] = isset( $_POST['enableEmbedResizeWidth']) ? intval( $_POST['enableEmbedResizeWidth']) : 600; |
| 374 | $settings ['enableEmbedResizeHeight'] = isset( $_POST['enableEmbedResizeHeight']) ? intval( $_POST['enableEmbedResizeHeight']) : 550; |
| 375 | $settings ['pdf_custom_color_settings'] = isset( $_POST['pdf_custom_color_settings']) ? intval( $_POST['pdf_custom_color_settings']) : 0; |
| 376 | $settings ['turn_off_rating_help'] = isset( $_POST['turn_off_rating_help']) ? intval( $_POST['turn_off_rating_help']) : 0; |
| 377 | |
| 378 | $settings ['custom_color'] = isset( $_POST['custom_color']) ? $_POST['custom_color'] : '#333333'; |
| 379 | |
| 380 | // Pro will handle g_loading_animation settings and other |
| 381 | // Keep backward compatibility with old filter names |
| 382 | $settings = apply_filters( 'ep_general_settings_before_save', $settings, $_POST); |
| 383 | $settings = apply_filters( 'ep_settings_settings_before_save', $settings, $_POST); |
| 384 | |
| 385 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 386 | |
| 387 | // Keep backward compatibility with old action names |
| 388 | do_action( 'ep_general_settings_after_save', $settings, $_POST); |
| 389 | do_action( 'ep_settings_settings_after_save', $settings, $_POST); |
| 390 | } |
| 391 | |
| 392 | // Keep backward compatibility method |
| 393 | public function save_general_settings() { |
| 394 | return $this->save_settings_settings(); |
| 395 | } |
| 396 | |
| 397 | public function save_youtube_settings() { |
| 398 | $option_name = EMBEDPRESS_PLG_NAME.':youtube'; |
| 399 | $settings = get_option( $option_name, []); |
| 400 | $settings['api_key'] = isset( $_POST['api_key']) ? sanitize_text_field( $_POST['api_key']) : 0; |
| 401 | $settings['pagesize'] = isset( $_POST['pagesize']) ? sanitize_text_field( $_POST['pagesize']) : 0; |
| 402 | $settings['start_time'] = isset( $_POST['start_time']) ? sanitize_text_field( $_POST['start_time']) : 0; |
| 403 | $settings['end_time'] = isset( $_POST['end_time']) ? sanitize_text_field( $_POST['end_time']) : 0; |
| 404 | $settings['autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : ''; |
| 405 | $settings['controls'] = isset( $_POST['controls']) ? sanitize_text_field( $_POST['controls']) : ''; |
| 406 | $settings['fs'] = isset( $_POST['fs']) ? sanitize_text_field( $_POST['fs']) : ''; |
| 407 | $settings['iv_load_policy'] = isset( $_POST['iv_load_policy']) ? sanitize_text_field( $_POST['iv_load_policy']) : 1; |
| 408 | $settings['color'] = isset( $_POST['color']) ? sanitize_text_field( $_POST['color']) : 'red'; |
| 409 | $settings['rel'] = isset( $_POST['rel']) ? sanitize_text_field( $_POST['rel']) : 1; |
| 410 | $settings['license_key'] = 1; // backward compatibility |
| 411 | |
| 412 | // Pro will handle g_loading_animation settings and other |
| 413 | $settings = apply_filters( 'ep_youtube_settings_before_save', $settings); |
| 414 | update_option( $option_name, $settings); |
| 415 | do_action( 'ep_youtube_settings_after_save', $settings); |
| 416 | |
| 417 | } |
| 418 | |
| 419 | public function save_wistia_settings() { |
| 420 | $option_name = EMBEDPRESS_PLG_NAME.':wistia'; |
| 421 | $settings = get_option( $option_name, []); |
| 422 | $settings['start_time'] = isset( $_POST['start_time']) ? sanitize_text_field( $_POST['start_time']) : 0; |
| 423 | $settings['autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : ''; |
| 424 | $settings['display_fullscreen_button'] = isset( $_POST['display_fullscreen_button']) ? sanitize_text_field( $_POST['display_fullscreen_button']) : ''; |
| 425 | $settings['small_play_button'] = isset( $_POST['small_play_button']) ? sanitize_text_field( $_POST['small_play_button']) : ''; |
| 426 | $settings['player_color'] = isset( $_POST['player_color']) ? sanitize_text_field( $_POST['player_color']) : ''; |
| 427 | $settings['plugin_resumable'] = isset( $_POST['plugin_resumable']) ? sanitize_text_field( $_POST['plugin_resumable']) : ''; |
| 428 | $settings['plugin_focus'] = isset( $_POST['plugin_focus']) ? sanitize_text_field( $_POST['plugin_focus']) : ''; |
| 429 | $settings['plugin_rewind'] = isset( $_POST['plugin_rewind']) ? sanitize_text_field( $_POST['plugin_rewind']) : ''; |
| 430 | $settings['display_playbar'] = isset( $_POST['display_playbar']) ? sanitize_text_field( $_POST['display_playbar']) : 1; |
| 431 | $settings['plugin_rewind_time'] = isset( $_POST['plugin_rewind_time']) ? sanitize_text_field( $_POST['plugin_rewind_time']) : 10; |
| 432 | $settings['license_key'] = 1; // backward compatibility |
| 433 | // Pro will handle g_loading_animation settings and other |
| 434 | $settings = apply_filters( 'ep_wistia_settings_before_save', $settings); |
| 435 | update_option( $option_name, $settings); |
| 436 | do_action( 'ep_wistia_settings_after_save', $settings); |
| 437 | } |
| 438 | |
| 439 | public function save_vimeo_settings() { |
| 440 | $option_name = EMBEDPRESS_PLG_NAME.':vimeo'; |
| 441 | $settings = get_option( $option_name, []); |
| 442 | $settings['start_time'] = isset( $_POST['start_time']) ? sanitize_text_field( $_POST['start_time']) : 0; |
| 443 | $settings['autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : ''; |
| 444 | $settings['color'] = isset( $_POST['color']) ? sanitize_text_field( $_POST['color']) : '#00adef'; |
| 445 | $settings['display_title'] = isset( $_POST['display_title']) ? sanitize_text_field( $_POST['display_title']) : 1; |
| 446 | $settings['display_author'] = isset( $_POST['display_author']) ? sanitize_text_field( $_POST['display_author']) : 1; |
| 447 | $settings['display_avatar'] = isset( $_POST['display_avatar']) ? sanitize_text_field( $_POST['display_avatar']) : 1; |
| 448 | $settings['license_key'] = 1; // backward compatibility |
| 449 | // Pro will handle g_loading_animation settings and other |
| 450 | $settings = apply_filters( 'ep_vimeo_settings_before_save', $settings); |
| 451 | update_option( $option_name, $settings); |
| 452 | do_action( 'ep_vimeo_settings_after_save', $settings); |
| 453 | } |
| 454 | |
| 455 | public function save_twitch_settings() { |
| 456 | $option_name = EMBEDPRESS_PLG_NAME.':twitch'; |
| 457 | $settings = get_option( $option_name, []); |
| 458 | $settings['embedpress_pro_twitch_autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : 'no'; |
| 459 | $settings['embedpress_pro_fs'] = isset( $_POST['fs']) ? sanitize_text_field( $_POST['fs']) : 'yes'; |
| 460 | $settings['start_time'] = isset( $_POST['start_time']) ? sanitize_text_field( $_POST['start_time']) : 0; |
| 461 | $settings['embedpress_pro_twitch_theme'] = isset( $_POST['theme']) ? sanitize_text_field( $_POST['theme']) : 'dark'; |
| 462 | $settings['embedpress_pro_twitch_mute'] = isset( $_POST['mute']) ? sanitize_text_field( $_POST['mute']) : 'yes'; |
| 463 | $settings['license_key'] = 1; // backward compatibility |
| 464 | // Pro will handle g_loading_animation settings and other |
| 465 | $settings = apply_filters( 'ep_twitch_settings_before_save', $settings); |
| 466 | update_option( $option_name, $settings); |
| 467 | do_action( 'ep_twitch_settings_after_save', $settings); |
| 468 | } |
| 469 | |
| 470 | public function save_spotify_settings() { |
| 471 | $option_name = EMBEDPRESS_PLG_NAME.':spotify'; |
| 472 | $settings = get_option( $option_name, []); |
| 473 | $settings['theme'] = isset( $_POST['spotify_theme']) ? sanitize_text_field( $_POST['spotify_theme']) : '1'; |
| 474 | $settings['license_key'] = 1; // backward compatibility |
| 475 | |
| 476 | $settings = apply_filters( 'ep_spotify_settings_before_save', $settings); |
| 477 | update_option( $option_name, $settings); |
| 478 | do_action( 'ep_spotify_settings_after_save', $settings); |
| 479 | return $settings; |
| 480 | } |
| 481 | |
| 482 | public function save_custom_logo_settings() { |
| 483 | do_action( 'before_embedpress_branding_save'); |
| 484 | $settings = (array) get_option( EMBEDPRESS_PLG_NAME, []); |
| 485 | $settings['embedpress_document_powered_by'] = isset( $_POST['embedpress_document_powered_by']) ? sanitize_text_field( $_POST['embedpress_document_powered_by']) : 'no'; |
| 486 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 487 | do_action( 'after_embedpress_branding_save'); |
| 488 | |
| 489 | } |
| 490 | |
| 491 | public function save_dailymotion_settings() { |
| 492 | $option_name = EMBEDPRESS_PLG_NAME.':dailymotion'; |
| 493 | $settings = get_option( $option_name, []); |
| 494 | $settings['start_time'] = isset( $_POST['start_time']) ? sanitize_text_field( $_POST['start_time']) : 0; |
| 495 | $settings['visual'] = isset( $_POST['visual']) ? sanitize_text_field( $_POST['visual']) : ''; |
| 496 | $settings['autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : ''; |
| 497 | $settings['play_on_mobile'] = isset( $_POST['play_on_mobile']) ? sanitize_text_field( $_POST['play_on_mobile']) : ''; |
| 498 | $settings['color'] = isset( $_POST['color']) ? sanitize_text_field( $_POST['color']) : '#dd3333'; |
| 499 | $settings['controls'] = isset( $_POST['controls']) ? sanitize_text_field( $_POST['controls']) : ''; |
| 500 | $settings['video_info'] = isset( $_POST['video_info']) ? sanitize_text_field( $_POST['video_info']) : ''; |
| 501 | $settings['mute'] = isset( $_POST['mute']) ? sanitize_text_field( $_POST['mute']) : ''; |
| 502 | // Pro will handle g_loading_animation settings and other |
| 503 | $settings = apply_filters( 'ep_dailymotion_settings_before_save', $settings); |
| 504 | update_option( $option_name, $settings); |
| 505 | do_action( 'ep_dailymotion_settings_after_save', $settings); |
| 506 | } |
| 507 | |
| 508 | public function save_soundcloud_settings() { |
| 509 | $option_name = EMBEDPRESS_PLG_NAME.':soundcloud'; |
| 510 | $settings = get_option( $option_name, []); |
| 511 | $settings['visual'] = isset( $_POST['visual']) ? sanitize_text_field( $_POST['visual']) : ''; |
| 512 | $settings['autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : ''; |
| 513 | $settings['play_on_mobile'] = isset( $_POST['play_on_mobile']) ? sanitize_text_field( $_POST['play_on_mobile']) : ''; |
| 514 | $settings['share_button'] = isset( $_POST['share_button']) ? sanitize_text_field( $_POST['share_button']) : ''; |
| 515 | $settings['comments'] = isset( $_POST['comments']) ? sanitize_text_field( $_POST['comments']) : ''; |
| 516 | $settings['color'] = isset( $_POST['color']) ? sanitize_text_field( $_POST['color']) : ''; |
| 517 | $settings['artwork'] = isset( $_POST['artwork']) ? sanitize_text_field( $_POST['artwork']) : ''; |
| 518 | $settings['play_count'] = isset( $_POST['play_count']) ? sanitize_text_field( $_POST['play_count']) : ''; |
| 519 | $settings['username'] = isset( $_POST['username']) ? sanitize_text_field( $_POST['username']) : ''; |
| 520 | |
| 521 | $settings['license_key'] = 1; // backward compatibility |
| 522 | // Pro will handle g_loading_animation settings and other |
| 523 | $settings = apply_filters( 'ep_soundcloud_settings_before_save', $settings); |
| 524 | update_option( $option_name, $settings); |
| 525 | do_action( 'ep_soundcloud_settings_after_save', $settings); |
| 526 | } |
| 527 | |
| 528 | public function save_gcalendar_settings() { |
| 529 | $client_secret = !empty( $_POST['epgc_client_secret']) ? json_decode( wp_unslash( trim( $_POST['epgc_client_secret'])), true) : []; |
| 530 | $epgc_cache_time = !empty( $_POST['epgc_cache_time'] ) ? absint( $_POST['epgc_cache_time']) : 0; |
| 531 | $epgc_selected_calendar_ids = !empty( $_POST['epgc_selected_calendar_ids'] ) ? array_map( 'sanitize_text_field', $_POST['epgc_selected_calendar_ids']) : []; |
| 532 | |
| 533 | |
| 534 | $pretty_client_secret = ''; |
| 535 | if ( !empty( $client_secret) ) { |
| 536 | $pretty_client_secret = $this->get_pretty_json_string( $client_secret); |
| 537 | } |
| 538 | |
| 539 | update_option( 'epgc_client_secret', $pretty_client_secret); |
| 540 | update_option( 'epgc_cache_time', $epgc_cache_time); |
| 541 | update_option( 'epgc_selected_calendar_ids', $epgc_selected_calendar_ids); |
| 542 | |
| 543 | } |
| 544 | |
| 545 | public function save_opensea_settings() { |
| 546 | $option_name = EMBEDPRESS_PLG_NAME.':opensea'; |
| 547 | $settings = get_option( $option_name, []); |
| 548 | $settings['api_key'] = isset( $_POST['api_key']) ? sanitize_text_field( $_POST['api_key']) : 0; |
| 549 | $settings['limit'] = isset( $_POST['limit']) ? sanitize_text_field( $_POST['limit']) : 0; |
| 550 | $settings['orderby'] = isset( $_POST['orderby']) ? sanitize_text_field( $_POST['orderby']) : 0; |
| 551 | |
| 552 | $settings['license_key'] = 1; // backward compatibility |
| 553 | |
| 554 | // Pro will handle g_loading_animation settings and other |
| 555 | $settings = apply_filters( 'ep_opensea_settings_before_save', $settings); |
| 556 | update_option( $option_name, $settings); |
| 557 | do_action( 'ep_opensea_settings_after_save', $settings); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | function get_pretty_json_string($array) { |
| 562 | return str_replace(" ", " ", json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * AJAX handler for saving global brand image |
| 567 | */ |
| 568 | public function save_global_brand_image() { |
| 569 | // Verify nonce for security |
| 570 | if (!wp_verify_nonce($_POST['nonce'], 'embedpress_ajax_nonce')) { |
| 571 | wp_die('Security check failed'); |
| 572 | } |
| 573 | |
| 574 | // Check user capabilities |
| 575 | if (!current_user_can('manage_options')) { |
| 576 | wp_die('Insufficient permissions'); |
| 577 | } |
| 578 | |
| 579 | $logo_url = isset($_POST['logo_url']) ? esc_url_raw($_POST['logo_url']) : ''; |
| 580 | $logo_id = isset($_POST['logo_id']) ? intval($_POST['logo_id']) : ''; |
| 581 | |
| 582 | // Save global brand settings |
| 583 | $global_brand_settings = [ |
| 584 | 'logo_url' => $logo_url, |
| 585 | 'logo_id' => $logo_id, |
| 586 | ]; |
| 587 | |
| 588 | $updated = update_option(EMBEDPRESS_PLG_NAME . ':global_brand', $global_brand_settings); |
| 589 | |
| 590 | // If global brand image is being set, auto-enable branding for providers without custom logos |
| 591 | if (!empty($logo_url)) { |
| 592 | $this->auto_enable_global_branding($logo_url, $logo_id); |
| 593 | } |
| 594 | |
| 595 | if ($updated !== false) { |
| 596 | wp_send_json_success([ |
| 597 | 'message' => 'Global brand image saved successfully', |
| 598 | 'logo_url' => $logo_url, |
| 599 | 'logo_id' => $logo_id |
| 600 | ]); |
| 601 | } else { |
| 602 | wp_send_json_error('Failed to save global brand image'); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * AJAX handler for dismissing UI elements (banners, popups, etc.) |
| 608 | */ |
| 609 | public function dismiss_element() { |
| 610 | // Verify nonce for security |
| 611 | if (!wp_verify_nonce($_POST['nonce'], 'embedpress_ajax_nonce')) { |
| 612 | wp_die('Security check failed'); |
| 613 | } |
| 614 | |
| 615 | // Check user capabilities |
| 616 | if (!current_user_can('manage_options')) { |
| 617 | wp_die('Insufficient permissions'); |
| 618 | } |
| 619 | |
| 620 | $element_type = isset($_POST['element_type']) ? sanitize_text_field($_POST['element_type']) : ''; |
| 621 | |
| 622 | // Define valid dismiss types and their corresponding option names |
| 623 | $valid_dismiss_types = [ |
| 624 | 'main_banner' => 'embedpress_main_banner_dismissed', |
| 625 | 'hub_popup' => 'embedpress_hub_popup_dismissed', |
| 626 | 'popup_banner' => 'embedpress_popup_dismissed', // Legacy support |
| 627 | ]; |
| 628 | |
| 629 | if (array_key_exists($element_type, $valid_dismiss_types)) { |
| 630 | $option_name = $valid_dismiss_types[$element_type]; |
| 631 | update_option($option_name, true); |
| 632 | |
| 633 | wp_send_json_success([ |
| 634 | 'message' => ucfirst(str_replace('_', ' ', $element_type)) . ' dismissed successfully', |
| 635 | 'element_type' => $element_type, |
| 636 | 'option_name' => $option_name |
| 637 | ]); |
| 638 | } |
| 639 | |
| 640 | wp_send_json_error([ |
| 641 | 'message' => 'Invalid element type: ' . $element_type, |
| 642 | 'valid_types' => array_keys($valid_dismiss_types) |
| 643 | ]); |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Auto-enable branding for all providers that don't have custom logos |
| 648 | */ |
| 649 | private function auto_enable_global_branding($logo_url, $logo_id) { |
| 650 | $providers = ['youtube', 'vimeo', 'wistia', 'twitch', 'dailymotion', 'document']; |
| 651 | |
| 652 | foreach ($providers as $provider) { |
| 653 | $option_name = EMBEDPRESS_PLG_NAME . ':' . $provider; |
| 654 | $settings = get_option($option_name, []); |
| 655 | |
| 656 | // Only auto-enable if provider doesn't have a custom logo set |
| 657 | $has_custom_logo = isset($settings['logo_url']) && !empty($settings['logo_url']); |
| 658 | |
| 659 | if (!$has_custom_logo) { |
| 660 | // Enable branding but don't set logo_url/logo_id - let the template logic handle global fallback |
| 661 | $settings['branding'] = 'yes'; |
| 662 | update_option($option_name, $settings); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Reorder submenu items to put Analytics 2nd and License last |
| 669 | */ |
| 670 | public function reorder_submenu_items() { |
| 671 | global $submenu; |
| 672 | |
| 673 | // Check if our menu exists |
| 674 | if (!isset($submenu[$this->page_slug])) { |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | $license_item = null; |
| 679 | $analytics_item = null; |
| 680 | $reordered_menu = []; |
| 681 | |
| 682 | // Find and extract License and Analytics items |
| 683 | foreach ($submenu[$this->page_slug] as $item) { |
| 684 | if (isset($item[0])) { |
| 685 | if ($item[0] === __('License', 'embedpress')) { |
| 686 | $license_item = $item; |
| 687 | continue; // Skip adding to reordered menu |
| 688 | } elseif ($item[0] === __('Analytics', 'embedpress')) { |
| 689 | $analytics_item = $item; |
| 690 | continue; // Skip adding to reordered menu |
| 691 | } |
| 692 | } |
| 693 | $reordered_menu[] = $item; |
| 694 | } |
| 695 | |
| 696 | // Rebuild the menu with proper order |
| 697 | $final_menu = []; |
| 698 | |
| 699 | // Add first item (Dashboard) |
| 700 | if (!empty($reordered_menu[0])) { |
| 701 | $final_menu[] = $reordered_menu[0]; |
| 702 | } |
| 703 | |
| 704 | // Add Analytics as 2nd item if it exists |
| 705 | if ($analytics_item !== null) { |
| 706 | $final_menu[] = $analytics_item; |
| 707 | } |
| 708 | |
| 709 | // Add remaining items (except first which we already added) |
| 710 | for ($i = 1; $i < count($reordered_menu); $i++) { |
| 711 | $final_menu[] = $reordered_menu[$i]; |
| 712 | } |
| 713 | |
| 714 | // Add License at the end if it exists |
| 715 | if ($license_item !== null) { |
| 716 | $final_menu[] = $license_item; |
| 717 | } |
| 718 | |
| 719 | // Replace the submenu with reordered items |
| 720 | $submenu[$this->page_slug] = $final_menu; |
| 721 | } |
| 722 | } |
| 723 |