@@ -21,110 +21,110 @@ |
||
21 | 21 | |
22 | 22 | class Actions implements HooksContract |
23 | 23 | { |
24 | - protected $about; |
|
25 | - protected $admin; |
|
26 | - protected $app; |
|
27 | - protected $blocks; |
|
28 | - protected $console; |
|
29 | - protected $editor; |
|
30 | - protected $listtable; |
|
31 | - protected $menu; |
|
32 | - protected $main; |
|
33 | - protected $notices; |
|
34 | - protected $public; |
|
35 | - protected $rebusify; |
|
36 | - protected $review; |
|
37 | - protected $router; |
|
38 | - protected $settings; |
|
39 | - protected $taxonomy; |
|
40 | - protected $translator; |
|
41 | - protected $welcome; |
|
24 | + protected $about; |
|
25 | + protected $admin; |
|
26 | + protected $app; |
|
27 | + protected $blocks; |
|
28 | + protected $console; |
|
29 | + protected $editor; |
|
30 | + protected $listtable; |
|
31 | + protected $menu; |
|
32 | + protected $main; |
|
33 | + protected $notices; |
|
34 | + protected $public; |
|
35 | + protected $rebusify; |
|
36 | + protected $review; |
|
37 | + protected $router; |
|
38 | + protected $settings; |
|
39 | + protected $taxonomy; |
|
40 | + protected $translator; |
|
41 | + protected $welcome; |
|
42 | 42 | |
43 | - public function __construct(Application $app ) { |
|
44 | - $this->app = $app; |
|
45 | - $this->admin = $app->make(AdminController::class); |
|
46 | - $this->blocks = $app->make(BlocksController::class); |
|
47 | - $this->console = $app->make(Console::class); |
|
48 | - $this->editor = $app->make(EditorController::class); |
|
49 | - $this->listtable = $app->make(ListTableController::class); |
|
50 | - $this->main = $app->make(MainController::class); |
|
51 | - $this->menu = $app->make(MenuController::class); |
|
52 | - $this->notices = $app->make(NoticeController::class); |
|
53 | - $this->public = $app->make(PublicController::class); |
|
54 | - $this->rebusify = $app->make(RebusifyController::class); |
|
55 | - $this->review = $app->make(ReviewController::class); |
|
56 | - $this->router = $app->make(Router::class); |
|
57 | - $this->settings = $app->make(SettingsController::class); |
|
58 | - $this->taxonomy = $app->make(TaxonomyController::class); |
|
59 | - $this->translator = $app->make(TranslationController::class); |
|
60 | - $this->welcome = $app->make(WelcomeController::class); |
|
61 | - } |
|
43 | + public function __construct(Application $app ) { |
|
44 | + $this->app = $app; |
|
45 | + $this->admin = $app->make(AdminController::class); |
|
46 | + $this->blocks = $app->make(BlocksController::class); |
|
47 | + $this->console = $app->make(Console::class); |
|
48 | + $this->editor = $app->make(EditorController::class); |
|
49 | + $this->listtable = $app->make(ListTableController::class); |
|
50 | + $this->main = $app->make(MainController::class); |
|
51 | + $this->menu = $app->make(MenuController::class); |
|
52 | + $this->notices = $app->make(NoticeController::class); |
|
53 | + $this->public = $app->make(PublicController::class); |
|
54 | + $this->rebusify = $app->make(RebusifyController::class); |
|
55 | + $this->review = $app->make(ReviewController::class); |
|
56 | + $this->router = $app->make(Router::class); |
|
57 | + $this->settings = $app->make(SettingsController::class); |
|
58 | + $this->taxonomy = $app->make(TaxonomyController::class); |
|
59 | + $this->translator = $app->make(TranslationController::class); |
|
60 | + $this->welcome = $app->make(WelcomeController::class); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function run() |
|
67 | - { |
|
68 | - add_action('admin_enqueue_scripts', [$this->admin, 'enqueueAssets']); |
|
69 | - add_action('admin_init', [$this->admin, 'registerTinymcePopups']); |
|
70 | - add_action('media_buttons', [$this->admin, 'renderTinymceButton'], 11); |
|
71 | - add_action('plugins_loaded', [$this->app, 'getDefaults'], 11); |
|
72 | - add_action('plugins_loaded', [$this->app, 'registerAddons']); |
|
73 | - add_action('plugins_loaded', [$this->app, 'registerLanguages']); |
|
74 | - add_action('plugins_loaded', [$this->app, 'registerReviewTypes']); |
|
75 | - add_action('upgrader_process_complete', [$this->app, 'upgraded'], 10, 2); |
|
76 | - add_action('init', [$this->blocks, 'registerAssets'], 9); |
|
77 | - add_action('init', [$this->blocks, 'registerBlocks']); |
|
78 | - add_action('admin_footer', [$this->console, 'logOnce']); |
|
79 | - add_action('wp_footer', [$this->console, 'logOnce']); |
|
80 | - add_action('add_meta_boxes_'.Application::POST_TYPE, [$this->editor, 'registerMetaBoxes']); |
|
81 | - add_action('admin_print_scripts', [$this->editor, 'removeAutosave'], 999); |
|
82 | - add_action('admin_menu', [$this->editor, 'removeMetaBoxes']); |
|
83 | - add_action('current_screen', [$this->editor, 'removePostTypeSupport']); |
|
84 | - add_action('post_submitbox_misc_actions', [$this->editor, 'renderPinnedInPublishMetaBox']); |
|
85 | - add_action('admin_head', [$this->editor, 'renderReviewFields']); |
|
86 | - add_action('admin_action_revert', [$this->editor, 'revertReview']); |
|
87 | - add_action('save_post_'.Application::POST_TYPE, [$this->editor, 'saveMetaboxes'], 10, 3); |
|
88 | - add_action('admin_action_approve', [$this->listtable, 'approve']); |
|
89 | - add_action('bulk_edit_custom_box', [$this->listtable, 'renderBulkEditFields'], 10, 2); |
|
90 | - add_action('restrict_manage_posts', [$this->listtable, 'renderColumnFilters']); |
|
91 | - add_action('manage_'.Application::POST_TYPE.'_posts_custom_column', [$this->listtable, 'renderColumnValues'], 10, 2); |
|
92 | - add_action('save_post_'.Application::POST_TYPE, [$this->listtable, 'saveBulkEditFields']); |
|
93 | - add_action('pre_get_posts', [$this->listtable, 'setQueryForColumn']); |
|
94 | - add_action('admin_action_unapprove', [$this->listtable, 'unapprove']); |
|
95 | - add_action('init', [$this->main, 'registerPostType'], 8); |
|
96 | - add_action('init', [$this->main, 'registerShortcodes']); |
|
97 | - add_action('init', [$this->main, 'registerTaxonomy']); |
|
98 | - add_action('widgets_init', [$this->main, 'registerWidgets']); |
|
99 | - add_action('admin_menu', [$this->menu, 'registerMenuCount']); |
|
100 | - add_action('admin_menu', [$this->menu, 'registerSubMenus']); |
|
101 | - add_action('admin_init', [$this->menu, 'setCustomPermissions'], 999); |
|
102 | - add_action('admin_notices', [$this->notices, 'filterAdminNotices']); |
|
103 | - add_action('wp_enqueue_scripts', [$this->public, 'enqueueAssets'], 999); |
|
104 | - add_filter('site-reviews/builder', [$this->public, 'modifyBuilder']); |
|
105 | - add_action('wp_footer', [$this->public, 'renderSchema']); |
|
106 | - add_action('site-reviews/review/created', [$this->rebusify, 'onCreated']); |
|
107 | - add_action('site-reviews/review/reverted', [$this->rebusify, 'onReverted']); |
|
108 | - add_action('site-reviews/review/saved', [$this->rebusify, 'onSaved']); |
|
109 | - add_action('updated_postmeta', [$this->rebusify, 'onUpdatedMeta'], 10, 3); |
|
110 | - add_action('set_object_terms', [$this->review, 'onAfterChangeCategory'], 10, 6); |
|
111 | - add_action('transition_post_status', [$this->review, 'onAfterChangeStatus'], 10, 3); |
|
112 | - add_action('site-reviews/review/created', [$this->review, 'onAfterCreate']); |
|
113 | - add_action('before_delete_post', [$this->review, 'onBeforeDelete']); |
|
114 | - add_action('update_postmeta', [$this->review, 'onBeforeUpdate'], 10, 4); |
|
115 | - add_action('admin_init', [$this->router, 'routeAdminPostRequest']); |
|
116 | - add_action('wp_ajax_'.Application::PREFIX.'action', [$this->router, 'routeAjaxRequest']); |
|
117 | - add_action('wp_ajax_nopriv_'.Application::PREFIX.'action', [$this->router, 'routeAjaxRequest']); |
|
118 | - add_action('init', [$this->router, 'routePublicPostRequest']); |
|
119 | - add_action('admin_init', [$this->settings, 'registerSettings']); |
|
120 | - add_action(Application::TAXONOMY.'_term_edit_form_top', [$this->taxonomy, 'disableParents']); |
|
121 | - add_action(Application::TAXONOMY.'_term_new_form_tag', [$this->taxonomy, 'disableParents']); |
|
122 | - add_action(Application::TAXONOMY.'_add_form_fields', [$this->taxonomy, 'enableParents']); |
|
123 | - add_action(Application::TAXONOMY.'_edit_form', [$this->taxonomy, 'enableParents']); |
|
124 | - add_action('restrict_manage_posts', [$this->taxonomy, 'renderTaxonomyFilter'], 9); |
|
125 | - add_action('set_object_terms', [$this->taxonomy, 'restrictTermSelection'], 9, 6); |
|
126 | - add_action('admin_enqueue_scripts', [$this->translator, 'translatePostStatusLabels']); |
|
127 | - add_action('activated_plugin', [$this->welcome, 'redirectOnActivation'], 10, 2); |
|
128 | - add_action('admin_menu', [$this->welcome, 'registerPage']); |
|
129 | - } |
|
63 | + /** |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function run() |
|
67 | + { |
|
68 | + add_action('admin_enqueue_scripts', [$this->admin, 'enqueueAssets']); |
|
69 | + add_action('admin_init', [$this->admin, 'registerTinymcePopups']); |
|
70 | + add_action('media_buttons', [$this->admin, 'renderTinymceButton'], 11); |
|
71 | + add_action('plugins_loaded', [$this->app, 'getDefaults'], 11); |
|
72 | + add_action('plugins_loaded', [$this->app, 'registerAddons']); |
|
73 | + add_action('plugins_loaded', [$this->app, 'registerLanguages']); |
|
74 | + add_action('plugins_loaded', [$this->app, 'registerReviewTypes']); |
|
75 | + add_action('upgrader_process_complete', [$this->app, 'upgraded'], 10, 2); |
|
76 | + add_action('init', [$this->blocks, 'registerAssets'], 9); |
|
77 | + add_action('init', [$this->blocks, 'registerBlocks']); |
|
78 | + add_action('admin_footer', [$this->console, 'logOnce']); |
|
79 | + add_action('wp_footer', [$this->console, 'logOnce']); |
|
80 | + add_action('add_meta_boxes_'.Application::POST_TYPE, [$this->editor, 'registerMetaBoxes']); |
|
81 | + add_action('admin_print_scripts', [$this->editor, 'removeAutosave'], 999); |
|
82 | + add_action('admin_menu', [$this->editor, 'removeMetaBoxes']); |
|
83 | + add_action('current_screen', [$this->editor, 'removePostTypeSupport']); |
|
84 | + add_action('post_submitbox_misc_actions', [$this->editor, 'renderPinnedInPublishMetaBox']); |
|
85 | + add_action('admin_head', [$this->editor, 'renderReviewFields']); |
|
86 | + add_action('admin_action_revert', [$this->editor, 'revertReview']); |
|
87 | + add_action('save_post_'.Application::POST_TYPE, [$this->editor, 'saveMetaboxes'], 10, 3); |
|
88 | + add_action('admin_action_approve', [$this->listtable, 'approve']); |
|
89 | + add_action('bulk_edit_custom_box', [$this->listtable, 'renderBulkEditFields'], 10, 2); |
|
90 | + add_action('restrict_manage_posts', [$this->listtable, 'renderColumnFilters']); |
|
91 | + add_action('manage_'.Application::POST_TYPE.'_posts_custom_column', [$this->listtable, 'renderColumnValues'], 10, 2); |
|
92 | + add_action('save_post_'.Application::POST_TYPE, [$this->listtable, 'saveBulkEditFields']); |
|
93 | + add_action('pre_get_posts', [$this->listtable, 'setQueryForColumn']); |
|
94 | + add_action('admin_action_unapprove', [$this->listtable, 'unapprove']); |
|
95 | + add_action('init', [$this->main, 'registerPostType'], 8); |
|
96 | + add_action('init', [$this->main, 'registerShortcodes']); |
|
97 | + add_action('init', [$this->main, 'registerTaxonomy']); |
|
98 | + add_action('widgets_init', [$this->main, 'registerWidgets']); |
|
99 | + add_action('admin_menu', [$this->menu, 'registerMenuCount']); |
|
100 | + add_action('admin_menu', [$this->menu, 'registerSubMenus']); |
|
101 | + add_action('admin_init', [$this->menu, 'setCustomPermissions'], 999); |
|
102 | + add_action('admin_notices', [$this->notices, 'filterAdminNotices']); |
|
103 | + add_action('wp_enqueue_scripts', [$this->public, 'enqueueAssets'], 999); |
|
104 | + add_filter('site-reviews/builder', [$this->public, 'modifyBuilder']); |
|
105 | + add_action('wp_footer', [$this->public, 'renderSchema']); |
|
106 | + add_action('site-reviews/review/created', [$this->rebusify, 'onCreated']); |
|
107 | + add_action('site-reviews/review/reverted', [$this->rebusify, 'onReverted']); |
|
108 | + add_action('site-reviews/review/saved', [$this->rebusify, 'onSaved']); |
|
109 | + add_action('updated_postmeta', [$this->rebusify, 'onUpdatedMeta'], 10, 3); |
|
110 | + add_action('set_object_terms', [$this->review, 'onAfterChangeCategory'], 10, 6); |
|
111 | + add_action('transition_post_status', [$this->review, 'onAfterChangeStatus'], 10, 3); |
|
112 | + add_action('site-reviews/review/created', [$this->review, 'onAfterCreate']); |
|
113 | + add_action('before_delete_post', [$this->review, 'onBeforeDelete']); |
|
114 | + add_action('update_postmeta', [$this->review, 'onBeforeUpdate'], 10, 4); |
|
115 | + add_action('admin_init', [$this->router, 'routeAdminPostRequest']); |
|
116 | + add_action('wp_ajax_'.Application::PREFIX.'action', [$this->router, 'routeAjaxRequest']); |
|
117 | + add_action('wp_ajax_nopriv_'.Application::PREFIX.'action', [$this->router, 'routeAjaxRequest']); |
|
118 | + add_action('init', [$this->router, 'routePublicPostRequest']); |
|
119 | + add_action('admin_init', [$this->settings, 'registerSettings']); |
|
120 | + add_action(Application::TAXONOMY.'_term_edit_form_top', [$this->taxonomy, 'disableParents']); |
|
121 | + add_action(Application::TAXONOMY.'_term_new_form_tag', [$this->taxonomy, 'disableParents']); |
|
122 | + add_action(Application::TAXONOMY.'_add_form_fields', [$this->taxonomy, 'enableParents']); |
|
123 | + add_action(Application::TAXONOMY.'_edit_form', [$this->taxonomy, 'enableParents']); |
|
124 | + add_action('restrict_manage_posts', [$this->taxonomy, 'renderTaxonomyFilter'], 9); |
|
125 | + add_action('set_object_terms', [$this->taxonomy, 'restrictTermSelection'], 9, 6); |
|
126 | + add_action('admin_enqueue_scripts', [$this->translator, 'translatePostStatusLabels']); |
|
127 | + add_action('activated_plugin', [$this->welcome, 'redirectOnActivation'], 10, 2); |
|
128 | + add_action('admin_menu', [$this->welcome, 'registerPage']); |
|
129 | + } |
|
130 | 130 | } |
@@ -11,272 +11,272 @@ |
||
11 | 11 | |
12 | 12 | class Translation |
13 | 13 | { |
14 | - const SEARCH_THRESHOLD = 3; |
|
14 | + const SEARCH_THRESHOLD = 3; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - protected $entries; |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + protected $entries; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $results; |
|
21 | + /** |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $results; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns all saved custom translations with translation context. |
|
28 | - * @return array |
|
29 | - */ |
|
30 | - public function all() |
|
31 | - { |
|
32 | - $translations = $this->translations(); |
|
33 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
34 | - array_walk($translations, function (&$entry) use ($entries) { |
|
35 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
36 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
37 | - : ''; |
|
38 | - }); |
|
39 | - return $translations; |
|
40 | - } |
|
26 | + /** |
|
27 | + * Returns all saved custom translations with translation context. |
|
28 | + * @return array |
|
29 | + */ |
|
30 | + public function all() |
|
31 | + { |
|
32 | + $translations = $this->translations(); |
|
33 | + $entries = $this->filter($translations, $this->entries())->results(); |
|
34 | + array_walk($translations, function (&$entry) use ($entries) { |
|
35 | + $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
36 | + ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
37 | + : ''; |
|
38 | + }); |
|
39 | + return $translations; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public function entries() |
|
46 | - { |
|
47 | - if (!isset($this->entries)) { |
|
48 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
49 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
50 | - $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
51 | - $this->entries = $entries; |
|
52 | - } |
|
53 | - return $this->entries; |
|
54 | - } |
|
42 | + /** |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public function entries() |
|
46 | + { |
|
47 | + if (!isset($this->entries)) { |
|
48 | + $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
49 | + $entries = $this->extractEntriesFromPotFile($potFile); |
|
50 | + $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
51 | + $this->entries = $entries; |
|
52 | + } |
|
53 | + return $this->entries; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param array|null $entriesToExclude |
|
58 | - * @param array|null $entries |
|
59 | - * @return static |
|
60 | - */ |
|
61 | - public function exclude($entriesToExclude = null, $entries = null) |
|
62 | - { |
|
63 | - return $this->filter($entriesToExclude, $entries, false); |
|
64 | - } |
|
56 | + /** |
|
57 | + * @param array|null $entriesToExclude |
|
58 | + * @param array|null $entries |
|
59 | + * @return static |
|
60 | + */ |
|
61 | + public function exclude($entriesToExclude = null, $entries = null) |
|
62 | + { |
|
63 | + return $this->filter($entriesToExclude, $entries, false); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @param string $potFile |
|
68 | - * @return array |
|
69 | - */ |
|
70 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
71 | - { |
|
72 | - try { |
|
73 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
74 | - foreach ($potEntries as $key => $entry) { |
|
75 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
76 | - } |
|
77 | - } catch (Exception $e) { |
|
78 | - glsr_log()->error($e->getMessage()); |
|
79 | - } |
|
80 | - return $entries; |
|
81 | - } |
|
66 | + /** |
|
67 | + * @param string $potFile |
|
68 | + * @return array |
|
69 | + */ |
|
70 | + public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
71 | + { |
|
72 | + try { |
|
73 | + $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
74 | + foreach ($potEntries as $key => $entry) { |
|
75 | + $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
76 | + } |
|
77 | + } catch (Exception $e) { |
|
78 | + glsr_log()->error($e->getMessage()); |
|
79 | + } |
|
80 | + return $entries; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @param array|null $filterWith |
|
85 | - * @param array|null $entries |
|
86 | - * @param bool $intersect |
|
87 | - * @return static |
|
88 | - */ |
|
89 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
90 | - { |
|
91 | - if (!is_array($entries)) { |
|
92 | - $entries = $this->results; |
|
93 | - } |
|
94 | - if (!is_array($filterWith)) { |
|
95 | - $filterWith = $this->translations(); |
|
96 | - } |
|
97 | - $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
98 | - $this->results = $intersect |
|
99 | - ? array_intersect_key($entries, $keys) |
|
100 | - : array_diff_key($entries, $keys); |
|
101 | - return $this; |
|
102 | - } |
|
83 | + /** |
|
84 | + * @param array|null $filterWith |
|
85 | + * @param array|null $entries |
|
86 | + * @param bool $intersect |
|
87 | + * @return static |
|
88 | + */ |
|
89 | + public function filter($filterWith = null, $entries = null, $intersect = true) |
|
90 | + { |
|
91 | + if (!is_array($entries)) { |
|
92 | + $entries = $this->results; |
|
93 | + } |
|
94 | + if (!is_array($filterWith)) { |
|
95 | + $filterWith = $this->translations(); |
|
96 | + } |
|
97 | + $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
98 | + $this->results = $intersect |
|
99 | + ? array_intersect_key($entries, $keys) |
|
100 | + : array_diff_key($entries, $keys); |
|
101 | + return $this; |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @param string $template |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - public function render($template, array $entry) |
|
109 | - { |
|
110 | - $data = array_combine( |
|
111 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
112 | - $entry |
|
113 | - ); |
|
114 | - $data['data.class'] = $data['data.error'] = ''; |
|
115 | - if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
116 | - $data['data.class'] = 'is-invalid'; |
|
117 | - $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
118 | - } |
|
119 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
120 | - 'context' => array_map('esc_html', $data), |
|
121 | - ]); |
|
122 | - } |
|
104 | + /** |
|
105 | + * @param string $template |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + public function render($template, array $entry) |
|
109 | + { |
|
110 | + $data = array_combine( |
|
111 | + array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
112 | + $entry |
|
113 | + ); |
|
114 | + $data['data.class'] = $data['data.error'] = ''; |
|
115 | + if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
116 | + $data['data.class'] = 'is-invalid'; |
|
117 | + $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
118 | + } |
|
119 | + return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
120 | + 'context' => array_map('esc_html', $data), |
|
121 | + ]); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * Returns a rendered string of all saved custom translations with translation context. |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - public function renderAll() |
|
129 | - { |
|
130 | - $rendered = ''; |
|
131 | - foreach ($this->all() as $index => $entry) { |
|
132 | - $entry['index'] = $index; |
|
133 | - $entry['prefix'] = OptionManager::databaseKey(); |
|
134 | - $rendered.= $this->render($entry['type'], $entry); |
|
135 | - } |
|
136 | - return $rendered; |
|
137 | - } |
|
124 | + /** |
|
125 | + * Returns a rendered string of all saved custom translations with translation context. |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + public function renderAll() |
|
129 | + { |
|
130 | + $rendered = ''; |
|
131 | + foreach ($this->all() as $index => $entry) { |
|
132 | + $entry['index'] = $index; |
|
133 | + $entry['prefix'] = OptionManager::databaseKey(); |
|
134 | + $rendered.= $this->render($entry['type'], $entry); |
|
135 | + } |
|
136 | + return $rendered; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * @param bool $resetAfterRender |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - public function renderResults($resetAfterRender = true) |
|
144 | - { |
|
145 | - $rendered = ''; |
|
146 | - foreach ($this->results as $id => $entry) { |
|
147 | - $data = [ |
|
148 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
149 | - 'id' => $id, |
|
150 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
151 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
152 | - ]; |
|
153 | - $text = !empty($data['p1']) |
|
154 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
155 | - : $data['s1']; |
|
156 | - $rendered.= $this->render('result', [ |
|
157 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
158 | - 'text' => wp_strip_all_tags($text), |
|
159 | - ]); |
|
160 | - } |
|
161 | - if ($resetAfterRender) { |
|
162 | - $this->reset(); |
|
163 | - } |
|
164 | - return $rendered; |
|
165 | - } |
|
139 | + /** |
|
140 | + * @param bool $resetAfterRender |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + public function renderResults($resetAfterRender = true) |
|
144 | + { |
|
145 | + $rendered = ''; |
|
146 | + foreach ($this->results as $id => $entry) { |
|
147 | + $data = [ |
|
148 | + 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
149 | + 'id' => $id, |
|
150 | + 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
151 | + 's1' => $this->getEntryString($entry, 'msgid'), |
|
152 | + ]; |
|
153 | + $text = !empty($data['p1']) |
|
154 | + ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
155 | + : $data['s1']; |
|
156 | + $rendered.= $this->render('result', [ |
|
157 | + 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
158 | + 'text' => wp_strip_all_tags($text), |
|
159 | + ]); |
|
160 | + } |
|
161 | + if ($resetAfterRender) { |
|
162 | + $this->reset(); |
|
163 | + } |
|
164 | + return $rendered; |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @return void |
|
169 | - */ |
|
170 | - public function reset() |
|
171 | - { |
|
172 | - $this->results = []; |
|
173 | - } |
|
167 | + /** |
|
168 | + * @return void |
|
169 | + */ |
|
170 | + public function reset() |
|
171 | + { |
|
172 | + $this->results = []; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * @return array |
|
177 | - */ |
|
178 | - public function results() |
|
179 | - { |
|
180 | - $results = $this->results; |
|
181 | - $this->reset(); |
|
182 | - return $results; |
|
183 | - } |
|
175 | + /** |
|
176 | + * @return array |
|
177 | + */ |
|
178 | + public function results() |
|
179 | + { |
|
180 | + $results = $this->results; |
|
181 | + $this->reset(); |
|
182 | + return $results; |
|
183 | + } |
|
184 | 184 | |
185 | - /** |
|
186 | - * @param string $needle |
|
187 | - * @return static |
|
188 | - */ |
|
189 | - public function search($needle = '') |
|
190 | - { |
|
191 | - $this->reset(); |
|
192 | - $needle = trim(strtolower($needle)); |
|
193 | - foreach ($this->entries() as $key => $entry) { |
|
194 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
195 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
196 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
197 | - if (in_array($needle, [$single, $plural])) { |
|
198 | - $this->results[$key] = $entry; |
|
199 | - } |
|
200 | - } elseif (Str::contains(sprintf('%s %s', $single, $plural), $needle)) { |
|
201 | - $this->results[$key] = $entry; |
|
202 | - } |
|
203 | - } |
|
204 | - return $this; |
|
205 | - } |
|
185 | + /** |
|
186 | + * @param string $needle |
|
187 | + * @return static |
|
188 | + */ |
|
189 | + public function search($needle = '') |
|
190 | + { |
|
191 | + $this->reset(); |
|
192 | + $needle = trim(strtolower($needle)); |
|
193 | + foreach ($this->entries() as $key => $entry) { |
|
194 | + $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
195 | + $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
196 | + if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
197 | + if (in_array($needle, [$single, $plural])) { |
|
198 | + $this->results[$key] = $entry; |
|
199 | + } |
|
200 | + } elseif (Str::contains(sprintf('%s %s', $single, $plural), $needle)) { |
|
201 | + $this->results[$key] = $entry; |
|
202 | + } |
|
203 | + } |
|
204 | + return $this; |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Store the translations to avoid unnecessary loops. |
|
209 | - * @return array |
|
210 | - */ |
|
211 | - public function translations() |
|
212 | - { |
|
213 | - static $translations; |
|
214 | - if (empty($translations)) { |
|
215 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
216 | - $translations = isset($settings['strings']) |
|
217 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
218 | - : []; |
|
219 | - } |
|
220 | - return $translations; |
|
221 | - } |
|
207 | + /** |
|
208 | + * Store the translations to avoid unnecessary loops. |
|
209 | + * @return array |
|
210 | + */ |
|
211 | + public function translations() |
|
212 | + { |
|
213 | + static $translations; |
|
214 | + if (empty($translations)) { |
|
215 | + $settings = glsr(OptionManager::class)->get('settings'); |
|
216 | + $translations = isset($settings['strings']) |
|
217 | + ? $this->normalizeSettings((array) $settings['strings']) |
|
218 | + : []; |
|
219 | + } |
|
220 | + return $translations; |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * @param string $key |
|
225 | - * @return string |
|
226 | - */ |
|
227 | - protected function getEntryString(array $entry, $key) |
|
228 | - { |
|
229 | - return isset($entry[$key]) |
|
230 | - ? implode('', (array) $entry[$key]) |
|
231 | - : ''; |
|
232 | - } |
|
223 | + /** |
|
224 | + * @param string $key |
|
225 | + * @return string |
|
226 | + */ |
|
227 | + protected function getEntryString(array $entry, $key) |
|
228 | + { |
|
229 | + return isset($entry[$key]) |
|
230 | + ? implode('', (array) $entry[$key]) |
|
231 | + : ''; |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * @return array |
|
236 | - */ |
|
237 | - protected function normalize(array $entries) |
|
238 | - { |
|
239 | - $keys = [ |
|
240 | - 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
241 | - ]; |
|
242 | - array_walk($entries, function (&$entry) use ($keys) { |
|
243 | - foreach ($keys as $key) { |
|
244 | - try { |
|
245 | - $entry = $this->normalizeEntryString($entry, $key); |
|
246 | - } catch (\TypeError $error) { |
|
247 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
248 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
249 | - } |
|
250 | - } |
|
251 | - }); |
|
252 | - return $entries; |
|
253 | - } |
|
234 | + /** |
|
235 | + * @return array |
|
236 | + */ |
|
237 | + protected function normalize(array $entries) |
|
238 | + { |
|
239 | + $keys = [ |
|
240 | + 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
241 | + ]; |
|
242 | + array_walk($entries, function (&$entry) use ($keys) { |
|
243 | + foreach ($keys as $key) { |
|
244 | + try { |
|
245 | + $entry = $this->normalizeEntryString($entry, $key); |
|
246 | + } catch (\TypeError $error) { |
|
247 | + glsr_log()->once('error', 'Translation/normalize', $error); |
|
248 | + glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
249 | + } |
|
250 | + } |
|
251 | + }); |
|
252 | + return $entries; |
|
253 | + } |
|
254 | 254 | |
255 | - /** |
|
256 | - * @param string $key |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - protected function normalizeEntryString(array $entry, $key) |
|
260 | - { |
|
261 | - if (isset($entry[$key])) { |
|
262 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
263 | - } |
|
264 | - return $entry; |
|
265 | - } |
|
255 | + /** |
|
256 | + * @param string $key |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + protected function normalizeEntryString(array $entry, $key) |
|
260 | + { |
|
261 | + if (isset($entry[$key])) { |
|
262 | + $entry[$key] = $this->getEntryString($entry, $key); |
|
263 | + } |
|
264 | + return $entry; |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * @return array |
|
269 | - */ |
|
270 | - protected function normalizeSettings(array $strings) |
|
271 | - { |
|
272 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
273 | - $strings = array_filter($strings, 'is_array'); |
|
274 | - foreach ($strings as &$string) { |
|
275 | - $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
276 | - $string = wp_parse_args($string, $defaultString); |
|
277 | - } |
|
278 | - return array_filter($strings, function ($string) { |
|
279 | - return !empty($string['id']); |
|
280 | - }); |
|
281 | - } |
|
267 | + /** |
|
268 | + * @return array |
|
269 | + */ |
|
270 | + protected function normalizeSettings(array $strings) |
|
271 | + { |
|
272 | + $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
273 | + $strings = array_filter($strings, 'is_array'); |
|
274 | + foreach ($strings as &$string) { |
|
275 | + $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
276 | + $string = wp_parse_args($string, $defaultString); |
|
277 | + } |
|
278 | + return array_filter($strings, function ($string) { |
|
279 | + return !empty($string['id']); |
|
280 | + }); |
|
281 | + } |
|
282 | 282 | } |
@@ -6,49 +6,49 @@ |
||
6 | 6 | |
7 | 7 | class Date |
8 | 8 | { |
9 | - /** |
|
10 | - * [60, 1], |
|
11 | - * [60 * 100, 60], |
|
12 | - * [3600 * 70, 3600], |
|
13 | - * [3600 * 24 * 10, 3600 * 24], |
|
14 | - * [3600 * 24 * 30, 3600 * 24 * 7], |
|
15 | - * [3600 * 24 * 30 * 30, 3600 * 24 * 30], |
|
16 | - * [INF, 3600 * 24 * 265],. |
|
17 | - */ |
|
18 | - protected static $TIME_PERIODS = [ |
|
19 | - [60, 1], |
|
20 | - [6000, 60], |
|
21 | - [252000, 3600], |
|
22 | - [864000, 86400], |
|
23 | - [2592000, 604800], |
|
24 | - [77760000, 2592000], |
|
25 | - [INF, 22896000], |
|
26 | - ]; |
|
9 | + /** |
|
10 | + * [60, 1], |
|
11 | + * [60 * 100, 60], |
|
12 | + * [3600 * 70, 3600], |
|
13 | + * [3600 * 24 * 10, 3600 * 24], |
|
14 | + * [3600 * 24 * 30, 3600 * 24 * 7], |
|
15 | + * [3600 * 24 * 30 * 30, 3600 * 24 * 30], |
|
16 | + * [INF, 3600 * 24 * 265],. |
|
17 | + */ |
|
18 | + protected static $TIME_PERIODS = [ |
|
19 | + [60, 1], |
|
20 | + [6000, 60], |
|
21 | + [252000, 3600], |
|
22 | + [864000, 86400], |
|
23 | + [2592000, 604800], |
|
24 | + [77760000, 2592000], |
|
25 | + [INF, 22896000], |
|
26 | + ]; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function relative($date) |
|
32 | - { |
|
33 | - $diff = time() - strtotime($date); |
|
34 | - foreach (static::$TIME_PERIODS as $i => $timePeriod) { |
|
35 | - if ($diff > $timePeriod[0]) { |
|
36 | - continue; |
|
37 | - } |
|
38 | - $unit = intval(floor($diff / $timePeriod[1])); |
|
39 | - $relativeDates = [ |
|
40 | - _n('%s second ago', '%s seconds ago', $unit, 'site-reviews'), |
|
41 | - _n('%s minute ago', '%s minutes ago', $unit, 'site-reviews'), |
|
42 | - _n('an hour ago', '%s hours ago', $unit, 'site-reviews'), |
|
43 | - _n('yesterday', '%s days ago', $unit, 'site-reviews'), |
|
44 | - _n('a week ago', '%s weeks ago', $unit, 'site-reviews'), |
|
45 | - _n('%s month ago', '%s months ago', $unit, 'site-reviews'), |
|
46 | - _n('%s year ago', '%s years ago', $unit, 'site-reviews'), |
|
47 | - ]; |
|
48 | - $relativeDate = $relativeDates[$i]; |
|
49 | - return Str::contains($relativeDate, '%s') |
|
50 | - ? sprintf($relativeDate, $unit) |
|
51 | - : $relativeDate; |
|
52 | - } |
|
53 | - } |
|
28 | + /** |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function relative($date) |
|
32 | + { |
|
33 | + $diff = time() - strtotime($date); |
|
34 | + foreach (static::$TIME_PERIODS as $i => $timePeriod) { |
|
35 | + if ($diff > $timePeriod[0]) { |
|
36 | + continue; |
|
37 | + } |
|
38 | + $unit = intval(floor($diff / $timePeriod[1])); |
|
39 | + $relativeDates = [ |
|
40 | + _n('%s second ago', '%s seconds ago', $unit, 'site-reviews'), |
|
41 | + _n('%s minute ago', '%s minutes ago', $unit, 'site-reviews'), |
|
42 | + _n('an hour ago', '%s hours ago', $unit, 'site-reviews'), |
|
43 | + _n('yesterday', '%s days ago', $unit, 'site-reviews'), |
|
44 | + _n('a week ago', '%s weeks ago', $unit, 'site-reviews'), |
|
45 | + _n('%s month ago', '%s months ago', $unit, 'site-reviews'), |
|
46 | + _n('%s year ago', '%s years ago', $unit, 'site-reviews'), |
|
47 | + ]; |
|
48 | + $relativeDate = $relativeDates[$i]; |
|
49 | + return Str::contains($relativeDate, '%s') |
|
50 | + ? sprintf($relativeDate, $unit) |
|
51 | + : $relativeDate; |
|
52 | + } |
|
53 | + } |
|
54 | 54 | } |
@@ -12,302 +12,302 @@ |
||
12 | 12 | */ |
13 | 13 | class Validator |
14 | 14 | { |
15 | - use ValidationRules; |
|
15 | + use ValidationRules; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - public $errors = []; |
|
17 | + /** |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + public $errors = []; |
|
21 | 21 | |
22 | - /** |
|
23 | - * The data under validation. |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $data = []; |
|
22 | + /** |
|
23 | + * The data under validation. |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $data = []; |
|
27 | 27 | |
28 | - /** |
|
29 | - * The failed validation rules. |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $failedRules = []; |
|
28 | + /** |
|
29 | + * The failed validation rules. |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $failedRules = []; |
|
33 | 33 | |
34 | - /** |
|
35 | - * The rules to be applied to the data. |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $rules = []; |
|
34 | + /** |
|
35 | + * The rules to be applied to the data. |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $rules = []; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The size related validation rules. |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - protected $sizeRules = [ |
|
45 | - 'Between', 'Max', 'Min', |
|
46 | - ]; |
|
40 | + /** |
|
41 | + * The size related validation rules. |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + protected $sizeRules = [ |
|
45 | + 'Between', 'Max', 'Min', |
|
46 | + ]; |
|
47 | 47 | |
48 | - /** |
|
49 | - * The validation rules that imply the field is required. |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $implicitRules = [ |
|
53 | - 'Required', |
|
54 | - ]; |
|
48 | + /** |
|
49 | + * The validation rules that imply the field is required. |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $implicitRules = [ |
|
53 | + 'Required', |
|
54 | + ]; |
|
55 | 55 | |
56 | - /** |
|
57 | - * The numeric related validation rules. |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - protected $numericRules = [ |
|
61 | - 'Number', |
|
62 | - ]; |
|
56 | + /** |
|
57 | + * The numeric related validation rules. |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + protected $numericRules = [ |
|
61 | + 'Number', |
|
62 | + ]; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Run the validator's rules against its data. |
|
66 | - * @param mixed $data |
|
67 | - * @return array |
|
68 | - */ |
|
69 | - public function validate($data, array $rules = []) |
|
70 | - { |
|
71 | - $this->normalizeData($data); |
|
72 | - $this->setRules($rules); |
|
73 | - foreach ($this->rules as $attribute => $rules) { |
|
74 | - foreach ($rules as $rule) { |
|
75 | - $this->validateAttribute($attribute, $rule); |
|
76 | - if ($this->shouldStopValidating($attribute)) { |
|
77 | - break; |
|
78 | - } |
|
79 | - } |
|
80 | - } |
|
81 | - return $this->errors; |
|
82 | - } |
|
64 | + /** |
|
65 | + * Run the validator's rules against its data. |
|
66 | + * @param mixed $data |
|
67 | + * @return array |
|
68 | + */ |
|
69 | + public function validate($data, array $rules = []) |
|
70 | + { |
|
71 | + $this->normalizeData($data); |
|
72 | + $this->setRules($rules); |
|
73 | + foreach ($this->rules as $attribute => $rules) { |
|
74 | + foreach ($rules as $rule) { |
|
75 | + $this->validateAttribute($attribute, $rule); |
|
76 | + if ($this->shouldStopValidating($attribute)) { |
|
77 | + break; |
|
78 | + } |
|
79 | + } |
|
80 | + } |
|
81 | + return $this->errors; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Validate a given attribute against a rule. |
|
86 | - * @param string $attribute |
|
87 | - * @param string $rule |
|
88 | - * @return void |
|
89 | - * @throws BadMethodCallException |
|
90 | - */ |
|
91 | - public function validateAttribute($attribute, $rule) |
|
92 | - { |
|
93 | - list($rule, $parameters) = $this->parseRule($rule); |
|
94 | - if ('' == $rule) { |
|
95 | - return; |
|
96 | - } |
|
97 | - $value = $this->getValue($attribute); |
|
98 | - if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
100 | - } |
|
101 | - if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | - $this->addFailure($attribute, $rule, $parameters); |
|
103 | - } |
|
104 | - } |
|
84 | + /** |
|
85 | + * Validate a given attribute against a rule. |
|
86 | + * @param string $attribute |
|
87 | + * @param string $rule |
|
88 | + * @return void |
|
89 | + * @throws BadMethodCallException |
|
90 | + */ |
|
91 | + public function validateAttribute($attribute, $rule) |
|
92 | + { |
|
93 | + list($rule, $parameters) = $this->parseRule($rule); |
|
94 | + if ('' == $rule) { |
|
95 | + return; |
|
96 | + } |
|
97 | + $value = $this->getValue($attribute); |
|
98 | + if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | + throw new BadMethodCallException("Method [$method] does not exist."); |
|
100 | + } |
|
101 | + if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | + $this->addFailure($attribute, $rule, $parameters); |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Add an error message to the validator's collection of errors. |
|
108 | - * @param string $attribute |
|
109 | - * @param string $rule |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - protected function addError($attribute, $rule, array $parameters) |
|
113 | - { |
|
114 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
115 | - $this->errors[$attribute][] = $message; |
|
116 | - } |
|
106 | + /** |
|
107 | + * Add an error message to the validator's collection of errors. |
|
108 | + * @param string $attribute |
|
109 | + * @param string $rule |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + protected function addError($attribute, $rule, array $parameters) |
|
113 | + { |
|
114 | + $message = $this->getMessage($attribute, $rule, $parameters); |
|
115 | + $this->errors[$attribute][] = $message; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Add a failed rule and error message to the collection. |
|
120 | - * @param string $attribute |
|
121 | - * @param string $rule |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - protected function addFailure($attribute, $rule, array $parameters) |
|
125 | - { |
|
126 | - $this->addError($attribute, $rule, $parameters); |
|
127 | - $this->failedRules[$attribute][$rule] = $parameters; |
|
128 | - } |
|
118 | + /** |
|
119 | + * Add a failed rule and error message to the collection. |
|
120 | + * @param string $attribute |
|
121 | + * @param string $rule |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + protected function addFailure($attribute, $rule, array $parameters) |
|
125 | + { |
|
126 | + $this->addError($attribute, $rule, $parameters); |
|
127 | + $this->failedRules[$attribute][$rule] = $parameters; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Get the data type of the given attribute. |
|
132 | - * @param string $attribute |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function getAttributeType($attribute) |
|
136 | - { |
|
137 | - return !$this->hasRule($attribute, $this->numericRules) |
|
138 | - ? 'length' |
|
139 | - : ''; |
|
140 | - } |
|
130 | + /** |
|
131 | + * Get the data type of the given attribute. |
|
132 | + * @param string $attribute |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function getAttributeType($attribute) |
|
136 | + { |
|
137 | + return !$this->hasRule($attribute, $this->numericRules) |
|
138 | + ? 'length' |
|
139 | + : ''; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Get the validation message for an attribute and rule. |
|
144 | - * @param string $attribute |
|
145 | - * @param string $rule |
|
146 | - * @return string|null |
|
147 | - */ |
|
148 | - protected function getMessage($attribute, $rule, array $parameters) |
|
149 | - { |
|
150 | - if (in_array($rule, $this->sizeRules)) { |
|
151 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
152 | - } |
|
153 | - $lowerRule = Str::snakeCase($rule); |
|
154 | - return $this->translator($lowerRule, $parameters); |
|
155 | - } |
|
142 | + /** |
|
143 | + * Get the validation message for an attribute and rule. |
|
144 | + * @param string $attribute |
|
145 | + * @param string $rule |
|
146 | + * @return string|null |
|
147 | + */ |
|
148 | + protected function getMessage($attribute, $rule, array $parameters) |
|
149 | + { |
|
150 | + if (in_array($rule, $this->sizeRules)) { |
|
151 | + return $this->getSizeMessage($attribute, $rule, $parameters); |
|
152 | + } |
|
153 | + $lowerRule = Str::snakeCase($rule); |
|
154 | + return $this->translator($lowerRule, $parameters); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * Get a rule and its parameters for a given attribute. |
|
159 | - * @param string $attribute |
|
160 | - * @param string|array $rules |
|
161 | - * @return array|null |
|
162 | - */ |
|
163 | - protected function getRule($attribute, $rules) |
|
164 | - { |
|
165 | - if (!array_key_exists($attribute, $this->rules)) { |
|
166 | - return; |
|
167 | - } |
|
168 | - $rules = (array) $rules; |
|
169 | - foreach ($this->rules[$attribute] as $rule) { |
|
170 | - list($rule, $parameters) = $this->parseRule($rule); |
|
171 | - if (in_array($rule, $rules)) { |
|
172 | - return [$rule, $parameters]; |
|
173 | - } |
|
174 | - } |
|
175 | - } |
|
157 | + /** |
|
158 | + * Get a rule and its parameters for a given attribute. |
|
159 | + * @param string $attribute |
|
160 | + * @param string|array $rules |
|
161 | + * @return array|null |
|
162 | + */ |
|
163 | + protected function getRule($attribute, $rules) |
|
164 | + { |
|
165 | + if (!array_key_exists($attribute, $this->rules)) { |
|
166 | + return; |
|
167 | + } |
|
168 | + $rules = (array) $rules; |
|
169 | + foreach ($this->rules[$attribute] as $rule) { |
|
170 | + list($rule, $parameters) = $this->parseRule($rule); |
|
171 | + if (in_array($rule, $rules)) { |
|
172 | + return [$rule, $parameters]; |
|
173 | + } |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get the size of an attribute. |
|
179 | - * @param string $attribute |
|
180 | - * @param mixed $value |
|
181 | - * @return mixed |
|
182 | - */ |
|
183 | - protected function getSize($attribute, $value) |
|
184 | - { |
|
185 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | - if (is_numeric($value) && $hasNumeric) { |
|
187 | - return $value; |
|
188 | - } elseif (is_array($value)) { |
|
189 | - return count($value); |
|
190 | - } |
|
191 | - return function_exists('mb_strlen') |
|
192 | - ? mb_strlen($value) |
|
193 | - : strlen($value); |
|
194 | - } |
|
177 | + /** |
|
178 | + * Get the size of an attribute. |
|
179 | + * @param string $attribute |
|
180 | + * @param mixed $value |
|
181 | + * @return mixed |
|
182 | + */ |
|
183 | + protected function getSize($attribute, $value) |
|
184 | + { |
|
185 | + $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | + if (is_numeric($value) && $hasNumeric) { |
|
187 | + return $value; |
|
188 | + } elseif (is_array($value)) { |
|
189 | + return count($value); |
|
190 | + } |
|
191 | + return function_exists('mb_strlen') |
|
192 | + ? mb_strlen($value) |
|
193 | + : strlen($value); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Get the proper error message for an attribute and size rule. |
|
198 | - * @param string $attribute |
|
199 | - * @param string $rule |
|
200 | - * @return string|null |
|
201 | - */ |
|
202 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
203 | - { |
|
204 | - $type = $this->getAttributeType($attribute); |
|
205 | - $lowerRule = Str::snakeCase($rule.$type); |
|
206 | - return $this->translator($lowerRule, $parameters); |
|
207 | - } |
|
196 | + /** |
|
197 | + * Get the proper error message for an attribute and size rule. |
|
198 | + * @param string $attribute |
|
199 | + * @param string $rule |
|
200 | + * @return string|null |
|
201 | + */ |
|
202 | + protected function getSizeMessage($attribute, $rule, array $parameters) |
|
203 | + { |
|
204 | + $type = $this->getAttributeType($attribute); |
|
205 | + $lowerRule = Str::snakeCase($rule.$type); |
|
206 | + return $this->translator($lowerRule, $parameters); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Get the value of a given attribute. |
|
211 | - * @param string $attribute |
|
212 | - * @return mixed |
|
213 | - */ |
|
214 | - protected function getValue($attribute) |
|
215 | - { |
|
216 | - if (isset($this->data[$attribute])) { |
|
217 | - return $this->data[$attribute]; |
|
218 | - } |
|
219 | - } |
|
209 | + /** |
|
210 | + * Get the value of a given attribute. |
|
211 | + * @param string $attribute |
|
212 | + * @return mixed |
|
213 | + */ |
|
214 | + protected function getValue($attribute) |
|
215 | + { |
|
216 | + if (isset($this->data[$attribute])) { |
|
217 | + return $this->data[$attribute]; |
|
218 | + } |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Determine if the given attribute has a rule in the given set. |
|
223 | - * @param string $attribute |
|
224 | - * @param string|array $rules |
|
225 | - * @return bool |
|
226 | - */ |
|
227 | - protected function hasRule($attribute, $rules) |
|
228 | - { |
|
229 | - return !is_null($this->getRule($attribute, $rules)); |
|
230 | - } |
|
221 | + /** |
|
222 | + * Determine if the given attribute has a rule in the given set. |
|
223 | + * @param string $attribute |
|
224 | + * @param string|array $rules |
|
225 | + * @return bool |
|
226 | + */ |
|
227 | + protected function hasRule($attribute, $rules) |
|
228 | + { |
|
229 | + return !is_null($this->getRule($attribute, $rules)); |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Normalize the provided data to an array. |
|
234 | - * @param mixed $data |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - protected function normalizeData($data) |
|
238 | - { |
|
239 | - $this->data = is_object($data) |
|
240 | - ? get_object_vars($data) |
|
241 | - : $data; |
|
242 | - } |
|
232 | + /** |
|
233 | + * Normalize the provided data to an array. |
|
234 | + * @param mixed $data |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + protected function normalizeData($data) |
|
238 | + { |
|
239 | + $this->data = is_object($data) |
|
240 | + ? get_object_vars($data) |
|
241 | + : $data; |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * Parse a parameter list. |
|
246 | - * @param string $rule |
|
247 | - * @param string $parameter |
|
248 | - * @return array |
|
249 | - */ |
|
250 | - protected function parseParameters($rule, $parameter) |
|
251 | - { |
|
252 | - return 'regex' == strtolower($rule) |
|
253 | - ? [$parameter] |
|
254 | - : str_getcsv($parameter); |
|
255 | - } |
|
244 | + /** |
|
245 | + * Parse a parameter list. |
|
246 | + * @param string $rule |
|
247 | + * @param string $parameter |
|
248 | + * @return array |
|
249 | + */ |
|
250 | + protected function parseParameters($rule, $parameter) |
|
251 | + { |
|
252 | + return 'regex' == strtolower($rule) |
|
253 | + ? [$parameter] |
|
254 | + : str_getcsv($parameter); |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Extract the rule name and parameters from a rule. |
|
259 | - * @param string $rule |
|
260 | - * @return array |
|
261 | - */ |
|
262 | - protected function parseRule($rule) |
|
263 | - { |
|
264 | - $parameters = []; |
|
265 | - if (Str::contains($rule, ':')) { |
|
266 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | - $parameters = $this->parseParameters($rule, $parameter); |
|
268 | - } |
|
269 | - $rule = Str::camelCase($rule); |
|
270 | - return [$rule, $parameters]; |
|
271 | - } |
|
257 | + /** |
|
258 | + * Extract the rule name and parameters from a rule. |
|
259 | + * @param string $rule |
|
260 | + * @return array |
|
261 | + */ |
|
262 | + protected function parseRule($rule) |
|
263 | + { |
|
264 | + $parameters = []; |
|
265 | + if (Str::contains($rule, ':')) { |
|
266 | + list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | + $parameters = $this->parseParameters($rule, $parameter); |
|
268 | + } |
|
269 | + $rule = Str::camelCase($rule); |
|
270 | + return [$rule, $parameters]; |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Set the validation rules. |
|
275 | - * @return void |
|
276 | - */ |
|
277 | - protected function setRules(array $rules) |
|
278 | - { |
|
279 | - foreach ($rules as $key => $rule) { |
|
280 | - $rules[$key] = is_string($rule) |
|
281 | - ? explode('|', $rule) |
|
282 | - : $rule; |
|
283 | - } |
|
284 | - $this->rules = $rules; |
|
285 | - } |
|
273 | + /** |
|
274 | + * Set the validation rules. |
|
275 | + * @return void |
|
276 | + */ |
|
277 | + protected function setRules(array $rules) |
|
278 | + { |
|
279 | + foreach ($rules as $key => $rule) { |
|
280 | + $rules[$key] = is_string($rule) |
|
281 | + ? explode('|', $rule) |
|
282 | + : $rule; |
|
283 | + } |
|
284 | + $this->rules = $rules; |
|
285 | + } |
|
286 | 286 | |
287 | - /** |
|
288 | - * Check if we should stop further validations on a given attribute. |
|
289 | - * @param string $attribute |
|
290 | - * @return bool |
|
291 | - */ |
|
292 | - protected function shouldStopValidating($attribute) |
|
293 | - { |
|
294 | - return $this->hasRule($attribute, $this->implicitRules) |
|
295 | - && isset($this->failedRules[$attribute]) |
|
296 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
297 | - } |
|
287 | + /** |
|
288 | + * Check if we should stop further validations on a given attribute. |
|
289 | + * @param string $attribute |
|
290 | + * @return bool |
|
291 | + */ |
|
292 | + protected function shouldStopValidating($attribute) |
|
293 | + { |
|
294 | + return $this->hasRule($attribute, $this->implicitRules) |
|
295 | + && isset($this->failedRules[$attribute]) |
|
296 | + && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Returns a translated message for the attribute. |
|
301 | - * @param string $key |
|
302 | - * @param string $attribute |
|
303 | - * @return void|string |
|
304 | - */ |
|
305 | - protected function translator($key, array $parameters) |
|
306 | - { |
|
307 | - $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | - if (isset($strings[$key])) { |
|
309 | - return $this->replace($strings[$key], $parameters); |
|
310 | - } |
|
311 | - return 'error'; |
|
312 | - } |
|
299 | + /** |
|
300 | + * Returns a translated message for the attribute. |
|
301 | + * @param string $key |
|
302 | + * @param string $attribute |
|
303 | + * @return void|string |
|
304 | + */ |
|
305 | + protected function translator($key, array $parameters) |
|
306 | + { |
|
307 | + $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | + if (isset($strings[$key])) { |
|
309 | + return $this->replace($strings[$key], $parameters); |
|
310 | + } |
|
311 | + return 'error'; |
|
312 | + } |
|
313 | 313 | } |
@@ -10,126 +10,126 @@ |
||
10 | 10 | */ |
11 | 11 | trait ValidationRules |
12 | 12 | { |
13 | - /** |
|
14 | - * Get the size of an attribute. |
|
15 | - * @param string $attribute |
|
16 | - * @param mixed $value |
|
17 | - * @return mixed |
|
18 | - */ |
|
19 | - abstract protected function getSize($attribute, $value); |
|
13 | + /** |
|
14 | + * Get the size of an attribute. |
|
15 | + * @param string $attribute |
|
16 | + * @param mixed $value |
|
17 | + * @return mixed |
|
18 | + */ |
|
19 | + abstract protected function getSize($attribute, $value); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Replace all placeholders. |
|
23 | - * @param string $message |
|
24 | - * @return string |
|
25 | - */ |
|
26 | - protected function replace($message, array $parameters) |
|
27 | - { |
|
28 | - if (!Str::contains($message, '%s')) { |
|
29 | - return $message; |
|
30 | - } |
|
31 | - return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
32 | - foreach ($parameters as $key => $value) { |
|
33 | - return array_shift($parameters); |
|
34 | - } |
|
35 | - }, $message); |
|
36 | - } |
|
21 | + /** |
|
22 | + * Replace all placeholders. |
|
23 | + * @param string $message |
|
24 | + * @return string |
|
25 | + */ |
|
26 | + protected function replace($message, array $parameters) |
|
27 | + { |
|
28 | + if (!Str::contains($message, '%s')) { |
|
29 | + return $message; |
|
30 | + } |
|
31 | + return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
32 | + foreach ($parameters as $key => $value) { |
|
33 | + return array_shift($parameters); |
|
34 | + } |
|
35 | + }, $message); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Validate that an attribute was "accepted". |
|
40 | - * This validation rule implies the attribute is "required". |
|
41 | - * @param string $attribute |
|
42 | - * @param mixed $value |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function validateAccepted($value) |
|
46 | - { |
|
47 | - $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
48 | - return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
49 | - } |
|
38 | + /** |
|
39 | + * Validate that an attribute was "accepted". |
|
40 | + * This validation rule implies the attribute is "required". |
|
41 | + * @param string $attribute |
|
42 | + * @param mixed $value |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function validateAccepted($value) |
|
46 | + { |
|
47 | + $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
48 | + return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Validate the size of an attribute is between a set of values. |
|
53 | - * @param string $attribute |
|
54 | - * @param mixed $value |
|
55 | - * @return bool |
|
56 | - */ |
|
57 | - public function validateBetween($value, $attribute, array $parameters) |
|
58 | - { |
|
59 | - $this->requireParameterCount(2, $parameters, 'between'); |
|
60 | - $size = $this->getSize($attribute, $value); |
|
61 | - return $size >= $parameters[0] && $size <= $parameters[1]; |
|
62 | - } |
|
51 | + /** |
|
52 | + * Validate the size of an attribute is between a set of values. |
|
53 | + * @param string $attribute |
|
54 | + * @param mixed $value |
|
55 | + * @return bool |
|
56 | + */ |
|
57 | + public function validateBetween($value, $attribute, array $parameters) |
|
58 | + { |
|
59 | + $this->requireParameterCount(2, $parameters, 'between'); |
|
60 | + $size = $this->getSize($attribute, $value); |
|
61 | + return $size >= $parameters[0] && $size <= $parameters[1]; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Validate that an attribute is a valid e-mail address. |
|
66 | - * @param mixed $value |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function validateEmail($value) |
|
70 | - { |
|
71 | - return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
72 | - } |
|
64 | + /** |
|
65 | + * Validate that an attribute is a valid e-mail address. |
|
66 | + * @param mixed $value |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function validateEmail($value) |
|
70 | + { |
|
71 | + return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Validate the size of an attribute is less than a maximum value. |
|
76 | - * @param string $attribute |
|
77 | - * @param mixed $value |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function validateMax($value, $attribute, array $parameters) |
|
81 | - { |
|
82 | - $this->requireParameterCount(1, $parameters, 'max'); |
|
83 | - return $this->getSize($attribute, $value) <= $parameters[0]; |
|
84 | - } |
|
74 | + /** |
|
75 | + * Validate the size of an attribute is less than a maximum value. |
|
76 | + * @param string $attribute |
|
77 | + * @param mixed $value |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function validateMax($value, $attribute, array $parameters) |
|
81 | + { |
|
82 | + $this->requireParameterCount(1, $parameters, 'max'); |
|
83 | + return $this->getSize($attribute, $value) <= $parameters[0]; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Validate the size of an attribute is greater than a minimum value. |
|
88 | - * @param string $attribute |
|
89 | - * @param mixed $value |
|
90 | - * @return bool |
|
91 | - */ |
|
92 | - public function validateMin($value, $attribute, array $parameters) |
|
93 | - { |
|
94 | - $this->requireParameterCount(1, $parameters, 'min'); |
|
95 | - return $this->getSize($attribute, $value) >= $parameters[0]; |
|
96 | - } |
|
86 | + /** |
|
87 | + * Validate the size of an attribute is greater than a minimum value. |
|
88 | + * @param string $attribute |
|
89 | + * @param mixed $value |
|
90 | + * @return bool |
|
91 | + */ |
|
92 | + public function validateMin($value, $attribute, array $parameters) |
|
93 | + { |
|
94 | + $this->requireParameterCount(1, $parameters, 'min'); |
|
95 | + return $this->getSize($attribute, $value) >= $parameters[0]; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Validate that an attribute is numeric. |
|
100 | - * @param mixed $value |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - public function validateNumber($value) |
|
104 | - { |
|
105 | - return is_numeric($value); |
|
106 | - } |
|
98 | + /** |
|
99 | + * Validate that an attribute is numeric. |
|
100 | + * @param mixed $value |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + public function validateNumber($value) |
|
104 | + { |
|
105 | + return is_numeric($value); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Validate that a required attribute exists. |
|
110 | - * @param mixed $value |
|
111 | - * @return bool |
|
112 | - */ |
|
113 | - public function validateRequired($value) |
|
114 | - { |
|
115 | - return is_null($value) |
|
116 | - || (is_string($value) && '' === trim($value)) |
|
117 | - || (is_array($value) && count($value) < 1) |
|
118 | - ? false |
|
119 | - : true; |
|
120 | - } |
|
108 | + /** |
|
109 | + * Validate that a required attribute exists. |
|
110 | + * @param mixed $value |
|
111 | + * @return bool |
|
112 | + */ |
|
113 | + public function validateRequired($value) |
|
114 | + { |
|
115 | + return is_null($value) |
|
116 | + || (is_string($value) && '' === trim($value)) |
|
117 | + || (is_array($value) && count($value) < 1) |
|
118 | + ? false |
|
119 | + : true; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Require a certain number of parameters to be present. |
|
124 | - * @param int $count |
|
125 | - * @param string $rule |
|
126 | - * @return void |
|
127 | - * @throws InvalidArgumentException |
|
128 | - */ |
|
129 | - protected function requireParameterCount($count, array $parameters, $rule) |
|
130 | - { |
|
131 | - if (count($parameters) < $count) { |
|
132 | - throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
133 | - } |
|
134 | - } |
|
122 | + /** |
|
123 | + * Require a certain number of parameters to be present. |
|
124 | + * @param int $count |
|
125 | + * @param string $rule |
|
126 | + * @return void |
|
127 | + * @throws InvalidArgumentException |
|
128 | + */ |
|
129 | + protected function requireParameterCount($count, array $parameters, $rule) |
|
130 | + { |
|
131 | + if (count($parameters) < $count) { |
|
132 | + throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
133 | + } |
|
134 | + } |
|
135 | 135 | } |
@@ -6,97 +6,97 @@ |
||
6 | 6 | |
7 | 7 | class Translator |
8 | 8 | { |
9 | - /** |
|
10 | - * @param string $original |
|
11 | - * @param string $domain |
|
12 | - * @return string |
|
13 | - */ |
|
14 | - public function translate($original, $domain, array $args) |
|
15 | - { |
|
16 | - $domains = apply_filters('site-reviews/translator/domains', [Application::ID]); |
|
17 | - if (!in_array($domain, $domains)) { |
|
18 | - return $original; |
|
19 | - } |
|
20 | - $args = $this->normalizeTranslationArgs($args); |
|
21 | - $strings = $this->getTranslationStrings($args['single'], $args['plural']); |
|
22 | - if (empty($strings)) { |
|
23 | - return $original; |
|
24 | - } |
|
25 | - $string = current($strings); |
|
26 | - return 'plural' == $string['type'] |
|
27 | - ? $this->translatePlural($domain, $string, $args) |
|
28 | - : $this->translateSingle($domain, $string, $args); |
|
29 | - } |
|
9 | + /** |
|
10 | + * @param string $original |
|
11 | + * @param string $domain |
|
12 | + * @return string |
|
13 | + */ |
|
14 | + public function translate($original, $domain, array $args) |
|
15 | + { |
|
16 | + $domains = apply_filters('site-reviews/translator/domains', [Application::ID]); |
|
17 | + if (!in_array($domain, $domains)) { |
|
18 | + return $original; |
|
19 | + } |
|
20 | + $args = $this->normalizeTranslationArgs($args); |
|
21 | + $strings = $this->getTranslationStrings($args['single'], $args['plural']); |
|
22 | + if (empty($strings)) { |
|
23 | + return $original; |
|
24 | + } |
|
25 | + $string = current($strings); |
|
26 | + return 'plural' == $string['type'] |
|
27 | + ? $this->translatePlural($domain, $string, $args) |
|
28 | + : $this->translateSingle($domain, $string, $args); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * Used when search/replacing a default text-domain translation |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function getTranslation(array $args) |
|
36 | - { |
|
37 | - $args = $this->normalizeTranslationArgs($args); |
|
38 | - return get_translations_for_domain(Application::ID)->translate_plural($args['single'], $args['plural'], $args['number']); |
|
39 | - } |
|
31 | + /** |
|
32 | + * Used when search/replacing a default text-domain translation |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function getTranslation(array $args) |
|
36 | + { |
|
37 | + $args = $this->normalizeTranslationArgs($args); |
|
38 | + return get_translations_for_domain(Application::ID)->translate_plural($args['single'], $args['plural'], $args['number']); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param string $single |
|
43 | - * @param string $plural |
|
44 | - * @return array |
|
45 | - */ |
|
46 | - protected function getTranslationStrings($single, $plural) |
|
47 | - { |
|
48 | - return array_filter(glsr(Translation::class)->translations(), function ($string) use ($single, $plural) { |
|
49 | - return $string['s1'] == html_entity_decode($single, ENT_COMPAT, 'UTF-8') |
|
50 | - && $string['p1'] == html_entity_decode($plural, ENT_COMPAT, 'UTF-8'); |
|
51 | - }); |
|
52 | - } |
|
41 | + /** |
|
42 | + * @param string $single |
|
43 | + * @param string $plural |
|
44 | + * @return array |
|
45 | + */ |
|
46 | + protected function getTranslationStrings($single, $plural) |
|
47 | + { |
|
48 | + return array_filter(glsr(Translation::class)->translations(), function ($string) use ($single, $plural) { |
|
49 | + return $string['s1'] == html_entity_decode($single, ENT_COMPAT, 'UTF-8') |
|
50 | + && $string['p1'] == html_entity_decode($plural, ENT_COMPAT, 'UTF-8'); |
|
51 | + }); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - protected function normalizeTranslationArgs(array $args) |
|
58 | - { |
|
59 | - $defaults = [ |
|
60 | - 'context' => '', |
|
61 | - 'number' => 1, |
|
62 | - 'plural' => '', |
|
63 | - 'single' => '', |
|
64 | - ]; |
|
65 | - return shortcode_atts($defaults, $args); |
|
66 | - } |
|
54 | + /** |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + protected function normalizeTranslationArgs(array $args) |
|
58 | + { |
|
59 | + $defaults = [ |
|
60 | + 'context' => '', |
|
61 | + 'number' => 1, |
|
62 | + 'plural' => '', |
|
63 | + 'single' => '', |
|
64 | + ]; |
|
65 | + return shortcode_atts($defaults, $args); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @param string $domain |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - protected function translatePlural($domain, array $string, array $args) |
|
73 | - { |
|
74 | - if (!empty($string['s2'])) { |
|
75 | - $args['single'] = $string['s2']; |
|
76 | - } |
|
77 | - if (!empty($string['p2'])) { |
|
78 | - $args['plural'] = $string['p2']; |
|
79 | - } |
|
80 | - return get_translations_for_domain($domain)->translate_plural( |
|
81 | - $args['single'], |
|
82 | - $args['plural'], |
|
83 | - $args['number'], |
|
84 | - $args['context'] |
|
85 | - ); |
|
86 | - } |
|
68 | + /** |
|
69 | + * @param string $domain |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + protected function translatePlural($domain, array $string, array $args) |
|
73 | + { |
|
74 | + if (!empty($string['s2'])) { |
|
75 | + $args['single'] = $string['s2']; |
|
76 | + } |
|
77 | + if (!empty($string['p2'])) { |
|
78 | + $args['plural'] = $string['p2']; |
|
79 | + } |
|
80 | + return get_translations_for_domain($domain)->translate_plural( |
|
81 | + $args['single'], |
|
82 | + $args['plural'], |
|
83 | + $args['number'], |
|
84 | + $args['context'] |
|
85 | + ); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @param string $domain |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - protected function translateSingle($domain, array $string, array $args) |
|
93 | - { |
|
94 | - if (!empty($string['s2'])) { |
|
95 | - $args['single'] = $string['s2']; |
|
96 | - } |
|
97 | - return get_translations_for_domain($domain)->translate( |
|
98 | - $args['single'], |
|
99 | - $args['context'] |
|
100 | - ); |
|
101 | - } |
|
88 | + /** |
|
89 | + * @param string $domain |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + protected function translateSingle($domain, array $string, array $args) |
|
93 | + { |
|
94 | + if (!empty($string['s2'])) { |
|
95 | + $args['single'] = $string['s2']; |
|
96 | + } |
|
97 | + return get_translations_for_domain($domain)->translate( |
|
98 | + $args['single'], |
|
99 | + $args['context'] |
|
100 | + ); |
|
101 | + } |
|
102 | 102 | } |
@@ -6,174 +6,174 @@ |
||
6 | 6 | |
7 | 7 | class Str |
8 | 8 | { |
9 | - /** |
|
10 | - * @param string $string |
|
11 | - * @return string |
|
12 | - */ |
|
13 | - public static function camelCase($string) |
|
14 | - { |
|
15 | - $string = ucwords(str_replace(['-', '_'], ' ', trim($string))); |
|
16 | - return str_replace(' ', '', $string); |
|
17 | - } |
|
9 | + /** |
|
10 | + * @param string $string |
|
11 | + * @return string |
|
12 | + */ |
|
13 | + public static function camelCase($string) |
|
14 | + { |
|
15 | + $string = ucwords(str_replace(['-', '_'], ' ', trim($string))); |
|
16 | + return str_replace(' ', '', $string); |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param string $haystack |
|
21 | - * @param string $needle |
|
22 | - * @return bool |
|
23 | - */ |
|
24 | - public static function contains($haystack, $needle) |
|
25 | - { |
|
26 | - return false !== strpos($haystack, $needle); |
|
27 | - } |
|
19 | + /** |
|
20 | + * @param string $haystack |
|
21 | + * @param string $needle |
|
22 | + * @return bool |
|
23 | + */ |
|
24 | + public static function contains($haystack, $needle) |
|
25 | + { |
|
26 | + return false !== strpos($haystack, $needle); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @param string $name |
|
31 | - * @param string $nameType first|first_initial|initials|last|last_initial |
|
32 | - * @param string $initialType period|period_space|space |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public static function convertName($name, $nameType = '', $initialType = '') |
|
36 | - { |
|
37 | - $names = preg_split('/\W/', $name, 0, PREG_SPLIT_NO_EMPTY); |
|
38 | - $firstName = array_shift($names); |
|
39 | - $lastName = array_pop($names); |
|
40 | - $initialTypes = [ |
|
41 | - 'period' => '.', |
|
42 | - 'period_space' => '. ', |
|
43 | - 'space' => ' ', |
|
44 | - ]; |
|
45 | - $initialPunctuation = (string) Arr::get($initialTypes, $initialType, ' '); |
|
46 | - if ('initials' == $nameType) { |
|
47 | - return static::convertToInitials($name, $initialPunctuation); |
|
48 | - } |
|
49 | - $nameTypes = [ |
|
50 | - 'first' => $firstName, |
|
51 | - 'first_initial' => substr($firstName, 0, 1).$initialPunctuation.$lastName, |
|
52 | - 'last' => $lastName, |
|
53 | - 'last_initial' => $firstName.' '.substr($lastName, 0, 1).$initialPunctuation, |
|
54 | - ]; |
|
55 | - return trim((string) Arr::get($nameTypes, $nameType, $name)); |
|
56 | - } |
|
29 | + /** |
|
30 | + * @param string $name |
|
31 | + * @param string $nameType first|first_initial|initials|last|last_initial |
|
32 | + * @param string $initialType period|period_space|space |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public static function convertName($name, $nameType = '', $initialType = '') |
|
36 | + { |
|
37 | + $names = preg_split('/\W/', $name, 0, PREG_SPLIT_NO_EMPTY); |
|
38 | + $firstName = array_shift($names); |
|
39 | + $lastName = array_pop($names); |
|
40 | + $initialTypes = [ |
|
41 | + 'period' => '.', |
|
42 | + 'period_space' => '. ', |
|
43 | + 'space' => ' ', |
|
44 | + ]; |
|
45 | + $initialPunctuation = (string) Arr::get($initialTypes, $initialType, ' '); |
|
46 | + if ('initials' == $nameType) { |
|
47 | + return static::convertToInitials($name, $initialPunctuation); |
|
48 | + } |
|
49 | + $nameTypes = [ |
|
50 | + 'first' => $firstName, |
|
51 | + 'first_initial' => substr($firstName, 0, 1).$initialPunctuation.$lastName, |
|
52 | + 'last' => $lastName, |
|
53 | + 'last_initial' => $firstName.' '.substr($lastName, 0, 1).$initialPunctuation, |
|
54 | + ]; |
|
55 | + return trim((string) Arr::get($nameTypes, $nameType, $name)); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param string $path |
|
60 | - * @param string $prefix |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - public static function convertPathToId($path, $prefix = '') |
|
64 | - { |
|
65 | - return str_replace(['[', ']'], ['-', ''], static::convertPathToName($path, $prefix)); |
|
66 | - } |
|
58 | + /** |
|
59 | + * @param string $path |
|
60 | + * @param string $prefix |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + public static function convertPathToId($path, $prefix = '') |
|
64 | + { |
|
65 | + return str_replace(['[', ']'], ['-', ''], static::convertPathToName($path, $prefix)); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @param string $path |
|
70 | - * @param string $prefix |
|
71 | - * @return string |
|
72 | - */ |
|
73 | - public static function convertPathToName($path, $prefix = '') |
|
74 | - { |
|
75 | - $levels = explode('.', $path); |
|
76 | - return array_reduce($levels, function ($result, $value) { |
|
77 | - return $result .= '['.$value.']'; |
|
78 | - }, $prefix); |
|
79 | - } |
|
68 | + /** |
|
69 | + * @param string $path |
|
70 | + * @param string $prefix |
|
71 | + * @return string |
|
72 | + */ |
|
73 | + public static function convertPathToName($path, $prefix = '') |
|
74 | + { |
|
75 | + $levels = explode('.', $path); |
|
76 | + return array_reduce($levels, function ($result, $value) { |
|
77 | + return $result .= '['.$value.']'; |
|
78 | + }, $prefix); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param string $name |
|
83 | - * @param string $initialPunctuation |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public static function convertToInitials($name, $initialPunctuation = '') |
|
87 | - { |
|
88 | - preg_match_all('/(?<=\s|\b)\pL/u', $name, $matches); |
|
89 | - return array_reduce($matches[0], function ($carry, $word) use ($initialPunctuation) { |
|
90 | - return $carry.strtoupper(substr($word, 0, 1)).$initialPunctuation; |
|
91 | - }); |
|
92 | - } |
|
81 | + /** |
|
82 | + * @param string $name |
|
83 | + * @param string $initialPunctuation |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public static function convertToInitials($name, $initialPunctuation = '') |
|
87 | + { |
|
88 | + preg_match_all('/(?<=\s|\b)\pL/u', $name, $matches); |
|
89 | + return array_reduce($matches[0], function ($carry, $word) use ($initialPunctuation) { |
|
90 | + return $carry.strtoupper(substr($word, 0, 1)).$initialPunctuation; |
|
91 | + }); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @param string $string |
|
96 | - * @return string |
|
97 | - */ |
|
98 | - public static function dashCase($string) |
|
99 | - { |
|
100 | - return str_replace('_', '-', static::snakeCase($string)); |
|
101 | - } |
|
94 | + /** |
|
95 | + * @param string $string |
|
96 | + * @return string |
|
97 | + */ |
|
98 | + public static function dashCase($string) |
|
99 | + { |
|
100 | + return str_replace('_', '-', static::snakeCase($string)); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * @param string $needle |
|
105 | - * @param string $haystack |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - public static function endsWith($needle, $haystack) |
|
109 | - { |
|
110 | - $length = strlen($needle); |
|
111 | - return 0 != $length |
|
112 | - ? substr($haystack, -$length) === $needle |
|
113 | - : true; |
|
114 | - } |
|
103 | + /** |
|
104 | + * @param string $needle |
|
105 | + * @param string $haystack |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + public static function endsWith($needle, $haystack) |
|
109 | + { |
|
110 | + $length = strlen($needle); |
|
111 | + return 0 != $length |
|
112 | + ? substr($haystack, -$length) === $needle |
|
113 | + : true; |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * @param string $prefix |
|
118 | - * @param string $string |
|
119 | - * @param string|null $trim |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - public static function prefix($prefix, $string, $trim = null) |
|
123 | - { |
|
124 | - if (null === $trim) { |
|
125 | - $trim = $prefix; |
|
126 | - } |
|
127 | - return $prefix.trim(static::removePrefix($trim, $string)); |
|
128 | - } |
|
116 | + /** |
|
117 | + * @param string $prefix |
|
118 | + * @param string $string |
|
119 | + * @param string|null $trim |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + public static function prefix($prefix, $string, $trim = null) |
|
123 | + { |
|
124 | + if (null === $trim) { |
|
125 | + $trim = $prefix; |
|
126 | + } |
|
127 | + return $prefix.trim(static::removePrefix($trim, $string)); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @param string $prefix |
|
132 | - * @param string $string |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - public static function removePrefix($prefix, $string) |
|
136 | - { |
|
137 | - return static::startsWith($prefix, $string) |
|
138 | - ? substr($string, strlen($prefix)) |
|
139 | - : $string; |
|
140 | - } |
|
130 | + /** |
|
131 | + * @param string $prefix |
|
132 | + * @param string $string |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + public static function removePrefix($prefix, $string) |
|
136 | + { |
|
137 | + return static::startsWith($prefix, $string) |
|
138 | + ? substr($string, strlen($prefix)) |
|
139 | + : $string; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * @param string $string |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - public static function snakeCase($string) |
|
147 | - { |
|
148 | - if (!ctype_lower($string)) { |
|
149 | - $string = preg_replace('/\s+/u', '', $string); |
|
150 | - $string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
151 | - $string = function_exists('mb_strtolower') |
|
152 | - ? mb_strtolower($string, 'UTF-8') |
|
153 | - : strtolower($string); |
|
154 | - } |
|
155 | - return str_replace('-', '_', $string); |
|
156 | - } |
|
142 | + /** |
|
143 | + * @param string $string |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + public static function snakeCase($string) |
|
147 | + { |
|
148 | + if (!ctype_lower($string)) { |
|
149 | + $string = preg_replace('/\s+/u', '', $string); |
|
150 | + $string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
151 | + $string = function_exists('mb_strtolower') |
|
152 | + ? mb_strtolower($string, 'UTF-8') |
|
153 | + : strtolower($string); |
|
154 | + } |
|
155 | + return str_replace('-', '_', $string); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @param string $needle |
|
160 | - * @param string $haystack |
|
161 | - * @return bool |
|
162 | - */ |
|
163 | - public static function startsWith($needle, $haystack) |
|
164 | - { |
|
165 | - return substr($haystack, 0, strlen($needle)) === $needle; |
|
166 | - } |
|
158 | + /** |
|
159 | + * @param string $needle |
|
160 | + * @param string $haystack |
|
161 | + * @return bool |
|
162 | + */ |
|
163 | + public static function startsWith($needle, $haystack) |
|
164 | + { |
|
165 | + return substr($haystack, 0, strlen($needle)) === $needle; |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * @param string $string |
|
170 | - * @param int $length |
|
171 | - * @return string |
|
172 | - */ |
|
173 | - public static function truncate($string, $length) |
|
174 | - { |
|
175 | - return strlen($string) > $length |
|
176 | - ? substr($string, 0, $length) |
|
177 | - : $string; |
|
178 | - } |
|
168 | + /** |
|
169 | + * @param string $string |
|
170 | + * @param int $length |
|
171 | + * @return string |
|
172 | + */ |
|
173 | + public static function truncate($string, $length) |
|
174 | + { |
|
175 | + return strlen($string) > $length |
|
176 | + ? substr($string, 0, $length) |
|
177 | + : $string; |
|
178 | + } |
|
179 | 179 | } |
@@ -10,228 +10,228 @@ |
||
10 | 10 | |
11 | 11 | class TranslationController |
12 | 12 | { |
13 | - /** |
|
14 | - * @var Translator |
|
15 | - */ |
|
16 | - public $translator; |
|
17 | - |
|
18 | - public function __construct(Translator $translator) |
|
19 | - { |
|
20 | - $this->translator = $translator; |
|
21 | - } |
|
22 | - |
|
23 | - /** |
|
24 | - * @param array $messages |
|
25 | - * @return array |
|
26 | - * @filter bulk_post_updated_messages |
|
27 | - */ |
|
28 | - public function filterBulkUpdateMessages($messages, array $counts) |
|
29 | - { |
|
30 | - $messages = Arr::consolidateArray($messages); |
|
31 | - $messages[Application::POST_TYPE] = [ |
|
32 | - 'updated' => _n('%s review updated.', '%s reviews updated.', $counts['updated'], 'site-reviews'), |
|
33 | - 'locked' => _n('%s review not updated, somebody is editing it.', '%s reviews not updated, somebody is editing them.', $counts['locked'], 'site-reviews'), |
|
34 | - 'deleted' => _n('%s review permanently deleted.', '%s reviews permanently deleted.', $counts['deleted'], 'site-reviews'), |
|
35 | - 'trashed' => _n('%s review moved to the Trash.', '%s reviews moved to the Trash.', $counts['trashed'], 'site-reviews'), |
|
36 | - 'untrashed' => _n('%s review restored from the Trash.', '%s reviews restored from the Trash.', $counts['untrashed'], 'site-reviews'), |
|
37 | - ]; |
|
38 | - return $messages; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @param string $translation |
|
43 | - * @param string $text |
|
44 | - * @param string $domain |
|
45 | - * @return string |
|
46 | - * @filter gettext |
|
47 | - */ |
|
48 | - public function filterGettext($translation, $text, $domain) |
|
49 | - { |
|
50 | - return apply_filters('site-reviews/gettext/'.$domain, $translation, $text); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param string $translation |
|
55 | - * @param string $text |
|
56 | - * @return string |
|
57 | - * @filter site-reviews/gettext/site-reviews |
|
58 | - */ |
|
59 | - public function filterGettextSiteReviews($translation, $text) |
|
60 | - { |
|
61 | - return $this->translator->translate($translation, Application::ID, [ |
|
62 | - 'single' => $text, |
|
63 | - ]); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @param string $translation |
|
68 | - * @param string $text |
|
69 | - * @param string $context |
|
70 | - * @param string $domain |
|
71 | - * @return string |
|
72 | - * @filter gettext_with_context |
|
73 | - */ |
|
74 | - public function filterGettextWithContext($translation, $text, $context, $domain) |
|
75 | - { |
|
76 | - return apply_filters('site-reviews/gettext_with_context/'.$domain, $translation, $text, $context); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param string $translation |
|
81 | - * @param string $text |
|
82 | - * @param string $context |
|
83 | - * @return string |
|
84 | - * @filter site-reviews/gettext_with_context/site-reviews |
|
85 | - */ |
|
86 | - public function filterGettextWithContextSiteReviews($translation, $text, $context) |
|
87 | - { |
|
88 | - return $this->translator->translate($translation, Application::ID, [ |
|
89 | - 'context' => $context, |
|
90 | - 'single' => $text, |
|
91 | - ]); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string $translation |
|
96 | - * @param string $single |
|
97 | - * @param string $plural |
|
98 | - * @param int $number |
|
99 | - * @param string $domain |
|
100 | - * @return string |
|
101 | - * @filter ngettext |
|
102 | - */ |
|
103 | - public function filterNgettext($translation, $single, $plural, $number, $domain) |
|
104 | - { |
|
105 | - return apply_filters('site-reviews/ngettext/'.$domain, $translation, $single, $plural, $number); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @param string $translation |
|
110 | - * @param string $single |
|
111 | - * @param string $plural |
|
112 | - * @param int $number |
|
113 | - * @return string |
|
114 | - * @filter site-reviews/ngettext/site-reviews |
|
115 | - */ |
|
116 | - public function filterNgettextSiteReviews($translation, $single, $plural, $number) |
|
117 | - { |
|
118 | - return $this->translator->translate($translation, Application::ID, [ |
|
119 | - 'number' => $number, |
|
120 | - 'plural' => $plural, |
|
121 | - 'single' => $single, |
|
122 | - ]); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $translation |
|
127 | - * @param string $single |
|
128 | - * @param string $plural |
|
129 | - * @param int $number |
|
130 | - * @param string $context |
|
131 | - * @param string $domain |
|
132 | - * @return string |
|
133 | - * @filter ngettext_with_context |
|
134 | - */ |
|
135 | - public function filterNgettextWithContext($translation, $single, $plural, $number, $context, $domain) |
|
136 | - { |
|
137 | - return apply_filters('site-reviews/ngettext_with_context/'.$domain, $translation, $single, $plural, $number, $context); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param string $translation |
|
142 | - * @param string $single |
|
143 | - * @param string $plural |
|
144 | - * @param int $number |
|
145 | - * @param string $context |
|
146 | - * @return string |
|
147 | - * @filter site-reviews/ngettext_with_context/site-reviews |
|
148 | - */ |
|
149 | - public function filterNgettextWithContextSiteReviews($translation, $single, $plural, $number, $context) |
|
150 | - { |
|
151 | - return $this->translator->translate($translation, Application::ID, [ |
|
152 | - 'context' => $context, |
|
153 | - 'number' => $number, |
|
154 | - 'plural' => $plural, |
|
155 | - 'single' => $single, |
|
156 | - ]); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param array $postStates |
|
161 | - * @param \WP_Post $post |
|
162 | - * @return array |
|
163 | - * @filter display_post_states |
|
164 | - */ |
|
165 | - public function filterPostStates($postStates, $post) |
|
166 | - { |
|
167 | - $postStates = Arr::consolidateArray($postStates); |
|
168 | - if (Application::POST_TYPE == Arr::get($post, 'post_type') && array_key_exists('pending', $postStates)) { |
|
169 | - $postStates['pending'] = __('Unapproved', 'site-reviews'); |
|
170 | - } |
|
171 | - return $postStates; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param string $translation |
|
176 | - * @param string $text |
|
177 | - * @return string |
|
178 | - * @filter site-reviews/gettext/default |
|
179 | - * @filter site-reviews/gettext_with_context/default |
|
180 | - */ |
|
181 | - public function filterPostStatusLabels($translation, $text) |
|
182 | - { |
|
183 | - return $this->canModifyTranslation() |
|
184 | - ? glsr(Labels::class)->filterPostStatusLabels($translation, $text) |
|
185 | - : $translation; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @param string $translation |
|
190 | - * @param string $single |
|
191 | - * @param string $plural |
|
192 | - * @param int $number |
|
193 | - * @return string |
|
194 | - * @filter site-reviews/ngettext/default |
|
195 | - */ |
|
196 | - public function filterPostStatusText($translation, $single, $plural, $number) |
|
197 | - { |
|
198 | - if ($this->canModifyTranslation()) { |
|
199 | - $strings = [ |
|
200 | - 'Published' => __('Approved', 'site-reviews'), |
|
201 | - 'Pending' => __('Unapproved', 'site-reviews'), |
|
202 | - ]; |
|
203 | - foreach ($strings as $search => $replace) { |
|
204 | - if (!Str::contains($single, $search)) { |
|
205 | - continue; |
|
206 | - } |
|
207 | - return $this->translator->getTranslation([ |
|
208 | - 'number' => $number, |
|
209 | - 'plural' => str_replace($search, $replace, $plural), |
|
210 | - 'single' => str_replace($search, $replace, $single), |
|
211 | - ]); |
|
212 | - } |
|
213 | - } |
|
214 | - return $translation; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * @return void |
|
219 | - * @action admin_enqueue_scripts |
|
220 | - */ |
|
221 | - public function translatePostStatusLabels() |
|
222 | - { |
|
223 | - if ($this->canModifyTranslation()) { |
|
224 | - glsr(Labels::class)->translatePostStatusLabels(); |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @return bool |
|
230 | - */ |
|
231 | - protected function canModifyTranslation() |
|
232 | - { |
|
233 | - $screen = glsr_current_screen(); |
|
234 | - return Application::POST_TYPE == $screen->post_type |
|
235 | - && in_array($screen->base, ['edit', 'post']); |
|
236 | - } |
|
13 | + /** |
|
14 | + * @var Translator |
|
15 | + */ |
|
16 | + public $translator; |
|
17 | + |
|
18 | + public function __construct(Translator $translator) |
|
19 | + { |
|
20 | + $this->translator = $translator; |
|
21 | + } |
|
22 | + |
|
23 | + /** |
|
24 | + * @param array $messages |
|
25 | + * @return array |
|
26 | + * @filter bulk_post_updated_messages |
|
27 | + */ |
|
28 | + public function filterBulkUpdateMessages($messages, array $counts) |
|
29 | + { |
|
30 | + $messages = Arr::consolidateArray($messages); |
|
31 | + $messages[Application::POST_TYPE] = [ |
|
32 | + 'updated' => _n('%s review updated.', '%s reviews updated.', $counts['updated'], 'site-reviews'), |
|
33 | + 'locked' => _n('%s review not updated, somebody is editing it.', '%s reviews not updated, somebody is editing them.', $counts['locked'], 'site-reviews'), |
|
34 | + 'deleted' => _n('%s review permanently deleted.', '%s reviews permanently deleted.', $counts['deleted'], 'site-reviews'), |
|
35 | + 'trashed' => _n('%s review moved to the Trash.', '%s reviews moved to the Trash.', $counts['trashed'], 'site-reviews'), |
|
36 | + 'untrashed' => _n('%s review restored from the Trash.', '%s reviews restored from the Trash.', $counts['untrashed'], 'site-reviews'), |
|
37 | + ]; |
|
38 | + return $messages; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @param string $translation |
|
43 | + * @param string $text |
|
44 | + * @param string $domain |
|
45 | + * @return string |
|
46 | + * @filter gettext |
|
47 | + */ |
|
48 | + public function filterGettext($translation, $text, $domain) |
|
49 | + { |
|
50 | + return apply_filters('site-reviews/gettext/'.$domain, $translation, $text); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param string $translation |
|
55 | + * @param string $text |
|
56 | + * @return string |
|
57 | + * @filter site-reviews/gettext/site-reviews |
|
58 | + */ |
|
59 | + public function filterGettextSiteReviews($translation, $text) |
|
60 | + { |
|
61 | + return $this->translator->translate($translation, Application::ID, [ |
|
62 | + 'single' => $text, |
|
63 | + ]); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @param string $translation |
|
68 | + * @param string $text |
|
69 | + * @param string $context |
|
70 | + * @param string $domain |
|
71 | + * @return string |
|
72 | + * @filter gettext_with_context |
|
73 | + */ |
|
74 | + public function filterGettextWithContext($translation, $text, $context, $domain) |
|
75 | + { |
|
76 | + return apply_filters('site-reviews/gettext_with_context/'.$domain, $translation, $text, $context); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param string $translation |
|
81 | + * @param string $text |
|
82 | + * @param string $context |
|
83 | + * @return string |
|
84 | + * @filter site-reviews/gettext_with_context/site-reviews |
|
85 | + */ |
|
86 | + public function filterGettextWithContextSiteReviews($translation, $text, $context) |
|
87 | + { |
|
88 | + return $this->translator->translate($translation, Application::ID, [ |
|
89 | + 'context' => $context, |
|
90 | + 'single' => $text, |
|
91 | + ]); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string $translation |
|
96 | + * @param string $single |
|
97 | + * @param string $plural |
|
98 | + * @param int $number |
|
99 | + * @param string $domain |
|
100 | + * @return string |
|
101 | + * @filter ngettext |
|
102 | + */ |
|
103 | + public function filterNgettext($translation, $single, $plural, $number, $domain) |
|
104 | + { |
|
105 | + return apply_filters('site-reviews/ngettext/'.$domain, $translation, $single, $plural, $number); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @param string $translation |
|
110 | + * @param string $single |
|
111 | + * @param string $plural |
|
112 | + * @param int $number |
|
113 | + * @return string |
|
114 | + * @filter site-reviews/ngettext/site-reviews |
|
115 | + */ |
|
116 | + public function filterNgettextSiteReviews($translation, $single, $plural, $number) |
|
117 | + { |
|
118 | + return $this->translator->translate($translation, Application::ID, [ |
|
119 | + 'number' => $number, |
|
120 | + 'plural' => $plural, |
|
121 | + 'single' => $single, |
|
122 | + ]); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $translation |
|
127 | + * @param string $single |
|
128 | + * @param string $plural |
|
129 | + * @param int $number |
|
130 | + * @param string $context |
|
131 | + * @param string $domain |
|
132 | + * @return string |
|
133 | + * @filter ngettext_with_context |
|
134 | + */ |
|
135 | + public function filterNgettextWithContext($translation, $single, $plural, $number, $context, $domain) |
|
136 | + { |
|
137 | + return apply_filters('site-reviews/ngettext_with_context/'.$domain, $translation, $single, $plural, $number, $context); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param string $translation |
|
142 | + * @param string $single |
|
143 | + * @param string $plural |
|
144 | + * @param int $number |
|
145 | + * @param string $context |
|
146 | + * @return string |
|
147 | + * @filter site-reviews/ngettext_with_context/site-reviews |
|
148 | + */ |
|
149 | + public function filterNgettextWithContextSiteReviews($translation, $single, $plural, $number, $context) |
|
150 | + { |
|
151 | + return $this->translator->translate($translation, Application::ID, [ |
|
152 | + 'context' => $context, |
|
153 | + 'number' => $number, |
|
154 | + 'plural' => $plural, |
|
155 | + 'single' => $single, |
|
156 | + ]); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param array $postStates |
|
161 | + * @param \WP_Post $post |
|
162 | + * @return array |
|
163 | + * @filter display_post_states |
|
164 | + */ |
|
165 | + public function filterPostStates($postStates, $post) |
|
166 | + { |
|
167 | + $postStates = Arr::consolidateArray($postStates); |
|
168 | + if (Application::POST_TYPE == Arr::get($post, 'post_type') && array_key_exists('pending', $postStates)) { |
|
169 | + $postStates['pending'] = __('Unapproved', 'site-reviews'); |
|
170 | + } |
|
171 | + return $postStates; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param string $translation |
|
176 | + * @param string $text |
|
177 | + * @return string |
|
178 | + * @filter site-reviews/gettext/default |
|
179 | + * @filter site-reviews/gettext_with_context/default |
|
180 | + */ |
|
181 | + public function filterPostStatusLabels($translation, $text) |
|
182 | + { |
|
183 | + return $this->canModifyTranslation() |
|
184 | + ? glsr(Labels::class)->filterPostStatusLabels($translation, $text) |
|
185 | + : $translation; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @param string $translation |
|
190 | + * @param string $single |
|
191 | + * @param string $plural |
|
192 | + * @param int $number |
|
193 | + * @return string |
|
194 | + * @filter site-reviews/ngettext/default |
|
195 | + */ |
|
196 | + public function filterPostStatusText($translation, $single, $plural, $number) |
|
197 | + { |
|
198 | + if ($this->canModifyTranslation()) { |
|
199 | + $strings = [ |
|
200 | + 'Published' => __('Approved', 'site-reviews'), |
|
201 | + 'Pending' => __('Unapproved', 'site-reviews'), |
|
202 | + ]; |
|
203 | + foreach ($strings as $search => $replace) { |
|
204 | + if (!Str::contains($single, $search)) { |
|
205 | + continue; |
|
206 | + } |
|
207 | + return $this->translator->getTranslation([ |
|
208 | + 'number' => $number, |
|
209 | + 'plural' => str_replace($search, $replace, $plural), |
|
210 | + 'single' => str_replace($search, $replace, $single), |
|
211 | + ]); |
|
212 | + } |
|
213 | + } |
|
214 | + return $translation; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * @return void |
|
219 | + * @action admin_enqueue_scripts |
|
220 | + */ |
|
221 | + public function translatePostStatusLabels() |
|
222 | + { |
|
223 | + if ($this->canModifyTranslation()) { |
|
224 | + glsr(Labels::class)->translatePostStatusLabels(); |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @return bool |
|
230 | + */ |
|
231 | + protected function canModifyTranslation() |
|
232 | + { |
|
233 | + $screen = glsr_current_screen(); |
|
234 | + return Application::POST_TYPE == $screen->post_type |
|
235 | + && in_array($screen->base, ['edit', 'post']); |
|
236 | + } |
|
237 | 237 | } |
@@ -10,52 +10,52 @@ |
||
10 | 10 | |
11 | 11 | class Pagination implements PartialContract |
12 | 12 | { |
13 | - /** |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - protected $args; |
|
13 | + /** |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + protected $args; |
|
17 | 17 | |
18 | - /** |
|
19 | - * @return void|string |
|
20 | - */ |
|
21 | - public function build(array $args = []) |
|
22 | - { |
|
23 | - $this->args = $this->normalize($args); |
|
24 | - if ($this->args['total'] < 2) { |
|
25 | - return; |
|
26 | - } |
|
27 | - return glsr(Template::class)->build('templates/pagination', [ |
|
28 | - 'context' => [ |
|
29 | - 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
30 | - 'loader' => '<div class="glsr-loader"></div>', |
|
31 | - 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
32 | - ], |
|
33 | - ]); |
|
34 | - } |
|
18 | + /** |
|
19 | + * @return void|string |
|
20 | + */ |
|
21 | + public function build(array $args = []) |
|
22 | + { |
|
23 | + $this->args = $this->normalize($args); |
|
24 | + if ($this->args['total'] < 2) { |
|
25 | + return; |
|
26 | + } |
|
27 | + return glsr(Template::class)->build('templates/pagination', [ |
|
28 | + 'context' => [ |
|
29 | + 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
30 | + 'loader' => '<div class="glsr-loader"></div>', |
|
31 | + 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
32 | + ], |
|
33 | + ]); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - protected function buildLinks() |
|
40 | - { |
|
41 | - $args = glsr(Style::class)->paginationArgs($this->args); |
|
42 | - if ('array' == $args['type']) { |
|
43 | - $args['type'] = 'plain'; |
|
44 | - } |
|
45 | - return paginate_links($args); |
|
46 | - } |
|
36 | + /** |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + protected function buildLinks() |
|
40 | + { |
|
41 | + $args = glsr(Style::class)->paginationArgs($this->args); |
|
42 | + if ('array' == $args['type']) { |
|
43 | + $args['type'] = 'plain'; |
|
44 | + } |
|
45 | + return paginate_links($args); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - protected function normalize(array $args) |
|
52 | - { |
|
53 | - if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
54 | - $args['base'] = $baseUrl.'%_%'; |
|
55 | - } |
|
56 | - return wp_parse_args(array_filter($args), [ |
|
57 | - 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
58 | - 'total' => 1, |
|
59 | - ]); |
|
60 | - } |
|
48 | + /** |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + protected function normalize(array $args) |
|
52 | + { |
|
53 | + if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
54 | + $args['base'] = $baseUrl.'%_%'; |
|
55 | + } |
|
56 | + return wp_parse_args(array_filter($args), [ |
|
57 | + 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
58 | + 'total' => 1, |
|
59 | + ]); |
|
60 | + } |
|
61 | 61 | } |