function gp_populate_notices() { GP::$redirect_notices = array(); $prefix = '_gp_notice_'; foreach ($_COOKIE as $key => $value) { if (gp_startswith($key, $prefix) && ($suffix = substr($key, strlen($prefix)))) { GP::$redirect_notices[$suffix] = $value; } setcookie($key, '', 0, gp_url_path()); } }
function clean_up_global_scope() { parent::clean_up_global_scope(); $locales =& GP_Locales::instance(); $locales->locales = array(); $_GET = array(); $_POST = array(); /** * @todo re-initialize all thing objects */ GP::$translation_set = new GP_Translation_Set(); GP::$original = new GP_Original(); }
function clean_up_global_scope() { GP::$user->reintialize_wp_users_object(); $locales =& GP_Locales::instance(); $locales->locales = array(); $_GET = array(); $_POST = array(); $this->flush_cache(); /** * @todo re-initialize all thing objects */ GP::$translation_set = new GP_Translation_Set(); GP::$original = new GP_Original(); }
function test_read_translations() { $stubbed_originals = array(); foreach ($this->entries as $sample) { list($context, $original, $translation) = $sample; $stubbed_originals[] = new GP_Original(array('singular' => $original, 'context' => $context)); } GP::$original = $this->getMock('GP_Original', array('by_project_id')); GP::$original->expects($this->once())->method('by_project_id')->with($this->equalTo(2))->will($this->returnValue($stubbed_originals)); $translations = $this->properties->read_translations_from_file(GP_DIR_TESTDATA . '/translation.properties', (object) array('id' => 2)); foreach ($this->entries as $sample) { list($context, $original, $translation) = $sample; $this->assertEquals($translation, $translations->translate($original, $context)); } }
require_once GP_PATH . GP_INC . 'routes/_main.php'; require_once GP_PATH . GP_INC . 'routes/index.php'; require_once GP_PATH . GP_INC . 'routes/login.php'; require_once GP_PATH . GP_INC . 'routes/original.php'; require_once GP_PATH . GP_INC . 'routes/profile.php'; require_once GP_PATH . GP_INC . 'routes/project.php'; require_once GP_PATH . GP_INC . 'routes/translation-set.php'; require_once GP_PATH . GP_INC . 'routes/translation.php'; require_once GP_PATH . GP_INC . 'routes/glossary.php'; require_once GP_PATH . GP_INC . 'routes/glossary-entry.php'; require_once GP_PATH . GP_INC . 'routes/locale.php'; GP::$translation_warnings = new GP_Translation_Warnings(); GP::$builtin_translation_warnings = new GP_Builtin_Translation_Warnings(); GP::$builtin_translation_warnings->add_all(GP::$translation_warnings); GP::$router = new GP_Router(); GP::$formats = array(); require_once GP_PATH . GP_INC . 'format.php'; require_once GP_PATH . GP_INC . 'formats/format_android.php'; require_once GP_PATH . GP_INC . 'formats/format_pomo.php'; require_once GP_PATH . GP_INC . 'formats/format_resx.php'; require_once GP_PATH . GP_INC . 'formats/format_strings.php'; // Let's do it again, there are more variables added since last time we called it gp_set_globals(get_defined_vars()); require_once GP_PATH . GP_INC . 'plugin.php'; $plugins = scandir(GP_PLUGINS_PATH); foreach ($plugins as $plugin) { if (is_dir(GP_PLUGINS_PATH . '/' . $plugin)) { if (is_readable(GP_PLUGINS_PATH . "/{$plugin}/{$plugin}.php")) { require_once GP_PLUGINS_PATH . "/{$plugin}/{$plugin}.php"; } } else {
list($this->project_id, $this->locale_slug, $this->set_slug) = $this->project_id_locale_slug_set_slug($this->object_id); } $this->object_type = 'project|locale|set-slug'; $this->default_conditions = "object_type = '" . $this->object_type . "'"; } function prepare_fields_for_save($args) { $args = (array) $args; $args['object_type'] = $this->object_type; if (gp_array_get($args, 'project_id') && gp_array_get($args, 'locale_slug') && gp_array_get($args, 'set_slug') && !gp_array_get($args, 'object_id')) { $args['object_id'] = $this->object_id($args['project_id'], $args['locale_slug'], $args['set_slug']); } $args = parent::prepare_fields_for_save($args); return $args; } function project_id_locale_slug_set_slug($object_id) { return explode('|', $object_id); } function object_id($project_id, $locale_slug, $set_slug = 'default') { return implode('|', array($project_id, $locale_slug, $set_slug)); } function by_project_id($project_id) { $project_id = (int) $project_id; return $this->find_many("object_id LIKE '{$project_id}|%'"); } } GP::$validator_permission = new GP_Validator_Permission();
while (!$glossary && $project->parent_project_id) { $project = GP::$project->get($project->parent_project_id); $translation_set = GP::$translation_set->by_project_id_slug_and_locale($project->id, $slug, $locale); if ($translation_set) { $glossary = $this->by_set_id($translation_set->id); } } } return $glossary; } function by_set_id($set_id) { return $this->one("\n\t\t SELECT * FROM {$this->table}\n\t\t WHERE translation_set_id = %d LIMIT 1", $set_id); } /** * Copies glossary items from a glossary to the current one * This function does not merge then, just copies unconditionally. If a translation already exists, it will be duplicated. * * @param int $source_glossary_id * * @return mixed */ function copy_glossary_items_from($source_glossary_id) { global $wpdb; $current_date = $this->now_in_mysql_format(); return $this->query("\n\t\t\tINSERT INTO {$wpdb->gp_glossary_items} (\n\t\t\t\tid, term, type, examples, comment, suggested_translation, last_update\n\t\t\t)\n\t\t\tSELECT\n\t\t\t\t%s AS id, term, type, examples, comment, suggested_translation, %s AS last_update\n\t\t\tFROM {$wpdb->gp_glossary_items} WHERE id = %s", $this->id, $current_date, $source_glossary_id); } } GP::$glossary = new GP_Glossary();
$sql_project = $gpdb->prepare('o.project_id != %d', $this->project_id); $sql_singular = $gpdb->prepare('o.singular = BINARY %s', $this->singular); $sql_plural = is_null($this->plural) ? 'o.plural IS NULL' : $gpdb->prepare('o.plural = BINARY %s', $this->plural); $sql_context = is_null($this->context) ? 'o.context IS NULL' : $gpdb->prepare('o.context = BINARY %s', $this->context); $sql = "SELECT t.*, s.locale, s.slug\n\t\t\tFROM {$this->table} o\n\t\t\t\tJOIN {$gpdb->translations} t ON o.id = t.original_id\n\t\t\t\tJOIN {$gpdb->translation_sets} s ON t.translation_set_id = s.id\n\t\t\tWHERE\n\t\t\t\t{$sql_context} AND {$sql_singular} AND {$sql_plural}\n\t\t\t\tAND o.status = '+active' AND {$sql_project}\n\t\t\t\tAND t.status = 'current'\n\t\t\tGROUP BY t.translation_0, t.translation_1, t.translation_2, t.translation_3, t.translation_4, t.translation_5, s.locale, s.slug\n\t\t\tORDER BY t.date_modified DESC, t.id DESC"; $other_project_translations = GP::$translation->many($sql); foreach ($other_project_translations as $t) { $o_translation_set = array_filter($project_translations_sets, function ($set) use($t) { return $set->locale == $t->locale && $set->slug == $t->slug; }); if (empty($o_translation_set)) { continue; } $o_translation_set = reset($o_translation_set); if (in_array($o_translation_set->id, $matched_sets)) { // We already have a translation for this set. continue; } $matched_sets[] = $o_translation_set->id; $copy_status = apply_filters('translations_from_other_projects_status', 'current'); $t->copy_into_set($o_translation_set->id, $this->id, $copy_status); } } function after_create() { do_action('original_created', $this); return true; } } GP::$original = new GP_Original();
* @type WP_User $user The user being evaluated. * @type mixed $extra Extra information given to the permission check. * } */ $preliminary = apply_filters('gp_pre_can_user', 'no-verdict', $filter_args); if (is_bool($preliminary)) { return $preliminary; } $verdict = $this->find_one(array('action' => 'admin', 'user_id' => $user_id)) || $this->find_one($args) || $this->find_one(array_merge($args, array('object_id' => null))); /** * Filter whether an user can do an action. * * @since 1.0.0 * * @param bool $verdict Whether user can do an action. * @param array $args { * Arguments of the permission check. * * @type int $user_id The user being evaluated. * @type string $action Action to be executed. * @type string $object_type Object type to execute against. * @type string $object_id Object ID to execute against. * @type WP_User $user The user being evaluated. * @type mixed $extra Extra information given to the permission check. * } */ return apply_filters('gp_can_user', $verdict, $filter_args); } } GP::$permission = new GP_Permission();
function gp_populate_notices() { GP::$redirect_notices = array(); $prefix = '_gp_notice_'; $cookie_path = '/' . ltrim(gp_url_path(), '/'); // Make sure that the cookie path is never empty. foreach ($_COOKIE as $key => $value) { if (gp_startswith($key, $prefix) && ($suffix = substr($key, strlen($prefix)))) { GP::$redirect_notices[$suffix] = wp_unslash($value); gp_set_cookie($key, '', 0, $cookie_path); } } }
$args = compact('user_id', 'action', 'object_type', 'object_id'); $preliminary = apply_filters('pre_can_user', 'no-verdict', $args); if (is_bool($preliminary)) { return $preliminary; } $verdict = $user && $user->admin() || GP::$permission->find_one($args) || GP::$permission->find_one(array_merge($args, array('object_id' => null))); return apply_filters('can_user', $verdict, $args); } function get_meta($key) { global $wp_users_object; if (!($user = $wp_users_object->get_user($this->id))) { return; } $key = gp_sanitize_meta_key($key); if (!isset($user->{$key})) { return; } return $user->{$key}; } function set_meta($key, $value) { return gp_update_meta($this->id, $key, $value, 'user'); } function delete_meta($key) { return gp_delete_meta($this->id, $key, '', 'user'); } } GP::$user = new GP_User();
<?php class GP_Administrator_Permission extends GP_Permission { var $table_basename = 'gp_permissions'; var $field_names = array('id', 'user_id', 'action', 'object_type', 'object_id'); var $non_db_field_names = array(); var $non_updatable_attributes = array('id'); /** * Adds restrictions to the fields in the object. * * @since 1.1.0 */ function restrict_fields($permission) { $permission->user_id_should_not_be('empty'); $permission->action_should_not_be('empty'); $permission->object_type_should_be('empty'); $permission->object_id_should_be('empty'); } } GP::$administrator_permission = new GP_Administrator_Permission();
function test_edit_post_error_redirect_if_create_was_unsuccessful() { $this->set_admin_user_as_current(); GP::$translation_set = $this->getMock('GP_Translation_Set', array('update')); $_POST['set'] = $this->factory->translation_set->generate_args(); $this->route->edit_post($this->set->id); $this->assertErrorRedirect(); }
unset($cookies); } require_once GP_PATH . GP_INC . 'warnings.php'; require_once GP_PATH . GP_INC . 'validation.php'; require_once GP_PATH . GP_INC . 'thing.php'; foreach (glob(GP_PATH . GP_INC . 'things/*.php') as $thing) { require_once $thing; } require_once GP_PATH . GP_INC . 'routes.php'; foreach (glob(GP_PATH . GP_INC . 'routes/*.php') as $route) { require_once $route; } GP::$translation_warnings = new GP_Translation_Warnings(); GP::$builtin_translation_warnings = new GP_Builtin_Translation_Warnings(); GP::$builtin_translation_warnings->add_all(GP::$translation_warnings); GP::$router = new GP_Router(); // Let's do it again, there are more variables added since last time we called it gp_set_globals(get_defined_vars()); require_once GP_PATH . GP_INC . 'plugin.php'; $plugins = glob(GP_PATH . 'plugins/*.php'); if ($plugins) { foreach ($plugins as $plugin) { require_once $plugin; } } unset($plugins, $plugin); do_action('plugins_loaded'); if (defined('GP_INSTALLING') && GP_INSTALLING) { return; } else { define('GP_INSTALLING', false);
<?php class GP { // models static $project; static $user; static $translation_set; static $permission; static $translation; static $original; // other singletons static $router; static $redirect_notices = array(); static $translation_warnings; static $builtin_translation_warnings; static $current_route = null; // plugins can use this space static $vars = array(); // for plugin singletons static $plugins; } GP::$plugins = new stdClass();
function update_status_breakdown() { $counts = wp_cache_get($this->id, 'translation_set_status_breakdown'); if (!is_array($counts)) { /* * TODO: * - calculate weighted coefficient by priority to know how much of the strings are translated * - calculate untranslated */ $t = GP::$translation->table; $o = GP::$original->table; $counts = GP::$translation->many_no_map("\n\t\t\t\tSELECT t.status as translation_status, COUNT(*) as n\n\t\t\t\tFROM {$t} AS t INNER JOIN {$o} AS o ON t.original_id = o.id WHERE t.translation_set_id = %d AND o.status LIKE '+%%' GROUP BY t.status", $this->id); $warnings_count = GP::$translation->value_no_map("\n\t\t\t\tSELECT COUNT(*) FROM {$t} AS t INNER JOIN {$o} AS o ON t.original_id = o.id\n\t\t\t\tWHERE t.translation_set_id = %d AND o.status LIKE '+%%' AND (t.status = 'current' OR t.status = 'waiting') AND warnings IS NOT NULL", $this->id); $counts[] = (object) array('translation_status' => 'warnings', 'n' => $warnings_count); wp_cache_set($this->id, $counts, 'translation_set_status_breakdown'); } $statuses = GP::$translation->get_static('statuses'); $statuses[] = 'warnings'; foreach ($statuses as $status) { $this->{$status . '_count'} = 0; } $this->untranslated_count = 0; foreach ($counts as $count) { if (in_array($count->translation_status, $statuses)) { $this->{$count->translation_status . '_count'} = $count->n; } } } } GP::$translation_set = new GP_Translation_Set();
if (!empty($this->parts_of_speech)) { return; } $this->parts_of_speech = array('noun' => _x('noun', 'part-of-speech', 'glotpress'), 'verb' => _x('verb', 'part-of-speech', 'glotpress'), 'adjective' => _x('adjective', 'part-of-speech', 'glotpress'), 'adverb' => _x('adverb', 'part-of-speech', 'glotpress'), 'interjection' => _x('interjection', 'part-of-speech', 'glotpress'), 'conjunction' => _x('conjunction', 'part-of-speech', 'glotpress'), 'preposition' => _x('preposition', 'part-of-speech', 'glotpress'), 'pronoun' => _x('pronoun', 'part-of-speech', 'glotpress'), 'expression' => _x('expression', 'part-of-speech', 'glotpress')); } /** * Sets restriction rules for fields. * * @since 1.0.0 * * @param GP_Validation_Rules $rules The validation rules instance. */ public function restrict_fields($rules) { $rules->term_should_not_be('empty'); $rules->part_of_speech_should_not_be('empty'); $rules->glossary_id_should_be('positive_int'); $rules->last_edited_by_should_be('positive_int'); } public function by_glossary_id($glossary_id) { return $this->many("SELECT * FROM {$this->table} WHERE glossary_id= %d ORDER by term ASC", $glossary_id); } public function last_modified($glossary) { global $wpdb; return $wpdb->get_var($wpdb->prepare("SELECT date_modified FROM {$this->table} WHERE glossary_id = %d ORDER BY date_modified DESC LIMIT 1", $glossary->id, 'current')); } } GP::$glossary_entry = new GP_Glossary_Entry();
$translations[$i] = isset($this->{"translation_{$i}"}) ? $this->{"translation_{$i}"} : null; } return $translations; } function last_modified($translation_set) { global $gpdb; $last_modified = wp_cache_get($translation_set->id, 'translation_set_last_modified'); // Cached as "" if no translations. if ("" === $last_modified) { return false; } elseif (false !== $last_modified) { return $last_modified; } $last_modified = $gpdb->get_var($gpdb->prepare("SELECT date_modified FROM {$this->table} WHERE translation_set_id = %d AND status = %s ORDER BY date_modified DESC LIMIT 1", $translation_set->id, 'current')); wp_cache_set($translation_set->id, (string) $last_modified, 'translation_set_last_modified'); return $last_modified; } function after_create() { do_action('translation_created', $this); return true; } function after_save() { do_action('translation_saved', $this); return true; } } GP::$translation = new GP_Translation();
{ $sub_projects = $this->sub_projects(); foreach ($sub_projects as $sub) { $sub_projects = array_merge($sub_projects, $sub->inclusive_sub_projects()); } return $sub_projects; } public function duplicate_project_contents_from($source_project) { $source_sub_projects = $source_project->inclusive_sub_projects(); //Duplicate originals, translations sets and translations for the root project $this->copy_originals_from($source_project->id); $this->copy_sets_and_translations_from($source_project->id); //Keep a list of parents to preserve hierarchy $parents = array(); $parents[$source_project->id] = $this->id; //Duplicate originals, translations sets and translations for the child projects foreach ($source_sub_projects as $sub) { $copy_project = new GP_Project($sub->fields()); $copy_project->parent_project_id = $parents[$sub->parent_project_id]; $parent_project = $copy_project->get($copy_project->parent_project_id); $copy_project->path = gp_url_join($parent_project->path, $copy_project->slug); $copy = GP::$project->create($copy_project); $parents[$sub->id] = $copy->id; $copy->copy_originals_from($sub->id); $copy->copy_sets_and_translations_from($sub->id); } } } GP::$project = new GP_Project();
function test_edit_post_error_redirect_if_create_was_unsuccessful() { $this->set_admin_user_as_current(); GP::$translation_set = $this->getMockBuilder('GP_Translation_Set')->setMethods(array('update'))->getMock(); $_POST['set'] = $this->factory->translation_set->generate_args(); $_REQUEST['_gp_route_nonce'] = wp_create_nonce('edit-translation-set_' . $this->set->id); $this->route->edit_post($this->set->id); $this->assertErrorRedirect(); }