exists( $tempFolderPath . $oldFileName ) ) { if ( !$wp_filesystem->is_writable( $tempFolderPath . $oldFileName ) || !$wp_filesystem->is_readable( $tempFolderPath . $oldFileName )) { return false; } } //Verify directory $uploadDir = wp_upload_dir(); if ( !$wp_filesystem->is_dir( $uploadDir[ 'basedir' ] ) || !$wp_filesystem->is_writable( $uploadDir[ 'basedir' ] ) ) { return false; } //Create new dir and files if ( $wp_filesystem->is_dir( $tempFolderPath ) ) { array_map( 'unlink', glob( $tempFolderPath . '*.' . $fileExt ) ); } else { $wp_filesystem->mkdir( $tempFolderPath ); } //Create new file (flush) or update old one if ( $flush == true ) { $wp_filesystem->put_contents( $tempFolderPath . $newFileName . '.' . $fileExt, $fileContent, 0644 ); return $newFileName . '.' . $fileExt; } else { $wp_filesystem->put_contents( $tempFolderPath . $oldFileName, $fileContent, 0644 ); return $oldFileName; } } /*************************************************************** * Save scripts and styles with new random selectors after saving Titan Options ***************************************************************/ function an_save_setting_random_selectors() { //Restart cookie on every options save. if ( isset( $_COOKIE[AN_COOKIE] ) ){ setcookie( AN_COOKIE, null, -1, '/' ); } $an_option = unserialize( get_option( 'adblocker_notify_options' ) ); $anScripts = unserialize( get_option( 'adblocker_notify_selectors' ) ); if( $an_option[ 'an_option_selectors' ] == true ) { //Define new temp path $uploadDir = wp_upload_dir(); $tempDirName = an_random_slug(); $tempFolderPath = trailingslashit( $uploadDir[ 'basedir' ] ) . $tempDirName . '/'; $tempFolderURL = trailingslashit( $uploadDir[ 'baseurl' ] ) . $tempDirName . '/'; //Retrieve old files infos if( !isset( $anScripts[ 'files' ][ 'css' ] ) ) $anScripts[ 'files' ][ 'css' ] = ''; if( !isset( $anScripts[ 'files' ][ 'js' ] ) ) $anScripts[ 'files' ][ 'js' ] = ''; if( !isset( $anScripts[ 'selectors' ] ) ) $anScripts[ 'selectors' ] = ''; if( !isset( $anScripts[ 'temp-path' ] ) ) $anScripts[ 'temp-path' ] = false; //Define new selectors $newSelectors = array( an_random_slug(), an_random_slug(), an_random_slug() ); $flush = false; if( $an_option[ 'an_option_flush' ] == true || !file_exists( $anScripts[ 'temp-path' ] ) || $anScripts[ 'temp-path' ] == false ) $flush = true; //Keep old directory name and selectors if no flushed if ( $flush == false ){ $newSelectors = $anScripts[ 'selectors' ]; if ( isset( $anScripts[ 'temp-path' ] ) && $anScripts[ 'temp-path' ] != false ){ $tempFolderPath = $anScripts[ 'temp-path' ]; $tempFolderURL = $anScripts[ 'temp-url' ]; } } else { // Or remove it before new files creation if ( isset( $anScripts[ 'temp-path' ] ) && $anScripts[ 'temp-path' ] != false ){ an_delete_temp_folder( $anScripts[ 'temp-path' ] ); } } //Generate new css and js files $titanCssContent = an_update_titan_css_selectors( $an_option ); $newCSS = an_change_files_css_selectors( $flush, $tempFolderPath, $tempFolderURL, AN_URL . 'css/an-style.min.css', $anScripts[ 'files' ][ 'css' ], an_random_slug(), $anScripts[ 'selectors' ], $newSelectors, $titanCssContent ); $newJS = an_change_files_css_selectors( $flush, $tempFolderPath, $tempFolderURL, AN_URL . 'js/an-scripts.min.js', $anScripts[ 'files' ][ 'js' ], an_random_slug(), $anScripts[ 'selectors' ], $newSelectors ); //Upload dir and temp dir are not writable if ( $newCSS == false || $newJS == false ) { $tempFolderPath = false; } //Store data $newFiles = array( 'temp-path' => $tempFolderPath, 'temp-url' => $tempFolderURL, 'files' => array( 'css' => $newCSS, 'js' => $newJS ), 'selectors' => $newSelectors ); update_option( 'adblocker_notify_selectors', serialize( $newFiles ) ); //remove option flush $an_option[ 'an_option_flush' ] = false; update_option( 'adblocker_notify_options', serialize( $an_option ) ); } else { // Remove temp files if ( isset( $anScripts[ 'temp-path' ] ) ){ an_delete_temp_folder( $anScripts[ 'temp-path' ] ); } } } add_action( 'tf_admin_options_saved_adblocker_notify', 'an_save_setting_random_selectors', 99 ); /*************************************************************** * Admin Panel notice if wrong CHMOD on "wp-content/uploads" ***************************************************************/ function an_error_admin_notices() { $screen = get_current_screen(); if ( $screen->id != 'toplevel_page_' . AN_ID ) return; $anScripts = unserialize( get_option( 'adblocker_notify_selectors' ) ); if ( !empty( $anScripts ) && $anScripts[ 'temp-path' ] == false ) { echo '

