translate-file.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ElementsKit_Lite\Compatibility\Data_Migration; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | class Translate_File { |
| 8 | |
| 9 | use \ElementsKit_Lite\Traits\Singleton; |
| 10 | |
| 11 | private $OK_Translate_File = 'ekit_translate_file_checked'; |
| 12 | |
| 13 | public static function load_filesystem() { |
| 14 | |
| 15 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 16 | |
| 17 | WP_Filesystem(); |
| 18 | } |
| 19 | |
| 20 | public function init() { |
| 21 | |
| 22 | $option = get_option( $this->OK_Translate_File, 'no' ); |
| 23 | |
| 24 | if ( $option == 'no' ) { |
| 25 | |
| 26 | self::load_filesystem(); |
| 27 | |
| 28 | global $wp_filesystem; |
| 29 | |
| 30 | $the_dir = WP_LANG_DIR . '/plugins/'; |
| 31 | $files = $wp_filesystem->dirlist( $the_dir ); |
| 32 | |
| 33 | $o_dom = 'elementskit-'; |
| 34 | $n_dom = 'elementskit-lite-'; |
| 35 | |
| 36 | $file_pattern = 'elementskit-'; |
| 37 | $ln = strlen( $file_pattern ); |
| 38 | $invalid_pattern = '/elementskit\-.+\-.+/'; |
| 39 | |
| 40 | if ( ! empty( $files ) ) { |
| 41 | foreach ( $files as $file ) { |
| 42 | |
| 43 | $nm = $file['name']; |
| 44 | |
| 45 | $dbg['cont']['pahase0'][] = 9; |
| 46 | |
| 47 | /** |
| 48 | * Checking if file name is started with elementskit- |
| 49 | */ |
| 50 | if ( substr( $nm, 0, $ln ) == $file_pattern ) { |
| 51 | |
| 52 | /** |
| 53 | * Checking if file name is like this pattern elementskit-blabla- |
| 54 | * If so we will not process it |
| 55 | * |
| 56 | */ |
| 57 | if ( preg_match( $invalid_pattern, $nm ) ) { |
| 58 | |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Preparing a new name for the file |
| 64 | * and copying it i the same directory |
| 65 | * |
| 66 | */ |
| 67 | $new_name = str_replace( $o_dom, $n_dom, $nm ); |
| 68 | $wp_filesystem->copy( $the_dir . $nm, $the_dir . $new_name, true ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * As we have copied all the files |
| 75 | * we do not want to run this every time |
| 76 | * so we are updating the flag in WordPress option |
| 77 | */ |
| 78 | |
| 79 | update_option( $this->OK_Translate_File, 'yes' ); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 |