Example #1
0
 public function testGetCoreDefaultPackage()
 {
     $package = LocoPackage::get('', 'core');
     $this->assertEquals('core', $package->get_type());
     $this->assertEquals('Development', $package->get_name());
     $this->assertEquals('default', $package->get_domain());
     $this->assertEmpty($package->get_source_dirs());
 }
Example #2
0
 public function testLocoPluginPackage()
 {
     $package = LocoPackage::get('loco-translate/loco.php', 'plugin');
     $this->assertInstanceOf('LocoPackage', $package);
     $this->assertEquals('plugin', $package->get_type());
     $this->assertEquals(Loco::NS, $package->get_domain());
     $path = $package->lang_dir();
     $this->assertTrue(is_dir($path), 'Languages directory not found, ' . $path);
     return $package;
 }
Example #3
0
/**
 * Admin ajax include that syncs PO or POT file with sources
 */
DOING_AJAX or die;
if (empty($path) || !isset($name) || empty($type)) {
    throw new Exception(Loco::__('Invalid data posted to server'), 422);
}
// path is allowed to not exist yet
if ('/' !== $path[0]) {
    $path = WP_CONTENT_DIR . '/' . $path;
}
// but package must exist so we can get POT or source
/* @var $package LocoPackage */
loco_require('loco-packages', 'loco-locales');
$package = LocoPackage::get($name, $type);
if (!$package) {
    throw new Exception(sprintf(Loco::__('Package not found called %s'), $name), 404);
}
while (true) {
    // If file we're syncing is POT, we can only sync from sources
    if (!LocoAdmin::is_pot($path)) {
        // if a POT file exists, sync from that
        $domain = LocoAdmin::resolve_file_domain($path) or $domain = $package->get_domain();
        if ($pot_path = $package->get_pot($domain)) {
            $exp = LocoAdmin::parse_po($pot_path);
            if (!$exp || 1 === count($exp) && '' === $exp[0]['source']) {
                // throw new Exception( Loco::__('POT file is empty').' - '.basename($pot_path) );
                // fall through to try source code
            } else {
                $pot = basename($pot_path);
Example #4
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);
 }
Example #5
0
 protected function get_parent()
 {
     return $this->parent ? LocoPackage::get($this->parent, 'theme') : null;
 }