|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Update plugin translations |
| 5 | + * |
| 6 | + * @package wordpress/secure-custom-fields |
| 7 | + */ |
| 8 | + |
| 9 | +namespace WordPress\SCF\Scripts; |
| 10 | + |
| 11 | +// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions |
| 12 | +// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 13 | +// phpcs:disable WordPress.WP.AlternativeFunctions |
| 14 | + |
| 15 | +/** |
| 16 | + * Handles the translation update process for Secure Custom Fields. |
| 17 | + */ |
| 18 | +class Translation_Updater { |
| 19 | + /** |
| 20 | + * Plugin slug |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + private $plugin_slug = 'secure-custom-fields'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Language directory |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + private $lang_dir; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constructor |
| 35 | + */ |
| 36 | + public function __construct() { |
| 37 | + $this->lang_dir = __DIR__ . '/../lang'; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Run the translation update |
| 42 | + */ |
| 43 | + public function run() { |
| 44 | + if ( ! is_dir( $this->lang_dir ) ) { |
| 45 | + echo "Language directory not found: {$this->lang_dir}\n"; |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // Get available translations from WordPress.org |
| 50 | + $translations = $this->get_available_translations(); |
| 51 | + if ( empty( $translations ) ) { |
| 52 | + echo "No translations available from WordPress.org\n"; |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Get existing translation files |
| 57 | + $pattern = $this->lang_dir . '/' . $this->plugin_slug . '-*.po'; |
| 58 | + $po_files = glob( $pattern ); |
| 59 | + |
| 60 | + if ( empty( $po_files ) ) { |
| 61 | + echo "No translation files found in {$this->lang_dir}\n"; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + foreach ( $po_files as $po_file ) { |
| 66 | + $basename = basename( $po_file ); |
| 67 | + $locale = str_replace( array( $this->plugin_slug . '-', '.po' ), '', $basename ); |
| 68 | + |
| 69 | + echo "→ Checking $locale...\n"; |
| 70 | + |
| 71 | + $po_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.po'; |
| 72 | + $mo_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.mo'; |
| 73 | + $l10n_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.l10n.php'; |
| 74 | + |
| 75 | + // If translation is in WordPress.org list, delete local files |
| 76 | + if ( isset( $translations[ $locale ] ) ) { |
| 77 | + if ( file_exists( $po_path ) ) { |
| 78 | + unlink( $po_path ); |
| 79 | + echo " ✓ Deleted .po file\n"; |
| 80 | + } |
| 81 | + if ( file_exists( $mo_path ) ) { |
| 82 | + unlink( $mo_path ); |
| 83 | + echo " ✓ Deleted .mo file\n"; |
| 84 | + } |
| 85 | + if ( file_exists( $l10n_path ) ) { |
| 86 | + unlink( $l10n_path ); |
| 87 | + echo " ✓ Deleted .l10n.php file\n"; |
| 88 | + } |
| 89 | + echo " ✓ Translation available on WordPress.org\n\n"; |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + echo " ⚠️ Translation not available on WordPress.org\n"; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get available translations from WordPress.org |
| 99 | + * |
| 100 | + * @return array |
| 101 | + */ |
| 102 | + private function get_available_translations() { |
| 103 | + $api_url = sprintf( |
| 104 | + 'https://api.wordpress.org/translations/plugins/1.0/?slug=%s', |
| 105 | + $this->plugin_slug |
| 106 | + ); |
| 107 | + |
| 108 | + $response = file_get_contents( $api_url ); |
| 109 | + if ( false === $response ) { |
| 110 | + return array(); |
| 111 | + } |
| 112 | + |
| 113 | + $data = json_decode( $response, true ); |
| 114 | + if ( ! isset( $data['translations'] ) ) { |
| 115 | + return array(); |
| 116 | + } |
| 117 | + |
| 118 | + $translations = array(); |
| 119 | + foreach ( $data['translations'] as $translation ) { |
| 120 | + $translations[ $translation['language'] ] = array( |
| 121 | + 'package' => $translation['package'], |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + return $translations; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Run the script |
| 130 | +$updater = new Translation_Updater(); |
| 131 | +$updater->run(); |
0 commit comments