' . __( 'WARNING: There was an error creating Adblock Notify CSS and JS files. Upload directory is not writable. Please CHMOD "wp-content/uploads" to 0664 and verify your server settings', 'an-translate' ) . '      [ Changing File Permissions ]

' . __( 'Don\'t worry, we thought about it. Adblock Notify will print the scripts directly in your DOM, but for performance purpose it is recommended to change your uploads directory CHMOD.', 'an-translate' ) . '

'; } } add_action( 'admin_notices', 'an_error_admin_notices' ); /*************************************************************** * Edit Titan Generated CSS ***************************************************************/ function an_update_titan_css_selectors( $an_option ) { $tfStyle = ''; if( isset( $an_option[ 'an_alternative_custom_css' ] ) ){ $tfStyle .= $an_option[ 'an_alternative_custom_css' ]; } if( isset( $an_option[ 'an_option_modal_custom_css' ] ) ){ $tfStyle .= $an_option[ 'an_option_modal_custom_css' ]; } //Remove TitanFramework Generated Style $uploadDir = wp_upload_dir(); $TfCssFile = trailingslashit( $uploadDir[ 'basedir' ] ) . 'titan-framework-adblocker_notify-css.css'; if( file_exists( $TfCssFile ) ) unlink( $TfCssFile ); return $tfStyle; } /*************************************************************** * Print Style & Sripts if temp dir. is not writable LOW PERF ***************************************************************/ function an_print_change_files_css_selectors( $an_option, $anScripts ) { //Get AN style and script $anCSS = AN_URL . 'css/an-style.min.css'; $anJS = AN_URL . 'js/an-scripts.min.js'; $newSelectors = $anScripts[ 'selectors' ]; $defaultSelectors = array( 'an-Modal', 'reveal-modal', 'an-alternative' ); $tfStyle = ''; $tfStyle .= $an_option->getOption( 'an_alternative_custom_css' ); $tfStyle .= $an_option->getOption( 'an_option_modal_custom_css' ); $anCSSFileContent = wp_remote_get( $anCSS ); $anCSSFileContent = wp_remote_retrieve_body( $anCSSFileContent ); $anCSSFileContent = str_replace( $defaultSelectors, $newSelectors, $anCSSFileContent . $tfStyle ); $anJSFileContent = wp_remote_get( $anJS ); $anJSFileContent = wp_remote_retrieve_body( $anJSFileContent ); $anJSFileContent = str_replace( $defaultSelectors, $newSelectors, $anJSFileContent ); return ' '; }