EmbedpressSettings.php
261 lines
| 1 | <?php |
| 2 | namespace EmbedPress\Ends\Back\Settings; |
| 3 | |
| 4 | class EmbedpressSettings { |
| 5 | var $page_slug = ''; |
| 6 | /** |
| 7 | * @var int|string |
| 8 | */ |
| 9 | protected $file_version; |
| 10 | |
| 11 | public function __construct($page_slug = 'embedpress') { |
| 12 | $this->page_slug = $page_slug; |
| 13 | $this->file_version = defined( 'WP_DEBUG') && WP_DEBUG ? time() : EMBEDPRESS_VERSION; |
| 14 | add_action('admin_enqueue_scripts', [$this, 'handle_scripts_and_styles']); |
| 15 | add_action('admin_menu', [$this, 'register_menu']); |
| 16 | add_action( 'init', [$this, 'save_settings']); |
| 17 | |
| 18 | // ajax |
| 19 | add_action( 'wp_ajax_embedpress_elements_action', [$this, 'update_elements_list']); |
| 20 | add_action( 'wp_ajax_embedpress_settings_action', [$this, 'save_settings']); |
| 21 | |
| 22 | // Migration |
| 23 | $option = 'embedpress_elements_updated'; // to update initially for backward compatibility |
| 24 | if ( !get_option( $option, false) ) { |
| 25 | $elements_initial_states = [ |
| 26 | 'gutenberg' => [ |
| 27 | 'google-docs-block' => 'google-docs-block', |
| 28 | 'document' => 'document', |
| 29 | 'embedpress' => 'embedpress', |
| 30 | 'google-sheets-block' => 'google-sheets-block', |
| 31 | 'google-slides-block' => 'google-slides-block', |
| 32 | 'youtube-block' => 'youtube-block', |
| 33 | 'google-forms-block' => 'google-forms-block', |
| 34 | 'google-drawings-block' => 'google-drawings-block', |
| 35 | 'google-maps-block' => 'google-maps-block', |
| 36 | 'twitch-block' => 'twitch-block', |
| 37 | 'wistia-block' => 'wistia-block', |
| 38 | 'vimeo-block' => 'vimeo-block', |
| 39 | ], |
| 40 | 'elementor' => [ |
| 41 | 'embedpress-document' => 'embedpress-document', |
| 42 | 'embedpress' => 'embedpress', |
| 43 | ] |
| 44 | ]; |
| 45 | |
| 46 | $settings = get_option( EMBEDPRESS_PLG_NAME, [] ); |
| 47 | $yt = get_option( EMBEDPRESS_PLG_NAME.':youtube' ); |
| 48 | if ( empty( $settings) && empty( $yt) ) { |
| 49 | $settings['need_first_time_redirect'] = true; |
| 50 | } |
| 51 | if ( !isset( $settings['enablePluginInAdmin']) ) { |
| 52 | $settings['enablePluginInAdmin'] = 1; |
| 53 | } |
| 54 | if ( !isset( $settings['enablePluginInFront']) ) { |
| 55 | $settings['enablePluginInFront'] = 1; |
| 56 | } |
| 57 | |
| 58 | update_option( EMBEDPRESS_PLG_NAME.":elements", $elements_initial_states); |
| 59 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 60 | update_option( $option, true); |
| 61 | } |
| 62 | add_action( 'admin_init', [$this, 'embedpress_maybe_redirect_to_settings'] ); |
| 63 | |
| 64 | |
| 65 | } |
| 66 | function embedpress_maybe_redirect_to_settings() { |
| 67 | $settings = get_option( EMBEDPRESS_PLG_NAME, [] ); |
| 68 | if ( isset( $settings['need_first_time_redirect']) && $settings['need_first_time_redirect'] ) { |
| 69 | if ( get_option( 'embedpress_activation_redirect_done' ) || wp_doing_ajax() ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | update_option( 'embedpress_activation_redirect_done', true ); |
| 75 | $settings['need_first_time_redirect'] = false; |
| 76 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 77 | |
| 78 | if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | wp_safe_redirect( admin_url('admin.php?page='.$this->page_slug) ); |
| 83 | exit; |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | public function update_elements_list() { |
| 88 | if ( !empty($_POST['_wpnonce'] && wp_verify_nonce( $_POST['_wpnonce'], 'embedpress_elements_action')) ) { |
| 89 | $option = EMBEDPRESS_PLG_NAME.":elements"; |
| 90 | $elements = (array) get_option( $option, []); |
| 91 | $settings = (array) get_option( EMBEDPRESS_PLG_NAME, []); |
| 92 | |
| 93 | $type = !empty( $_POST['element_type']) ? sanitize_text_field( $_POST['element_type']) : ''; |
| 94 | $name = !empty( $_POST['element_name']) ? sanitize_text_field( $_POST['element_name']) : ''; |
| 95 | $checked = !empty( $_POST['checked']) ? $_POST['checked'] : false; |
| 96 | if ( 'false' != $checked ) { |
| 97 | $elements[$type][$name] = $name; |
| 98 | if ( $type === 'classic' ) { |
| 99 | $settings[$name] = 1; |
| 100 | } |
| 101 | }else{ |
| 102 | if( isset( $elements[$type]) && isset( $elements[$type][$name])){ |
| 103 | unset( $elements[$type][$name]); |
| 104 | } |
| 105 | if ( $type === 'classic' ) { |
| 106 | $settings[$name] = 0; |
| 107 | } |
| 108 | } |
| 109 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 110 | update_option( $option, $elements); |
| 111 | wp_send_json_success(); |
| 112 | } |
| 113 | wp_send_json_error(); |
| 114 | } |
| 115 | |
| 116 | public function register_menu() { |
| 117 | add_menu_page( __('EmbedPress Settings', 'embedpress'), 'EmbedPress', 'manage_options', $this->page_slug, |
| 118 | [ $this, 'render_settings_page' ], null, 64 ); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | public function handle_scripts_and_styles() { |
| 123 | if ( !empty( $_REQUEST['page']) && $this->page_slug === $_REQUEST['page'] ) { |
| 124 | $this->enqueue_styles(); |
| 125 | $this->enqueue_scripts(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public function enqueue_scripts() { |
| 130 | if ( !did_action( 'wp_enqueue_media') ) { |
| 131 | wp_enqueue_media(); |
| 132 | } |
| 133 | wp_register_script( 'ep-settings-script', EMBEDPRESS_SETTINGS_ASSETS_URL.'js/settings.js', ['jquery', 'wp-color-picker' ], $this->file_version, true ); |
| 134 | wp_enqueue_script( 'ep-settings', EMBEDPRESS_URL_ASSETS . 'js/settings.js', ['jquery', 'wp-color-picker' ], $this->file_version, true ); |
| 135 | wp_localize_script( 'ep-settings-script', 'embedpressObj', array( |
| 136 | 'nonce' => wp_create_nonce('embedpress_elements_action'), |
| 137 | ) ); |
| 138 | |
| 139 | wp_enqueue_script( 'ep-settings-script'); |
| 140 | } |
| 141 | |
| 142 | public function enqueue_styles() { |
| 143 | wp_enqueue_style( 'ep-settings-style', EMBEDPRESS_SETTINGS_ASSETS_URL.'css/style.css', null, $this->file_version ); |
| 144 | wp_enqueue_style( 'ep-settings-icon-style', EMBEDPRESS_SETTINGS_ASSETS_URL.'css/icon/style.css', null, $this->file_version ); |
| 145 | wp_enqueue_style( 'wp-color-picker' ); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | public function render_settings_page( ) { |
| 150 | $page_slug = $this->page_slug; // make this available for included template |
| 151 | $template = !empty( $_GET['page_type'] ) ? sanitize_text_field( $_GET['page_type']) : 'general'; |
| 152 | $nonce_field = wp_nonce_field('ep_settings_nonce', 'ep_settings_nonce', true, false); |
| 153 | $ep_page = admin_url('admin.php?page='.$this->page_slug); |
| 154 | $gen_menu_template_names = apply_filters('ep_general_menu_tmpl_names', ['general', 'youtube', 'vimeo', 'wistia', 'twitch']); |
| 155 | $brand_menu_template_names = apply_filters('ep_brand_menu_templates', ['custom-logo', 'branding',]); |
| 156 | $pro_active = is_embedpress_pro_active(); |
| 157 | $coming_soon = "<span class='ep-coming-soon'>". esc_html__( '(Coming soon)', 'embedpress'). "</span>"; |
| 158 | $success_message = esc_html__( "Settings Updated", "embedpress" ); |
| 159 | $error_message = esc_html__( "Ops! Something went wrong.", "embedpress" ); |
| 160 | include_once EMBEDPRESS_SETTINGS_PATH . 'templates/main-template.php'; |
| 161 | } |
| 162 | |
| 163 | public function save_settings() { |
| 164 | // needs to check for ajax and return response accordingly. |
| 165 | if ( !empty( $_POST['ep_settings_nonce']) && wp_verify_nonce( $_POST['ep_settings_nonce'], 'ep_settings_nonce') ) { |
| 166 | $submit_type = !empty( $_POST['submit'] ) ? $_POST['submit'] : ''; |
| 167 | $save_handler_method = "save_{$submit_type}_settings"; |
| 168 | do_action( "before_{$save_handler_method}"); |
| 169 | do_action( "before_embedpress_settings_save"); |
| 170 | if ( method_exists( $this, $save_handler_method ) ) { |
| 171 | $this->$save_handler_method(); |
| 172 | } |
| 173 | do_action( "after_embedpress_settings_save"); |
| 174 | $return_url = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url(); |
| 175 | $return_url = add_query_arg( 'success', 1, $return_url ); |
| 176 | if ( wp_doing_ajax() ) { |
| 177 | wp_send_json_success(); |
| 178 | } |
| 179 | wp_safe_redirect( $return_url); |
| 180 | exit(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public function save_general_settings() { |
| 185 | $settings = (array) get_option( EMBEDPRESS_PLG_NAME); |
| 186 | $settings ['enableEmbedResizeWidth'] = isset( $_POST['enableEmbedResizeWidth']) ? intval( $_POST['enableEmbedResizeWidth']) : 600; |
| 187 | $settings ['enableEmbedResizeHeight'] = isset( $_POST['enableEmbedResizeHeight']) ? intval( $_POST['enableEmbedResizeHeight']) : 550; |
| 188 | |
| 189 | // Pro will handle g_loading_animation settings and other |
| 190 | $settings = apply_filters( 'ep_general_settings_before_save', $settings, $_POST); |
| 191 | |
| 192 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 193 | do_action( 'ep_general_settings_after_save', $settings, $_POST); |
| 194 | } |
| 195 | |
| 196 | public function save_youtube_settings() { |
| 197 | $option_name = EMBEDPRESS_PLG_NAME.':youtube'; |
| 198 | $settings = get_option( $option_name); |
| 199 | $settings['autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : ''; |
| 200 | $settings['controls'] = isset( $_POST['controls']) ? sanitize_text_field( $_POST['controls']) : ''; |
| 201 | $settings['fs'] = isset( $_POST['fs']) ? sanitize_text_field( $_POST['fs']) : ''; |
| 202 | $settings['iv_load_policy'] = isset( $_POST['iv_load_policy']) ? sanitize_text_field( $_POST['iv_load_policy']) : 1; |
| 203 | $settings['license_key'] = 1; // backward compatibility |
| 204 | |
| 205 | // Pro will handle g_loading_animation settings and other |
| 206 | $settings = apply_filters( 'ep_youtube_settings_before_save', $settings); |
| 207 | update_option( $option_name, $settings); |
| 208 | do_action( 'ep_youtube_settings_after_save', $settings); |
| 209 | |
| 210 | } |
| 211 | |
| 212 | public function save_wistia_settings() { |
| 213 | $option_name = EMBEDPRESS_PLG_NAME.':wistia'; |
| 214 | $settings = get_option( $option_name); |
| 215 | $settings['autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : ''; |
| 216 | $settings['display_fullscreen_button'] = isset( $_POST['display_fullscreen_button']) ? sanitize_text_field( $_POST['display_fullscreen_button']) : ''; |
| 217 | $settings['small_play_button'] = isset( $_POST['small_play_button']) ? sanitize_text_field( $_POST['small_play_button']) : ''; |
| 218 | $settings['player_color'] = isset( $_POST['player_color']) ? sanitize_text_field( $_POST['player_color']) : ''; |
| 219 | $settings['plugin_resumable'] = isset( $_POST['plugin_resumable']) ? sanitize_text_field( $_POST['plugin_resumable']) : ''; |
| 220 | $settings['plugin_focus'] = isset( $_POST['plugin_focus']) ? sanitize_text_field( $_POST['plugin_focus']) : ''; |
| 221 | $settings['license_key'] = 1; // backward compatibility |
| 222 | // Pro will handle g_loading_animation settings and other |
| 223 | $settings = apply_filters( 'ep_wistia_settings_before_save', $settings); |
| 224 | update_option( $option_name, $settings); |
| 225 | do_action( 'ep_wistia_settings_after_save', $settings); |
| 226 | } |
| 227 | |
| 228 | public function save_vimeo_settings() { |
| 229 | $option_name = EMBEDPRESS_PLG_NAME.':vimeo'; |
| 230 | $settings = get_option( $option_name); |
| 231 | $settings['autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : ''; |
| 232 | $settings['color'] = isset( $_POST['color']) ? sanitize_text_field( $_POST['color']) : '#00adef'; |
| 233 | $settings['display_title'] = isset( $_POST['display_title']) ? sanitize_text_field( $_POST['display_title']) : 1; |
| 234 | $settings['license_key'] = 1; // backward compatibility |
| 235 | // Pro will handle g_loading_animation settings and other |
| 236 | $settings = apply_filters( 'ep_vimeo_settings_before_save', $settings); |
| 237 | update_option( $option_name, $settings); |
| 238 | do_action( 'ep_vimeo_settings_after_save', $settings); |
| 239 | } |
| 240 | |
| 241 | public function save_twitch_settings() { |
| 242 | $option_name = EMBEDPRESS_PLG_NAME.':twitch'; |
| 243 | $settings = get_option( $option_name); |
| 244 | $settings['embedpress_pro_twitch_autoplay'] = isset( $_POST['autoplay']) ? sanitize_text_field( $_POST['autoplay']) : 'no'; |
| 245 | $settings['embedpress_pro_fs'] = isset( $_POST['fs']) ? sanitize_text_field( $_POST['fs']) : 'yes'; |
| 246 | $settings['license_key'] = 1; // backward compatibility |
| 247 | // Pro will handle g_loading_animation settings and other |
| 248 | $settings = apply_filters( 'ep_twitch_settings_before_save', $settings); |
| 249 | update_option( $option_name, $settings); |
| 250 | do_action( 'ep_twitch_settings_after_save', $settings); |
| 251 | } |
| 252 | |
| 253 | public function save_custom_logo_settings() { |
| 254 | do_action( 'before_embedpress_branding_save'); |
| 255 | $settings = (array) get_option( EMBEDPRESS_PLG_NAME, []); |
| 256 | $settings['embedpress_document_powered_by'] = isset( $_POST['embedpress_document_powered_by']) ? sanitize_text_field( $_POST['embedpress_document_powered_by']) : 'no'; |
| 257 | update_option( EMBEDPRESS_PLG_NAME, $settings); |
| 258 | do_action( 'after_embedpress_branding_save'); |
| 259 | |
| 260 | } |
| 261 | } |