Example #1
0
 public function testGetCorePackages()
 {
     $packages = LocoPackage::get_core_packages();
     $this->assertCount(5, $packages);
     $this->assertArrayHasKey('', $packages);
     $this->assertArrayHasKey('ms', $packages);
     $this->assertArrayHasKey('admin', $packages);
     $this->assertArrayHasKey('admin-network', $packages);
     $this->assertArrayHasKey('continents-cities', $packages);
 }
Example #2
0
 /**
  * Admin tools page render call
  */
 public static function render_page_tools()
 {
     self::check_capability();
     do {
         try {
             // libs required for all manage translation pages
             loco_require('loco-locales', 'loco-packages');
             // most actions except root listing define a single package by name and type
             $package = null;
             if (isset($_GET['name']) && isset($_GET['type'])) {
                 $package = LocoPackage::get($_GET['name'], $_GET['type']);
             }
             // Extract messages if 'xgettext' is in query string
             //
             if (isset($_GET['xgettext'])) {
                 $domain = $_GET['xgettext'];
                 if ($pot_path = $package->get_pot($domain)) {
                     throw new Exception('POT already exists at ' . $pot_path);
                 }
                 // Establish best/intended location for new POT file
                 $dir = $package->lang_dir($domain);
                 $pot_path = $dir . '/' . $domain . '.pot';
                 $export = self::xgettext($package, $dir);
                 self::render_poeditor($package, $pot_path, $export);
                 break;
             }
             // Initialize a new PO file if 'msginit' is in query string
             //
             if (isset($_GET['msginit'])) {
                 $domain = $_GET['msginit'];
                 $force_global = isset($_GET['gforce']) ? (bool) $_GET['gforce'] : null;
                 // handle PO file creation if locale is set
                 if (isset($_GET['custom-locale'])) {
                     try {
                         $locale = $_GET['custom-locale'] or $locale = $_GET['common-locale'];
                         $po_path = self::msginit($package, $domain, $locale, $export, $head, $force_global);
                         if ($po_path) {
                             self::render_poeditor($package, $po_path, $export, $head);
                             break;
                         }
                     } catch (Exception $Ex) {
                         // fall through to msginit screen with error
                         self::error($Ex->getMessage());
                     }
                 }
                 // else do a dry run to pre-empt failures and allow manual alteration of target path
                 $path = self::msginit($package, $domain, 'zz_ZZ', $export, $head, $force_global);
                 // get alternative location options
                 $pdir = $package->lang_dir($domain, true);
                 $gdir = $package->global_lang_dir();
                 $pdir_ok = is_writeable($pdir);
                 $gdir_ok = is_writeable($gdir);
                 $is_global = $package->is_global_path($path);
                 // warn about unwriteable locations?
                 // render msginit start screen
                 $title = Loco::__('New PO file');
                 $locales = LocoLocale::get_names();
                 Loco::enqueue_scripts('build/admin-common', 'build/admin-poinit');
                 Loco::render('admin-poinit', compact('package', 'domain', 'title', 'locales', 'path', 'pdir', 'gdir', 'pdir_ok', 'gdir_ok', 'is_global'));
                 break;
             }
             // Render existing file in editor if 'poedit' contains a valid file path relative to content directory
             //
             if (isset($_GET['poedit']) && ($po_path = self::resolve_path($_GET['poedit']))) {
                 $export = self::parse_po_with_headers($po_path, $head);
                 // support incorrect usage of PO files as templates
                 if (isset($_GET['pot']) && !self::is_pot($po_path)) {
                     $po_path = dirname($po_path) . '/' . $_GET['pot'] . '.pot';
                     self::warning(sprintf(Loco::__('PO file used as template. This will be renamed to %s on first save'), basename($po_path)));
                 }
                 self::render_poeditor($package, $po_path, $export, $head);
                 break;
             }
             // Show filesystem check if 'fscheck' in query
             //
             if (isset($_GET['fscheck'])) {
                 $args = $package->meta() + compact('package');
                 Loco::enqueue_scripts('build/admin-common');
                 Loco::render('admin-fscheck', $args);
                 break;
             }
         } catch (Exception $Ex) {
             self::error($Ex->getMessage());
         }
         // default screen renders root page with available themes and plugins to translate
         // @var WP_Theme $theme
         $themes = array();
         foreach (wp_get_themes(array('allowed' => true)) as $name => $theme) {
             $package = LocoPackage::get($name, 'theme') and $name = $package->get_name();
             $themes[$name] = $package;
         }
         // @var array $plugin
         $plugins = array();
         foreach (get_plugins() as $plugin_file => $plugin) {
             $package = LocoPackage::get($plugin_file, 'plugin') and $plugins[] = $package;
         }
         // @var array $core
         $core = array();
         $conf = Loco::config();
         if (!empty($conf['enable_core'])) {
             foreach (LocoPackage::get_core_packages() as $package) {
                 // if package has no PO or POT we skip it because core packages have no source
                 if ($package->get_po() || $package->get_pot()) {
                     $core[] = $package;
                 }
             }
         }
         // order most active packges first in each set
         $args = array('themes' => LocoPackage::sort_modified($themes), 'plugins' => LocoPackage::sort_modified($plugins), 'core' => LocoPackage::sort_modified($core));
         // upgrade notice
         if ($updates = get_site_transient('update_plugins')) {
             $key = Loco::NS . '/loco.php';
             if (isset($updates->checked[$key]) && isset($updates->response[$key])) {
                 $old = $updates->checked[$key];
                 $new = $updates->response[$key]->new_version;
                 if (1 === version_compare($new, $old)) {
                     // current version is lower than latest
                     $args['update'] = $new;
                 }
             }
         }
         Loco::enqueue_scripts('build/admin-common');
         Loco::render('admin-root', $args);
     } while (false);
 }