@@ -14,9 +14,9 @@ discard block |
||
| 14 | 14 | * @return array |
| 15 | 15 | * @filter allowed_block_types |
| 16 | 16 | */ |
| 17 | - public function filterAllowedBlockTypes($blockTypes, $post) |
|
| 17 | + public function filterAllowedBlockTypes( $blockTypes, $post ) |
|
| 18 | 18 | { |
| 19 | - return glsr()->post_type !== get_post_type($post) |
|
| 19 | + return glsr()->post_type !== get_post_type( $post ) |
|
| 20 | 20 | ? $blockTypes |
| 21 | 21 | : []; |
| 22 | 22 | } |
@@ -26,9 +26,9 @@ discard block |
||
| 26 | 26 | * @return array |
| 27 | 27 | * @filter block_categories |
| 28 | 28 | */ |
| 29 | - public function filterBlockCategories($categories) |
|
| 29 | + public function filterBlockCategories( $categories ) |
|
| 30 | 30 | { |
| 31 | - $categories = Arr::consolidate($categories); |
|
| 31 | + $categories = Arr::consolidate( $categories ); |
|
| 32 | 32 | $categories[] = [ |
| 33 | 33 | 'icon' => null, |
| 34 | 34 | 'slug' => Application::ID, |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * @filter classic_editor_enabled_editors_for_post_type |
| 45 | 45 | * @plugin classic-editor/classic-editor.php |
| 46 | 46 | */ |
| 47 | - public function filterEnabledEditors($editors, $postType) |
|
| 47 | + public function filterEnabledEditors( $editors, $postType ) |
|
| 48 | 48 | { |
| 49 | 49 | return Application::POST_TYPE == $postType |
| 50 | 50 | ? ['block_editor' => false, 'classic_editor' => false] |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * @return bool |
| 58 | 58 | * @filter use_block_editor_for_post_type |
| 59 | 59 | */ |
| 60 | - public function filterUseBlockEditor($bool, $postType) |
|
| 60 | + public function filterUseBlockEditor( $bool, $postType ) |
|
| 61 | 61 | { |
| 62 | 62 | return Application::POST_TYPE == $postType |
| 63 | 63 | ? false |
@@ -72,13 +72,13 @@ discard block |
||
| 72 | 72 | { |
| 73 | 73 | wp_register_style( |
| 74 | 74 | Application::ID.'/blocks', |
| 75 | - glsr()->url('assets/styles/'.Application::ID.'-blocks.css'), |
|
| 75 | + glsr()->url( 'assets/styles/'.Application::ID.'-blocks.css' ), |
|
| 76 | 76 | ['wp-edit-blocks'], |
| 77 | 77 | glsr()->version |
| 78 | 78 | ); |
| 79 | 79 | wp_register_script( |
| 80 | 80 | Application::ID.'/blocks', |
| 81 | - glsr()->url('assets/scripts/'.Application::ID.'-blocks.js'), |
|
| 81 | + glsr()->url( 'assets/scripts/'.Application::ID.'-blocks.js' ), |
|
| 82 | 82 | ['wp-api-fetch', 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-element', Application::ID.'/admin'], |
| 83 | 83 | glsr()->version |
| 84 | 84 | ); |
@@ -93,14 +93,14 @@ discard block |
||
| 93 | 93 | $blocks = [ |
| 94 | 94 | 'form', 'reviews', 'summary', |
| 95 | 95 | ]; |
| 96 | - foreach ($blocks as $block) { |
|
| 97 | - $id = str_replace('_reviews', '', Application::ID.'_'.$block); |
|
| 98 | - $blockClass = Helper::buildClassName($id.'-block', 'Blocks'); |
|
| 99 | - if (!class_exists($blockClass)) { |
|
| 100 | - glsr_log()->error(sprintf('Block class missing (%s)', $blockClass)); |
|
| 96 | + foreach( $blocks as $block ) { |
|
| 97 | + $id = str_replace( '_reviews', '', Application::ID.'_'.$block ); |
|
| 98 | + $blockClass = Helper::buildClassName( $id.'-block', 'Blocks' ); |
|
| 99 | + if( !class_exists( $blockClass ) ) { |
|
| 100 | + glsr_log()->error( sprintf( 'Block class missing (%s)', $blockClass ) ); |
|
| 101 | 101 | continue; |
| 102 | 102 | } |
| 103 | - glsr($blockClass)->register($block); |
|
| 103 | + glsr( $blockClass )->register( $block ); |
|
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | } |
@@ -26,11 +26,11 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | public function routerClearConsole() |
| 28 | 28 | { |
| 29 | - glsr(AdminController::class)->routerClearConsole(); |
|
| 30 | - wp_send_json_success([ |
|
| 31 | - 'console' => glsr(Console::class)->get(), |
|
| 32 | - 'notices' => glsr(Notice::class)->get(), |
|
| 33 | - ]); |
|
| 29 | + glsr( AdminController::class )->routerClearConsole(); |
|
| 30 | + wp_send_json_success( [ |
|
| 31 | + 'console' => glsr( Console::class )->get(), |
|
| 32 | + 'notices' => glsr( Notice::class )->get(), |
|
| 33 | + ] ); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -38,31 +38,31 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public function routerDetectIpAddress() |
| 40 | 40 | { |
| 41 | - glsr(AdminController::class)->routerDetectIpAddress(); |
|
| 42 | - wp_send_json_success([ |
|
| 43 | - 'notices' => glsr(Notice::class)->get(), |
|
| 44 | - ]); |
|
| 41 | + glsr( AdminController::class )->routerDetectIpAddress(); |
|
| 42 | + wp_send_json_success( [ |
|
| 43 | + 'notices' => glsr( Notice::class )->get(), |
|
| 44 | + ] ); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | 48 | * @return void |
| 49 | 49 | */ |
| 50 | - public function routerDismissNotice(Request $request) |
|
| 50 | + public function routerDismissNotice( Request $request ) |
|
| 51 | 51 | { |
| 52 | - glsr(NoticeController::class)->routerDismissNotice($request); |
|
| 52 | + glsr( NoticeController::class )->routerDismissNotice( $request ); |
|
| 53 | 53 | wp_send_json_success(); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
| 57 | 57 | * @return void |
| 58 | 58 | */ |
| 59 | - public function routerMceShortcode(Request $request) |
|
| 59 | + public function routerMceShortcode( Request $request ) |
|
| 60 | 60 | { |
| 61 | 61 | $shortcode = $request->shortcode; |
| 62 | 62 | $response = false; |
| 63 | - if ($data = glsr()->retrieve('mce.'.$shortcode, false)) { |
|
| 64 | - if (!empty($data['errors'])) { |
|
| 65 | - $data['btn_okay'] = [esc_attr_x('Okay', 'admin-text', 'site-reviews')]; |
|
| 63 | + if( $data = glsr()->retrieve( 'mce.'.$shortcode, false ) ) { |
|
| 64 | + if( !empty($data['errors']) ) { |
|
| 65 | + $data['btn_okay'] = [esc_attr_x( 'Okay', 'admin-text', 'site-reviews' )]; |
|
| 66 | 66 | } |
| 67 | 67 | $response = [ |
| 68 | 68 | 'body' => $data['fields'], |
@@ -72,18 +72,18 @@ discard block |
||
| 72 | 72 | 'title' => $data['title'], |
| 73 | 73 | ]; |
| 74 | 74 | } |
| 75 | - wp_send_json_success($response); |
|
| 75 | + wp_send_json_success( $response ); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * @return void |
| 80 | 80 | */ |
| 81 | - public function routerMigratePlugin(Request $request) |
|
| 81 | + public function routerMigratePlugin( Request $request ) |
|
| 82 | 82 | { |
| 83 | - glsr(AdminController::class)->routerMigratePlugin($request); |
|
| 84 | - wp_send_json_success([ |
|
| 85 | - 'notices' => glsr(Notice::class)->get(), |
|
| 86 | - ]); |
|
| 83 | + glsr( AdminController::class )->routerMigratePlugin( $request ); |
|
| 84 | + wp_send_json_success( [ |
|
| 85 | + 'notices' => glsr( Notice::class )->get(), |
|
| 86 | + ] ); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -91,37 +91,37 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function routerFetchConsole() |
| 93 | 93 | { |
| 94 | - glsr(AdminController::class)->routerFetchConsole(); |
|
| 95 | - wp_send_json_success([ |
|
| 96 | - 'console' => glsr(Console::class)->get(), |
|
| 97 | - 'notices' => glsr(Notice::class)->get(), |
|
| 98 | - ]); |
|
| 94 | + glsr( AdminController::class )->routerFetchConsole(); |
|
| 95 | + wp_send_json_success( [ |
|
| 96 | + 'console' => glsr( Console::class )->get(), |
|
| 97 | + 'notices' => glsr( Notice::class )->get(), |
|
| 98 | + ] ); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * @return void |
| 103 | 103 | */ |
| 104 | - public function routerFetchPagedReviews(Request $request) |
|
| 104 | + public function routerFetchPagedReviews( Request $request ) |
|
| 105 | 105 | { |
| 106 | 106 | $args = [ |
| 107 | - 'page' => $request->get('page', 0), |
|
| 107 | + 'page' => $request->get( 'page', 0 ), |
|
| 108 | 108 | 'pageUrl' => '', |
| 109 | 109 | 'pagination' => 'ajax', |
| 110 | 110 | 'schema' => false, |
| 111 | 111 | ]; |
| 112 | - if (!$args['page']) { |
|
| 113 | - $urlPath = Url::path($request->url); |
|
| 114 | - $args['page'] = glsr(Helper::class)->getPageNumber($request->url); |
|
| 115 | - $args['pageUrl'] = Url::path(home_url()) === $urlPath |
|
| 112 | + if( !$args['page'] ) { |
|
| 113 | + $urlPath = Url::path( $request->url ); |
|
| 114 | + $args['page'] = glsr( Helper::class )->getPageNumber( $request->url ); |
|
| 115 | + $args['pageUrl'] = Url::path( home_url() ) === $urlPath |
|
| 116 | 116 | ? Url::home() |
| 117 | - : Url::home($urlPath); |
|
| 117 | + : Url::home( $urlPath ); |
|
| 118 | 118 | } |
| 119 | - $atts = glsr(SiteReviewsShortcode::class)->normalizeAtts(Arr::consolidate($request->atts)); |
|
| 120 | - $html = glsr(SiteReviewsPartial::class)->build(wp_parse_args($args, $atts)); |
|
| 121 | - return wp_send_json_success([ |
|
| 119 | + $atts = glsr( SiteReviewsShortcode::class )->normalizeAtts( Arr::consolidate( $request->atts ) ); |
|
| 120 | + $html = glsr( SiteReviewsPartial::class )->build( wp_parse_args( $args, $atts ) ); |
|
| 121 | + return wp_send_json_success( [ |
|
| 122 | 122 | 'pagination' => $html->getPagination(), |
| 123 | 123 | 'reviews' => $html->getReviews(), |
| 124 | - ]); |
|
| 124 | + ] ); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
@@ -129,90 +129,90 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | public function routerResetPermissions() |
| 131 | 131 | { |
| 132 | - glsr(Role::class)->resetAll(); |
|
| 133 | - $reloadLink = glsr(Builder::class)->a([ |
|
| 134 | - 'text' => _x('reload the page', 'admin-text', 'site-reviews'), |
|
| 132 | + glsr( Role::class )->resetAll(); |
|
| 133 | + $reloadLink = glsr( Builder::class )->a( [ |
|
| 134 | + 'text' => _x( 'reload the page', 'admin-text', 'site-reviews' ), |
|
| 135 | 135 | 'href' => 'javascript:window.location.reload(1)', |
| 136 | - ]); |
|
| 137 | - glsr(Notice::class)->clear()->addSuccess( |
|
| 138 | - sprintf(_x('The permissions have been reset, please %s for them to take effect.', 'admin-text', 'site-reviews'), $reloadLink) |
|
| 136 | + ] ); |
|
| 137 | + glsr( Notice::class )->clear()->addSuccess( |
|
| 138 | + sprintf( _x( 'The permissions have been reset, please %s for them to take effect.', 'admin-text', 'site-reviews' ), $reloadLink ) |
|
| 139 | 139 | ); |
| 140 | - wp_send_json_success([ |
|
| 141 | - 'notices' => glsr(Notice::class)->get(), |
|
| 142 | - ]); |
|
| 140 | + wp_send_json_success( [ |
|
| 141 | + 'notices' => glsr( Notice::class )->get(), |
|
| 142 | + ] ); |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | 146 | * @return void |
| 147 | 147 | */ |
| 148 | - public function routerSearchPosts(Request $request) |
|
| 148 | + public function routerSearchPosts( Request $request ) |
|
| 149 | 149 | { |
| 150 | - $results = glsr(Database::class)->searchPosts($request->search); |
|
| 151 | - wp_send_json_success([ |
|
| 152 | - 'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
| 150 | + $results = glsr( Database::class )->searchPosts( $request->search ); |
|
| 151 | + wp_send_json_success( [ |
|
| 152 | + 'empty' => '<div>'._x( 'Nothing found.', 'admin-text', 'site-reviews' ).'</div>', |
|
| 153 | 153 | 'items' => $results, |
| 154 | - ]); |
|
| 154 | + ] ); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
| 158 | 158 | * @return void |
| 159 | 159 | */ |
| 160 | - public function routerSearchTranslations(Request $request) |
|
| 160 | + public function routerSearchTranslations( Request $request ) |
|
| 161 | 161 | { |
| 162 | - if (empty($request->exclude)) { |
|
| 162 | + if( empty($request->exclude) ) { |
|
| 163 | 163 | $request->exclude = []; |
| 164 | 164 | } |
| 165 | - $results = glsr(Translation::class) |
|
| 166 | - ->search($request->search) |
|
| 165 | + $results = glsr( Translation::class ) |
|
| 166 | + ->search( $request->search ) |
|
| 167 | 167 | ->exclude() |
| 168 | - ->exclude($request->exclude) |
|
| 168 | + ->exclude( $request->exclude ) |
|
| 169 | 169 | ->renderResults(); |
| 170 | - wp_send_json_success([ |
|
| 171 | - 'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
| 170 | + wp_send_json_success( [ |
|
| 171 | + 'empty' => '<div>'._x( 'Nothing found.', 'admin-text', 'site-reviews' ).'</div>', |
|
| 172 | 172 | 'items' => $results, |
| 173 | - ]); |
|
| 173 | + ] ); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
| 177 | 177 | * @return void |
| 178 | 178 | */ |
| 179 | - public function routerSubmitReview(Request $request) |
|
| 179 | + public function routerSubmitReview( Request $request ) |
|
| 180 | 180 | { |
| 181 | - $command = new CreateReview($request->toArray()); |
|
| 182 | - $review = $this->execute($command); |
|
| 181 | + $command = new CreateReview( $request->toArray() ); |
|
| 182 | + $review = $this->execute( $command ); |
|
| 183 | 183 | $data = [ |
| 184 | - 'errors' => glsr()->sessionGet($command->form_id.'errors', false), |
|
| 185 | - 'html' => (string) $review, |
|
| 186 | - 'message' => glsr()->sessionGet($command->form_id.'message', ''), |
|
| 187 | - 'recaptcha' => glsr()->sessionGet($command->form_id.'recaptcha', false), |
|
| 184 | + 'errors' => glsr()->sessionGet( $command->form_id.'errors', false ), |
|
| 185 | + 'html' => (string)$review, |
|
| 186 | + 'message' => glsr()->sessionGet( $command->form_id.'message', '' ), |
|
| 187 | + 'recaptcha' => glsr()->sessionGet( $command->form_id.'recaptcha', false ), |
|
| 188 | 188 | 'redirect' => $command->redirect(), |
| 189 | - 'review' => (array) $review, |
|
| 189 | + 'review' => (array)$review, |
|
| 190 | 190 | ]; |
| 191 | - if (false === $data['errors']) { |
|
| 191 | + if( false === $data['errors'] ) { |
|
| 192 | 192 | glsr()->sessionClear(); |
| 193 | - wp_send_json_success($data); |
|
| 193 | + wp_send_json_success( $data ); |
|
| 194 | 194 | } |
| 195 | - wp_send_json_error($data); |
|
| 195 | + wp_send_json_error( $data ); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
| 199 | 199 | * @return void |
| 200 | 200 | */ |
| 201 | - public function routerTogglePinned(Request $request) |
|
| 201 | + public function routerTogglePinned( Request $request ) |
|
| 202 | 202 | { |
| 203 | - wp_send_json_success([ |
|
| 204 | - 'notices' => glsr(Notice::class)->get(), |
|
| 205 | - 'pinned' => $this->execute(new TogglePinned($request->toArray())), |
|
| 206 | - ]); |
|
| 203 | + wp_send_json_success( [ |
|
| 204 | + 'notices' => glsr( Notice::class )->get(), |
|
| 205 | + 'pinned' => $this->execute( new TogglePinned( $request->toArray() ) ), |
|
| 206 | + ] ); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
| 210 | 210 | * @return void |
| 211 | 211 | */ |
| 212 | - public function routerToggleStatus(Request $request) |
|
| 212 | + public function routerToggleStatus( Request $request ) |
|
| 213 | 213 | { |
| 214 | 214 | wp_send_json_success( |
| 215 | - $this->execute(new ToggleStatus($request->post_id, $request->status)) |
|
| 215 | + $this->execute( new ToggleStatus( $request->post_id, $request->status ) ) |
|
| 216 | 216 | ); |
| 217 | 217 | } |
| 218 | 218 | } |
@@ -35,19 +35,19 @@ discard block |
||
| 35 | 35 | /** |
| 36 | 36 | * @return array |
| 37 | 37 | */ |
| 38 | - public function build(array $args = [], $reviews = []) |
|
| 38 | + public function build( array $args = [], $reviews = [] ) |
|
| 39 | 39 | { |
| 40 | 40 | $this->args = $args; |
| 41 | 41 | $this->reviews = $reviews; |
| 42 | - $schema = $this->buildSummary($args); |
|
| 43 | - if (!empty($schema)) { |
|
| 42 | + $schema = $this->buildSummary( $args ); |
|
| 43 | + if( !empty($schema) ) { |
|
| 44 | 44 | $reviews = $this->buildReviews(); |
| 45 | - foreach ($reviews as &$review) { |
|
| 45 | + foreach( $reviews as &$review ) { |
|
| 46 | 46 | unset($review['@context']); |
| 47 | 47 | unset($review['itemReviewed']); |
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | - if (!empty($reviews)) { |
|
| 50 | + if( !empty($reviews) ) { |
|
| 51 | 51 | $schema['review'] = $reviews; |
| 52 | 52 | } |
| 53 | 53 | return $schema; |
@@ -57,25 +57,25 @@ discard block |
||
| 57 | 57 | * @param array|null $args |
| 58 | 58 | * @return array |
| 59 | 59 | */ |
| 60 | - public function buildSummary($args = null, array $ratings = []) |
|
| 60 | + public function buildSummary( $args = null, array $ratings = [] ) |
|
| 61 | 61 | { |
| 62 | - if (is_array($args)) { |
|
| 62 | + if( is_array( $args ) ) { |
|
| 63 | 63 | $this->args = $args; |
| 64 | 64 | } |
| 65 | - $buildSummary = Helper::buildMethodName($this->getSchemaOptionValue('type'), 'buildSummaryFor'); |
|
| 66 | - if ($count = array_sum($this->getRatingCounts($ratings))) { |
|
| 67 | - $schema = method_exists($this, $buildSummary) |
|
| 65 | + $buildSummary = Helper::buildMethodName( $this->getSchemaOptionValue( 'type' ), 'buildSummaryFor' ); |
|
| 66 | + if( $count = array_sum( $this->getRatingCounts( $ratings ) ) ) { |
|
| 67 | + $schema = method_exists( $this, $buildSummary ) |
|
| 68 | 68 | ? $this->$buildSummary() |
| 69 | 69 | : $this->buildSummaryForCustom(); |
| 70 | 70 | $schema->aggregateRating( |
| 71 | - $this->getSchemaType('AggregateRating') |
|
| 72 | - ->ratingValue($this->getRatingValue()) |
|
| 73 | - ->reviewCount($count) |
|
| 74 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
| 75 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
| 71 | + $this->getSchemaType( 'AggregateRating' ) |
|
| 72 | + ->ratingValue( $this->getRatingValue() ) |
|
| 73 | + ->reviewCount( $count ) |
|
| 74 | + ->bestRating( glsr()->constant( 'MAX_RATING', Rating::class ) ) |
|
| 75 | + ->worstRating( glsr()->constant( 'MIN_RATING', Rating::class ) ) |
|
| 76 | 76 | ); |
| 77 | 77 | $schema = $schema->toArray(); |
| 78 | - return glsr()->filterArray('schema/'.$schema['@type'], $schema, $args); |
|
| 78 | + return glsr()->filterArray( 'schema/'.$schema['@type'], $schema, $args ); |
|
| 79 | 79 | } |
| 80 | 80 | return []; |
| 81 | 81 | } |
@@ -85,54 +85,54 @@ discard block |
||
| 85 | 85 | */ |
| 86 | 86 | public function render() |
| 87 | 87 | { |
| 88 | - if (empty($schemas = glsr()->retrieve('schemas', []))) { |
|
| 88 | + if( empty($schemas = glsr()->retrieve( 'schemas', [] )) ) { |
|
| 89 | 89 | return; |
| 90 | 90 | } |
| 91 | - printf('<script type="application/ld+json">%s</script>', json_encode( |
|
| 92 | - glsr()->filterArray('schema/all', $schemas), |
|
| 91 | + printf( '<script type="application/ld+json">%s</script>', json_encode( |
|
| 92 | + glsr()->filterArray( 'schema/all', $schemas ), |
|
| 93 | 93 | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
| 94 | - )); |
|
| 94 | + ) ); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | - public function store(array $schema) |
|
| 100 | + public function store( array $schema ) |
|
| 101 | 101 | { |
| 102 | - if (empty($schema)) { |
|
| 102 | + if( empty($schema) ) { |
|
| 103 | 103 | return; |
| 104 | 104 | } |
| 105 | - $schemas = Arr::consolidate(glsr()->retrieve('schemas')); |
|
| 105 | + $schemas = Arr::consolidate( glsr()->retrieve( 'schemas' ) ); |
|
| 106 | 106 | $schemas[] = $schema; |
| 107 | - $schemas = array_map('unserialize', array_unique(array_map('serialize', $schemas))); |
|
| 108 | - glsr()->store('schemas', $schemas); |
|
| 107 | + $schemas = array_map( 'unserialize', array_unique( array_map( 'serialize', $schemas ) ) ); |
|
| 108 | + glsr()->store( 'schemas', $schemas ); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
| 112 | 112 | * @param Review $review |
| 113 | 113 | * @return array |
| 114 | 114 | */ |
| 115 | - protected function buildReview($review) |
|
| 115 | + protected function buildReview( $review ) |
|
| 116 | 116 | { |
| 117 | - $schema = $this->getSchemaType('Review') |
|
| 118 | - ->doIf(!in_array('title', $this->args['hide']), function ($schema) use ($review) { |
|
| 119 | - $schema->name($review->title); |
|
| 117 | + $schema = $this->getSchemaType( 'Review' ) |
|
| 118 | + ->doIf( !in_array( 'title', $this->args['hide'] ), function( $schema ) use ($review) { |
|
| 119 | + $schema->name( $review->title ); |
|
| 120 | 120 | }) |
| 121 | - ->doIf(!in_array('excerpt', $this->args['hide']), function ($schema) use ($review) { |
|
| 122 | - $schema->reviewBody($review->content); |
|
| 121 | + ->doIf( !in_array( 'excerpt', $this->args['hide'] ), function( $schema ) use ($review) { |
|
| 122 | + $schema->reviewBody( $review->content ); |
|
| 123 | 123 | }) |
| 124 | - ->datePublished((new DateTime($review->date))) |
|
| 125 | - ->author($this->getSchemaType('Person')->name($review->author)) |
|
| 126 | - ->itemReviewed($this->getSchemaType()->name($this->getSchemaOptionValue('name'))); |
|
| 127 | - if (!empty($review->rating)) { |
|
| 124 | + ->datePublished( (new DateTime( $review->date )) ) |
|
| 125 | + ->author( $this->getSchemaType( 'Person' )->name( $review->author ) ) |
|
| 126 | + ->itemReviewed( $this->getSchemaType()->name( $this->getSchemaOptionValue( 'name' ) ) ); |
|
| 127 | + if( !empty($review->rating) ) { |
|
| 128 | 128 | $schema->reviewRating( |
| 129 | - $this->getSchemaType('Rating') |
|
| 130 | - ->ratingValue($review->rating) |
|
| 131 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
| 132 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
| 129 | + $this->getSchemaType( 'Rating' ) |
|
| 130 | + ->ratingValue( $review->rating ) |
|
| 131 | + ->bestRating( glsr()->constant( 'MAX_RATING', Rating::class ) ) |
|
| 132 | + ->worstRating( glsr()->constant( 'MIN_RATING', Rating::class ) ) |
|
| 133 | 133 | ); |
| 134 | 134 | } |
| 135 | - return glsr()->filterArray('schema/review', $schema->toArray(), $review, $this->args); |
|
| 135 | + return glsr()->filterArray( 'schema/review', $schema->toArray(), $review, $this->args ); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
@@ -141,11 +141,11 @@ discard block |
||
| 141 | 141 | protected function buildReviews() |
| 142 | 142 | { |
| 143 | 143 | $reviews = []; |
| 144 | - foreach ($this->reviews as $review) { |
|
| 144 | + foreach( $this->reviews as $review ) { |
|
| 145 | 145 | // Only include critic reviews that have been directly produced by your site, not reviews from third-party sites or syndicated reviews. |
| 146 | 146 | // @see https://developers.google.com/search/docs/data-types/review |
| 147 | - if ('local' === $review->review_type) { |
|
| 148 | - $reviews[] = $this->buildReview($review); |
|
| 147 | + if( 'local' === $review->review_type ) { |
|
| 148 | + $reviews[] = $this->buildReview( $review ); |
|
| 149 | 149 | } |
| 150 | 150 | } |
| 151 | 151 | return $reviews; |
@@ -155,14 +155,14 @@ discard block |
||
| 155 | 155 | * @param mixed $schema |
| 156 | 156 | * @return mixed |
| 157 | 157 | */ |
| 158 | - protected function buildSchemaValues($schema, array $values = []) |
|
| 158 | + protected function buildSchemaValues( $schema, array $values = [] ) |
|
| 159 | 159 | { |
| 160 | - foreach ($values as $value) { |
|
| 161 | - $option = $this->getSchemaOptionValue($value); |
|
| 162 | - if (empty($option)) { |
|
| 160 | + foreach( $values as $value ) { |
|
| 161 | + $option = $this->getSchemaOptionValue( $value ); |
|
| 162 | + if( empty($option) ) { |
|
| 163 | 163 | continue; |
| 164 | 164 | } |
| 165 | - $schema->$value($option); |
|
| 165 | + $schema->$value( $option ); |
|
| 166 | 166 | } |
| 167 | 167 | return $schema; |
| 168 | 168 | } |
@@ -172,9 +172,9 @@ discard block |
||
| 172 | 172 | */ |
| 173 | 173 | protected function buildSummaryForCustom() |
| 174 | 174 | { |
| 175 | - return $this->buildSchemaValues($this->getSchemaType(), [ |
|
| 175 | + return $this->buildSchemaValues( $this->getSchemaType(), [ |
|
| 176 | 176 | 'description', 'image', 'name', 'url', |
| 177 | - ]); |
|
| 177 | + ] ); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
@@ -182,9 +182,9 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | protected function buildSummaryForLocalBusiness() |
| 184 | 184 | { |
| 185 | - return $this->buildSchemaValues($this->buildSummaryForCustom(), [ |
|
| 185 | + return $this->buildSchemaValues( $this->buildSummaryForCustom(), [ |
|
| 186 | 186 | 'address', 'priceRange', 'telephone', |
| 187 | - ]); |
|
| 187 | + ] ); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
@@ -192,25 +192,25 @@ discard block |
||
| 192 | 192 | */ |
| 193 | 193 | protected function buildSummaryForProduct() |
| 194 | 194 | { |
| 195 | - $offerType = $this->getSchemaOption('offerType', 'AggregateOffer'); |
|
| 196 | - $offers = $this->buildSchemaValues($this->getSchemaType($offerType), [ |
|
| 195 | + $offerType = $this->getSchemaOption( 'offerType', 'AggregateOffer' ); |
|
| 196 | + $offers = $this->buildSchemaValues( $this->getSchemaType( $offerType ), [ |
|
| 197 | 197 | 'highPrice', 'lowPrice', 'price', 'priceCurrency', |
| 198 | - ]); |
|
| 198 | + ] ); |
|
| 199 | 199 | return $this->buildSummaryForCustom() |
| 200 | - ->doIf(!empty($offers->getProperties()), function ($schema) use ($offers) { |
|
| 201 | - $schema->offers($offers); |
|
| 200 | + ->doIf( !empty($offers->getProperties()), function( $schema ) use ($offers) { |
|
| 201 | + $schema->offers( $offers ); |
|
| 202 | 202 | }) |
| 203 | - ->setProperty('@id', $this->getSchemaOptionValue('url').'#product'); |
|
| 203 | + ->setProperty( '@id', $this->getSchemaOptionValue( 'url' ).'#product' ); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /** |
| 207 | 207 | * @return array |
| 208 | 208 | */ |
| 209 | - protected function getRatingCounts(array $ratings = []) |
|
| 209 | + protected function getRatingCounts( array $ratings = [] ) |
|
| 210 | 210 | { |
| 211 | - if (!isset($this->ratingCounts)) { |
|
| 211 | + if( !isset($this->ratingCounts) ) { |
|
| 212 | 212 | $this->ratingCounts = empty($ratings) |
| 213 | - ? glsr(RatingManager::class)->ratings($this->args) |
|
| 213 | + ? glsr( RatingManager::class )->ratings( $this->args ) |
|
| 214 | 214 | : $ratings; |
| 215 | 215 | } |
| 216 | 216 | return $this->ratingCounts; |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | */ |
| 222 | 222 | protected function getRatingValue() |
| 223 | 223 | { |
| 224 | - return glsr(Rating::class)->average($this->getRatingCounts()); |
|
| 224 | + return glsr( Rating::class )->average( $this->getRatingCounts() ); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
@@ -229,15 +229,15 @@ discard block |
||
| 229 | 229 | * @param string $fallback |
| 230 | 230 | * @return string |
| 231 | 231 | */ |
| 232 | - protected function getSchemaOption($option, $fallback) |
|
| 232 | + protected function getSchemaOption( $option, $fallback ) |
|
| 233 | 233 | { |
| 234 | - $option = strtolower($option); |
|
| 235 | - if ($schemaOption = trim((string) get_post_meta(intval(get_the_ID()), 'schema_'.$option, true))) { |
|
| 234 | + $option = strtolower( $option ); |
|
| 235 | + if( $schemaOption = trim( (string)get_post_meta( intval( get_the_ID() ), 'schema_'.$option, true ) ) ) { |
|
| 236 | 236 | return $schemaOption; |
| 237 | 237 | } |
| 238 | - $setting = glsr(OptionManager::class)->get('settings.schema.'.$option); |
|
| 239 | - if (is_array($setting)) { |
|
| 240 | - return $this->getSchemaOptionDefault($setting, $fallback); |
|
| 238 | + $setting = glsr( OptionManager::class )->get( 'settings.schema.'.$option ); |
|
| 239 | + if( is_array( $setting ) ) { |
|
| 240 | + return $this->getSchemaOptionDefault( $setting, $fallback ); |
|
| 241 | 241 | } |
| 242 | 242 | return !empty($setting) |
| 243 | 243 | ? $setting |
@@ -248,12 +248,12 @@ discard block |
||
| 248 | 248 | * @param string $fallback |
| 249 | 249 | * @return string |
| 250 | 250 | */ |
| 251 | - protected function getSchemaOptionDefault(array $setting, $fallback) |
|
| 251 | + protected function getSchemaOptionDefault( array $setting, $fallback ) |
|
| 252 | 252 | { |
| 253 | - $setting = wp_parse_args($setting, [ |
|
| 253 | + $setting = wp_parse_args( $setting, [ |
|
| 254 | 254 | 'custom' => '', |
| 255 | 255 | 'default' => $fallback, |
| 256 | - ]); |
|
| 256 | + ] ); |
|
| 257 | 257 | return 'custom' != $setting['default'] |
| 258 | 258 | ? $setting['default'] |
| 259 | 259 | : $setting['custom']; |
@@ -264,21 +264,21 @@ discard block |
||
| 264 | 264 | * @param string $fallback |
| 265 | 265 | * @return void|string |
| 266 | 266 | */ |
| 267 | - protected function getSchemaOptionValue($option, $fallback = 'post') |
|
| 267 | + protected function getSchemaOptionValue( $option, $fallback = 'post' ) |
|
| 268 | 268 | { |
| 269 | - if (array_key_exists($option, $this->keyValues)) { |
|
| 269 | + if( array_key_exists( $option, $this->keyValues ) ) { |
|
| 270 | 270 | return $this->keyValues[$option]; |
| 271 | 271 | } |
| 272 | - $value = $this->getSchemaOption($option, $fallback); |
|
| 273 | - if ($value != $fallback) { |
|
| 274 | - return $this->setAndGetKeyValue($option, $value); |
|
| 272 | + $value = $this->getSchemaOption( $option, $fallback ); |
|
| 273 | + if( $value != $fallback ) { |
|
| 274 | + return $this->setAndGetKeyValue( $option, $value ); |
|
| 275 | 275 | } |
| 276 | - if (!is_single() && !is_page()) { |
|
| 276 | + if( !is_single() && !is_page() ) { |
|
| 277 | 277 | return; |
| 278 | 278 | } |
| 279 | - $method = Helper::buildMethodName($option, 'getThing'); |
|
| 280 | - if (method_exists($this, $method)) { |
|
| 281 | - return $this->setAndGetKeyValue($option, $this->$method()); |
|
| 279 | + $method = Helper::buildMethodName( $option, 'getThing' ); |
|
| 280 | + if( method_exists( $this, $method ) ) { |
|
| 281 | + return $this->setAndGetKeyValue( $option, $this->$method() ); |
|
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
@@ -286,15 +286,15 @@ discard block |
||
| 286 | 286 | * @param string|null $type |
| 287 | 287 | * @return mixed |
| 288 | 288 | */ |
| 289 | - protected function getSchemaType($type = null) |
|
| 289 | + protected function getSchemaType( $type = null ) |
|
| 290 | 290 | { |
| 291 | - if (!is_string($type)) { |
|
| 292 | - $type = $this->getSchemaOption('type', 'LocalBusiness'); |
|
| 291 | + if( !is_string( $type ) ) { |
|
| 292 | + $type = $this->getSchemaOption( 'type', 'LocalBusiness' ); |
|
| 293 | 293 | } |
| 294 | - $className = Helper::buildClassName($type, 'Modules\Schema'); |
|
| 295 | - return class_exists($className) |
|
| 294 | + $className = Helper::buildClassName( $type, 'Modules\Schema' ); |
|
| 295 | + return class_exists( $className ) |
|
| 296 | 296 | ? new $className() |
| 297 | - : new UnknownType($type); |
|
| 297 | + : new UnknownType( $type ); |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | /** |
@@ -303,19 +303,19 @@ discard block |
||
| 303 | 303 | protected function getThingDescription() |
| 304 | 304 | { |
| 305 | 305 | $post = get_post(); |
| 306 | - $text = Arr::get($post, 'post_excerpt'); |
|
| 307 | - if (empty($text)) { |
|
| 308 | - $text = Arr::get($post, 'post_content'); |
|
| 306 | + $text = Arr::get( $post, 'post_excerpt' ); |
|
| 307 | + if( empty($text) ) { |
|
| 308 | + $text = Arr::get( $post, 'post_content' ); |
|
| 309 | 309 | } |
| 310 | - if (function_exists('excerpt_remove_blocks')) { |
|
| 311 | - $text = excerpt_remove_blocks($text); |
|
| 310 | + if( function_exists( 'excerpt_remove_blocks' ) ) { |
|
| 311 | + $text = excerpt_remove_blocks( $text ); |
|
| 312 | 312 | } |
| 313 | - $text = strip_shortcodes($text); |
|
| 314 | - $text = wpautop($text); |
|
| 315 | - $text = wptexturize($text); |
|
| 316 | - $text = wp_strip_all_tags($text); |
|
| 317 | - $text = str_replace(']]>', ']]>', $text); |
|
| 318 | - return wp_trim_words($text, apply_filters('excerpt_length', 55)); |
|
| 313 | + $text = strip_shortcodes( $text ); |
|
| 314 | + $text = wpautop( $text ); |
|
| 315 | + $text = wptexturize( $text ); |
|
| 316 | + $text = wp_strip_all_tags( $text ); |
|
| 317 | + $text = str_replace( ']]>', ']]>', $text ); |
|
| 318 | + return wp_trim_words( $text, apply_filters( 'excerpt_length', 55 ) ); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | /** |
@@ -323,7 +323,7 @@ discard block |
||
| 323 | 323 | */ |
| 324 | 324 | protected function getThingImage() |
| 325 | 325 | { |
| 326 | - return (string) get_the_post_thumbnail_url(null, 'large'); |
|
| 326 | + return (string)get_the_post_thumbnail_url( null, 'large' ); |
|
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | /** |
@@ -339,7 +339,7 @@ discard block |
||
| 339 | 339 | */ |
| 340 | 340 | protected function getThingUrl() |
| 341 | 341 | { |
| 342 | - return (string) get_the_permalink(); |
|
| 342 | + return (string)get_the_permalink(); |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | /** |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | * @param string $value |
| 348 | 348 | * @return string |
| 349 | 349 | */ |
| 350 | - protected function setAndGetKeyValue($option, $value) |
|
| 350 | + protected function setAndGetKeyValue( $option, $value ) |
|
| 351 | 351 | { |
| 352 | 352 | $this->keyValues[$option] = $value; |
| 353 | 353 | return $value; |
@@ -21,13 +21,13 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Console |
| 23 | 23 | { |
| 24 | - const DEBUG = 0; // Detailed debug information |
|
| 25 | - const INFO = 1; // Interesting events |
|
| 26 | - const NOTICE = 2; // Normal but significant events |
|
| 27 | - const WARNING = 4; // Exceptional occurrences that are not errors |
|
| 28 | - const ERROR = 8; // Runtime errors that do not require immediate action |
|
| 29 | - const CRITICAL = 16; // Critical conditions |
|
| 30 | - const ALERT = 32; // Action must be taken immediately |
|
| 24 | + const DEBUG = 0; // Detailed debug information |
|
| 25 | + const INFO = 1; // Interesting events |
|
| 26 | + const NOTICE = 2; // Normal but significant events |
|
| 27 | + const WARNING = 4; // Exceptional occurrences that are not errors |
|
| 28 | + const ERROR = 8; // Runtime errors that do not require immediate action |
|
| 29 | + const CRITICAL = 16; // Critical conditions |
|
| 30 | + const ALERT = 32; // Action must be taken immediately |
|
| 31 | 31 | const EMERGENCY = 64; // System is unusable |
| 32 | 32 | |
| 33 | 33 | protected $file; |
@@ -40,14 +40,14 @@ discard block |
||
| 40 | 40 | $this->reset(); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function __call($method, $args) |
|
| 43 | + public function __call( $method, $args ) |
|
| 44 | 44 | { |
| 45 | - $constant = 'static::'.strtoupper($method); |
|
| 46 | - if (defined($constant)) { |
|
| 47 | - $args = Arr::prepend($args, constant($constant)); |
|
| 48 | - return call_user_func_array([$this, 'log'], array_slice($args, 0, 3)); |
|
| 45 | + $constant = 'static::'.strtoupper( $method ); |
|
| 46 | + if( defined( $constant ) ) { |
|
| 47 | + $args = Arr::prepend( $args, constant( $constant ) ); |
|
| 48 | + return call_user_func_array( [$this, 'log'], array_slice( $args, 0, 3 ) ); |
|
| 49 | 49 | } |
| 50 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
| 50 | + throw new BadMethodCallException( "Method [$method] does not exist." ); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | public function clear() |
| 65 | 65 | { |
| 66 | 66 | $this->log = ''; |
| 67 | - file_put_contents($this->file, $this->log); |
|
| 67 | + file_put_contents( $this->file, $this->log ); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | public function get() |
| 74 | 74 | { |
| 75 | 75 | return empty($this->log) |
| 76 | - ? _x('Console is empty', 'admin-text', 'site-reviews') |
|
| 76 | + ? _x( 'Console is empty', 'admin-text', 'site-reviews' ) |
|
| 77 | 77 | : $this->log; |
| 78 | 78 | } |
| 79 | 79 | |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | public function getLevel() |
| 84 | 84 | { |
| 85 | - return glsr()->filterInt('console/level', static::INFO); |
|
| 85 | + return glsr()->filterInt( 'console/level', static::INFO ); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | public function getLevels() |
| 92 | 92 | { |
| 93 | - $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
| 94 | - return array_map('strtolower', array_flip($constants)); |
|
| 93 | + $constants = (new ReflectionClass( __CLASS__ ))->getConstants(); |
|
| 94 | + return array_map( 'strtolower', array_flip( $constants ) ); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -100,21 +100,21 @@ discard block |
||
| 100 | 100 | public function humanLevel() |
| 101 | 101 | { |
| 102 | 102 | $level = $this->getLevel(); |
| 103 | - return sprintf('%s (%d)', strtoupper(Arr::get($this->getLevels(), $level, 'unknown')), $level); |
|
| 103 | + return sprintf( '%s (%d)', strtoupper( Arr::get( $this->getLevels(), $level, 'unknown' ) ), $level ); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * @param string|null $valueIfEmpty |
| 108 | 108 | * @return string |
| 109 | 109 | */ |
| 110 | - public function humanSize($valueIfEmpty = null) |
|
| 110 | + public function humanSize( $valueIfEmpty = null ) |
|
| 111 | 111 | { |
| 112 | 112 | $bytes = $this->size(); |
| 113 | - if (empty($bytes) && is_string($valueIfEmpty)) { |
|
| 113 | + if( empty($bytes) && is_string( $valueIfEmpty ) ) { |
|
| 114 | 114 | return $valueIfEmpty; |
| 115 | 115 | } |
| 116 | - $exponent = floor(log(max($bytes, 1), 1024)); |
|
| 117 | - return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
| 116 | + $exponent = floor( log( max( $bytes, 1 ), 1024 ) ); |
|
| 117 | + return round( $bytes / pow( 1024, $exponent ), 2 ).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -124,19 +124,19 @@ discard block |
||
| 124 | 124 | * @param string $backtraceLine |
| 125 | 125 | * @return static |
| 126 | 126 | */ |
| 127 | - public function log($level, $message, $context = [], $backtraceLine = '') |
|
| 127 | + public function log( $level, $message, $context = [], $backtraceLine = '' ) |
|
| 128 | 128 | { |
| 129 | - if (empty($backtraceLine)) { |
|
| 129 | + if( empty($backtraceLine) ) { |
|
| 130 | 130 | $backtraceLine = $this->getBacktraceLine(); |
| 131 | 131 | } |
| 132 | - if ($this->canLogEntry($level, $backtraceLine)) { |
|
| 133 | - $levelName = Arr::get($this->getLevels(), $level); |
|
| 134 | - $context = Arr::consolidate($context); |
|
| 135 | - $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
| 136 | - $message = $this->interpolate($message, $context); |
|
| 137 | - $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
| 138 | - file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
| 139 | - apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
| 132 | + if( $this->canLogEntry( $level, $backtraceLine ) ) { |
|
| 133 | + $levelName = Arr::get( $this->getLevels(), $level ); |
|
| 134 | + $context = Arr::consolidate( $context ); |
|
| 135 | + $backtraceLine = $this->normalizeBacktraceLine( $backtraceLine ); |
|
| 136 | + $message = $this->interpolate( $message, $context ); |
|
| 137 | + $entry = $this->buildLogEntry( $levelName, $message, $backtraceLine ); |
|
| 138 | + file_put_contents( $this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX ); |
|
| 139 | + apply_filters( 'console', $message, $levelName, $backtraceLine ); // Show in Blackbar plugin if installed |
|
| 140 | 140 | $this->reset(); |
| 141 | 141 | } |
| 142 | 142 | return $this; |
@@ -147,17 +147,17 @@ discard block |
||
| 147 | 147 | */ |
| 148 | 148 | public function logOnce() |
| 149 | 149 | { |
| 150 | - $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
| 150 | + $once = Arr::consolidate( glsr()->{$this->logOnceKey}); |
|
| 151 | 151 | $levels = $this->getLevels(); |
| 152 | - foreach ($once as $entry) { |
|
| 153 | - $levelName = Arr::get($entry, 'level'); |
|
| 154 | - if (!in_array($levelName, $levels)) { |
|
| 152 | + foreach( $once as $entry ) { |
|
| 153 | + $levelName = Arr::get( $entry, 'level' ); |
|
| 154 | + if( !in_array( $levelName, $levels ) ) { |
|
| 155 | 155 | continue; |
| 156 | 156 | } |
| 157 | - $level = Arr::get(array_flip($levels), $levelName); |
|
| 158 | - $message = Arr::get($entry, 'message'); |
|
| 159 | - $backtraceLine = Arr::get($entry, 'backtrace'); |
|
| 160 | - $this->log($level, $message, [], $backtraceLine); |
|
| 157 | + $level = Arr::get( array_flip( $levels ), $levelName ); |
|
| 158 | + $message = Arr::get( $entry, 'message' ); |
|
| 159 | + $backtraceLine = Arr::get( $entry, 'backtrace' ); |
|
| 160 | + $this->log( $level, $message, [], $backtraceLine ); |
|
| 161 | 161 | } |
| 162 | 162 | glsr()->{$this->logOnceKey} = []; |
| 163 | 163 | } |
@@ -168,21 +168,21 @@ discard block |
||
| 168 | 168 | * @param mixed $data |
| 169 | 169 | * @return void |
| 170 | 170 | */ |
| 171 | - public function once($levelName, $handle, $data) |
|
| 171 | + public function once( $levelName, $handle, $data ) |
|
| 172 | 172 | { |
| 173 | - $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
| 174 | - $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
| 175 | - return Arr::get($entry, 'level') == $levelName |
|
| 176 | - && Arr::get($entry, 'handle') == $handle; |
|
| 173 | + $once = Arr::consolidate( glsr()->{$this->logOnceKey}); |
|
| 174 | + $filtered = array_filter( $once, function( $entry ) use ($levelName, $handle) { |
|
| 175 | + return Arr::get( $entry, 'level' ) == $levelName |
|
| 176 | + && Arr::get( $entry, 'handle' ) == $handle; |
|
| 177 | 177 | }); |
| 178 | - if (!empty($filtered)) { |
|
| 178 | + if( !empty($filtered) ) { |
|
| 179 | 179 | return; |
| 180 | 180 | } |
| 181 | 181 | $once[] = [ |
| 182 | - 'backtrace' => $this->getBacktraceLineFromData($data), |
|
| 182 | + 'backtrace' => $this->getBacktraceLineFromData( $data ), |
|
| 183 | 183 | 'handle' => $handle, |
| 184 | 184 | 'level' => $levelName, |
| 185 | - 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
| 185 | + 'message' => '[RECURRING] '.$this->getMessageFromData( $data ), |
|
| 186 | 186 | ]; |
| 187 | 187 | glsr()->{$this->logOnceKey} = $once; |
| 188 | 188 | } |
@@ -192,8 +192,8 @@ discard block |
||
| 192 | 192 | */ |
| 193 | 193 | public function size() |
| 194 | 194 | { |
| 195 | - return file_exists($this->file) |
|
| 196 | - ? filesize($this->file) |
|
| 195 | + return file_exists( $this->file ) |
|
| 196 | + ? filesize( $this->file ) |
|
| 197 | 197 | : 0; |
| 198 | 198 | } |
| 199 | 199 | |
@@ -202,11 +202,11 @@ discard block |
||
| 202 | 202 | * @param int $index |
| 203 | 203 | * @return string |
| 204 | 204 | */ |
| 205 | - protected function buildBacktraceLine($backtrace, $index) |
|
| 205 | + protected function buildBacktraceLine( $backtrace, $index ) |
|
| 206 | 206 | { |
| 207 | - return sprintf('%s:%s', |
|
| 208 | - Arr::get($backtrace, $index.'.file'), // realpath |
|
| 209 | - Arr::get($backtrace, $index.'.line') |
|
| 207 | + return sprintf( '%s:%s', |
|
| 208 | + Arr::get( $backtrace, $index.'.file' ), // realpath |
|
| 209 | + Arr::get( $backtrace, $index.'.line' ) |
|
| 210 | 210 | ); |
| 211 | 211 | } |
| 212 | 212 | |
@@ -216,11 +216,11 @@ discard block |
||
| 216 | 216 | * @param string $backtraceLine |
| 217 | 217 | * @return string |
| 218 | 218 | */ |
| 219 | - protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
| 219 | + protected function buildLogEntry( $levelName, $message, $backtraceLine = '' ) |
|
| 220 | 220 | { |
| 221 | - return sprintf('[%s] %s [%s] %s', |
|
| 222 | - current_time('mysql'), |
|
| 223 | - strtoupper($levelName), |
|
| 221 | + return sprintf( '[%s] %s [%s] %s', |
|
| 222 | + current_time( 'mysql' ), |
|
| 223 | + strtoupper( $levelName ), |
|
| 224 | 224 | $backtraceLine, |
| 225 | 225 | $message |
| 226 | 226 | ); |
@@ -230,10 +230,10 @@ discard block |
||
| 230 | 230 | * @param int $level |
| 231 | 231 | * @return bool |
| 232 | 232 | */ |
| 233 | - protected function canLogEntry($level, $backtraceLine) |
|
| 233 | + protected function canLogEntry( $level, $backtraceLine ) |
|
| 234 | 234 | { |
| 235 | - $levelExists = array_key_exists($level, $this->getLevels()); |
|
| 236 | - if (!Str::contains($backtraceLine, glsr()->path())) { |
|
| 235 | + $levelExists = array_key_exists( $level, $this->getLevels() ); |
|
| 236 | + if( !Str::contains( $backtraceLine, glsr()->path() ) ) { |
|
| 237 | 237 | return $levelExists; // ignore level restriction if triggered outside of the plugin |
| 238 | 238 | } |
| 239 | 239 | return $levelExists && $level >= $this->getLevel(); |
@@ -244,17 +244,17 @@ discard block |
||
| 244 | 244 | */ |
| 245 | 245 | protected function getBacktraceLine() |
| 246 | 246 | { |
| 247 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
| 248 | - $search = array_search('glsr_log', wp_list_pluck($backtrace, 'function')); |
|
| 249 | - if (false !== $search) { |
|
| 250 | - return $this->buildBacktraceLine($backtrace, (int) $search); |
|
| 247 | + $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 6 ); |
|
| 248 | + $search = array_search( 'glsr_log', wp_list_pluck( $backtrace, 'function' ) ); |
|
| 249 | + if( false !== $search ) { |
|
| 250 | + return $this->buildBacktraceLine( $backtrace, (int)$search ); |
|
| 251 | 251 | } |
| 252 | - $search = array_search('log', wp_list_pluck($backtrace, 'function')); |
|
| 253 | - if (false !== $search) { |
|
| 254 | - $index = '{closure}' == Arr::get($backtrace, ($search + 2).'.function') |
|
| 252 | + $search = array_search( 'log', wp_list_pluck( $backtrace, 'function' ) ); |
|
| 253 | + if( false !== $search ) { |
|
| 254 | + $index = '{closure}' == Arr::get( $backtrace, ($search + 2).'.function' ) |
|
| 255 | 255 | ? $search + 4 |
| 256 | 256 | : $search + 1; |
| 257 | - return $this->buildBacktraceLine($backtrace, $index); |
|
| 257 | + return $this->buildBacktraceLine( $backtrace, $index ); |
|
| 258 | 258 | } |
| 259 | 259 | return 'Unknown'; |
| 260 | 260 | } |
@@ -263,23 +263,23 @@ discard block |
||
| 263 | 263 | * @param mixed $data |
| 264 | 264 | * @return string |
| 265 | 265 | */ |
| 266 | - protected function getBacktraceLineFromData($data) |
|
| 266 | + protected function getBacktraceLineFromData( $data ) |
|
| 267 | 267 | { |
| 268 | 268 | $backtrace = $data instanceof Throwable |
| 269 | 269 | ? $data->getTrace() |
| 270 | - : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
| 271 | - return $this->buildBacktraceLine($backtrace, 0); |
|
| 270 | + : debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 ); |
|
| 271 | + return $this->buildBacktraceLine( $backtrace, 0 ); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
| 275 | 275 | * @param mixed $data |
| 276 | 276 | * @return string |
| 277 | 277 | */ |
| 278 | - protected function getMessageFromData($data) |
|
| 278 | + protected function getMessageFromData( $data ) |
|
| 279 | 279 | { |
| 280 | 280 | return $data instanceof Throwable |
| 281 | - ? $this->normalizeThrowableMessage($data->getMessage()) |
|
| 282 | - : print_r($data, 1); |
|
| 281 | + ? $this->normalizeThrowableMessage( $data->getMessage() ) |
|
| 282 | + : print_r( $data, 1 ); |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | /** |
@@ -288,53 +288,53 @@ discard block |
||
| 288 | 288 | * @param array $context |
| 289 | 289 | * @return string |
| 290 | 290 | */ |
| 291 | - protected function interpolate($message, $context = []) |
|
| 291 | + protected function interpolate( $message, $context = [] ) |
|
| 292 | 292 | { |
| 293 | - if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
| 294 | - return print_r($message, true); |
|
| 293 | + if( $this->isObjectOrArray( $message ) || !is_array( $context ) ) { |
|
| 294 | + return print_r( $message, true ); |
|
| 295 | 295 | } |
| 296 | 296 | $replace = []; |
| 297 | - foreach ($context as $key => $value) { |
|
| 298 | - $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
| 297 | + foreach( $context as $key => $value ) { |
|
| 298 | + $replace['{'.$key.'}'] = $this->normalizeValue( $value ); |
|
| 299 | 299 | } |
| 300 | - return strtr($message, $replace); |
|
| 300 | + return strtr( $message, $replace ); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | /** |
| 304 | 304 | * @param mixed $value |
| 305 | 305 | * @return bool |
| 306 | 306 | */ |
| 307 | - protected function isObjectOrArray($value) |
|
| 307 | + protected function isObjectOrArray( $value ) |
|
| 308 | 308 | { |
| 309 | - return is_object($value) || is_array($value); |
|
| 309 | + return is_object( $value ) || is_array( $value ); |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | /** |
| 313 | 313 | * @param string $backtraceLine |
| 314 | 314 | * @return string |
| 315 | 315 | */ |
| 316 | - protected function normalizeBacktraceLine($backtraceLine) |
|
| 316 | + protected function normalizeBacktraceLine( $backtraceLine ) |
|
| 317 | 317 | { |
| 318 | 318 | $search = [ |
| 319 | - glsr()->path('plugin/'), |
|
| 320 | - glsr()->path('plugin/', false), |
|
| 321 | - trailingslashit(glsr()->path()), |
|
| 322 | - trailingslashit(glsr()->path('', false)), |
|
| 319 | + glsr()->path( 'plugin/' ), |
|
| 320 | + glsr()->path( 'plugin/', false ), |
|
| 321 | + trailingslashit( glsr()->path() ), |
|
| 322 | + trailingslashit( glsr()->path( '', false ) ), |
|
| 323 | 323 | WP_CONTENT_DIR, |
| 324 | 324 | ABSPATH, |
| 325 | 325 | ]; |
| 326 | - return str_replace(array_unique($search), '', $backtraceLine); |
|
| 326 | + return str_replace( array_unique( $search ), '', $backtraceLine ); |
|
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | /** |
| 330 | 330 | * @param string $message |
| 331 | 331 | * @return string |
| 332 | 332 | */ |
| 333 | - protected function normalizeThrowableMessage($message) |
|
| 333 | + protected function normalizeThrowableMessage( $message ) |
|
| 334 | 334 | { |
| 335 | - $calledIn = strpos($message, ', called in'); |
|
| 335 | + $calledIn = strpos( $message, ', called in' ); |
|
| 336 | 336 | return false !== $calledIn |
| 337 | - ? substr($message, 0, $calledIn) |
|
| 337 | + ? substr( $message, 0, $calledIn ) |
|
| 338 | 338 | : $message; |
| 339 | 339 | } |
| 340 | 340 | |
@@ -342,14 +342,14 @@ discard block |
||
| 342 | 342 | * @param mixed $value |
| 343 | 343 | * @return string |
| 344 | 344 | */ |
| 345 | - protected function normalizeValue($value) |
|
| 345 | + protected function normalizeValue( $value ) |
|
| 346 | 346 | { |
| 347 | - if ($value instanceof DateTime) { |
|
| 348 | - $value = $value->format('Y-m-d H:i:s'); |
|
| 349 | - } elseif ($this->isObjectOrArray($value)) { |
|
| 350 | - $value = json_encode($value); |
|
| 347 | + if( $value instanceof DateTime ) { |
|
| 348 | + $value = $value->format( 'Y-m-d H:i:s' ); |
|
| 349 | + } elseif( $this->isObjectOrArray( $value ) ) { |
|
| 350 | + $value = json_encode( $value ); |
|
| 351 | 351 | } |
| 352 | - return (string) $value; |
|
| 352 | + return (string)$value; |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | */ |
| 358 | 358 | protected function reset() |
| 359 | 359 | { |
| 360 | - if ($this->size() <= pow(1024, 2) / 4) { |
|
| 360 | + if( $this->size() <= pow( 1024, 2 ) / 4 ) { |
|
| 361 | 361 | return; |
| 362 | 362 | } |
| 363 | 363 | $this->clear(); |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | $this->file, |
| 366 | 366 | $this->buildLogEntry( |
| 367 | 367 | static::NOTICE, |
| 368 | - _x('Console was automatically cleared (256KB maximum size)', 'admin-text', 'site-reviews') |
|
| 368 | + _x( 'Console was automatically cleared (256KB maximum size)', 'admin-text', 'site-reviews' ) |
|
| 369 | 369 | ) |
| 370 | 370 | ); |
| 371 | 371 | } |
@@ -375,23 +375,23 @@ discard block |
||
| 375 | 375 | */ |
| 376 | 376 | protected function setLogFile() |
| 377 | 377 | { |
| 378 | - if (!function_exists('wp_hash')) { |
|
| 378 | + if( !function_exists( 'wp_hash' ) ) { |
|
| 379 | 379 | require_once ABSPATH.WPINC.'/pluggable.php'; |
| 380 | 380 | } |
| 381 | 381 | $uploads = wp_upload_dir(); |
| 382 | - $base = trailingslashit($uploads['basedir'].'/'.glsr()->id); |
|
| 383 | - $this->file = $base.'logs/'.sanitize_file_name('console-'.wp_hash(glsr()->id).'.log'); |
|
| 382 | + $base = trailingslashit( $uploads['basedir'].'/'.glsr()->id ); |
|
| 383 | + $this->file = $base.'logs/'.sanitize_file_name( 'console-'.wp_hash( glsr()->id ).'.log' ); |
|
| 384 | 384 | $files = [ |
| 385 | 385 | $base.'index.php' => '<?php', |
| 386 | 386 | $base.'logs/.htaccess' => 'deny from all', |
| 387 | 387 | $base.'logs/index.php' => '<?php', |
| 388 | 388 | $this->file => '', |
| 389 | 389 | ]; |
| 390 | - foreach ($files as $file => $contents) { |
|
| 391 | - if (wp_mkdir_p(dirname($file)) && !file_exists($file)) { |
|
| 392 | - file_put_contents($file, $contents); |
|
| 390 | + foreach( $files as $file => $contents ) { |
|
| 391 | + if( wp_mkdir_p( dirname( $file ) ) && !file_exists( $file ) ) { |
|
| 392 | + file_put_contents( $file, $contents ); |
|
| 393 | 393 | } |
| 394 | 394 | } |
| 395 | - $this->log = file_get_contents($this->file); |
|
| 395 | + $this->log = file_get_contents( $this->file ); |
|
| 396 | 396 | } |
| 397 | 397 | } |
@@ -32,10 +32,10 @@ discard block |
||
| 32 | 32 | public function all() |
| 33 | 33 | { |
| 34 | 34 | $translations = $this->translations(); |
| 35 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
| 36 | - array_walk($translations, function (&$entry) use ($entries) { |
|
| 37 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 38 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 35 | + $entries = $this->filter( $translations, $this->entries() )->results(); |
|
| 36 | + array_walk( $translations, function( &$entry ) use ($entries) { |
|
| 37 | + $entry['desc'] = array_key_exists( $entry['id'], $entries ) |
|
| 38 | + ? $this->getEntryString( $entries[$entry['id']], 'msgctxt' ) |
|
| 39 | 39 | : ''; |
| 40 | 40 | }); |
| 41 | 41 | return $translations; |
@@ -46,10 +46,10 @@ discard block |
||
| 46 | 46 | */ |
| 47 | 47 | public function entries() |
| 48 | 48 | { |
| 49 | - if (!isset($this->entries)) { |
|
| 50 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 51 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 52 | - $entries = glsr()->filterArray('translation/entries', $entries); |
|
| 49 | + if( !isset($this->entries) ) { |
|
| 50 | + $potFile = glsr()->path( glsr()->languages.'/'.Application::ID.'.pot' ); |
|
| 51 | + $entries = $this->extractEntriesFromPotFile( $potFile ); |
|
| 52 | + $entries = glsr()->filterArray( 'translation/entries', $entries ); |
|
| 53 | 53 | $this->entries = $entries; |
| 54 | 54 | } |
| 55 | 55 | return $this->entries; |
@@ -60,27 +60,27 @@ discard block |
||
| 60 | 60 | * @param array|null $entries |
| 61 | 61 | * @return static |
| 62 | 62 | */ |
| 63 | - public function exclude($entriesToExclude = null, $entries = null) |
|
| 63 | + public function exclude( $entriesToExclude = null, $entries = null ) |
|
| 64 | 64 | { |
| 65 | - return $this->filter($entriesToExclude, $entries, false); |
|
| 65 | + return $this->filter( $entriesToExclude, $entries, false ); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * @param string $potFile |
| 70 | 70 | * @return array |
| 71 | 71 | */ |
| 72 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 72 | + public function extractEntriesFromPotFile( $potFile, array $entries = [] ) |
|
| 73 | 73 | { |
| 74 | 74 | try { |
| 75 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 76 | - foreach ($potEntries as $key => $entry) { |
|
| 77 | - if (Str::contains(static::CONTEXT_ADMIN_KEY, Arr::get($entry, 'msgctxt'))) { |
|
| 75 | + $potEntries = $this->normalize( Parser::parseFile( $potFile )->getEntries() ); |
|
| 76 | + foreach( $potEntries as $key => $entry ) { |
|
| 77 | + if( Str::contains( static::CONTEXT_ADMIN_KEY, Arr::get( $entry, 'msgctxt' ) ) ) { |
|
| 78 | 78 | continue; |
| 79 | 79 | } |
| 80 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 80 | + $entries[html_entity_decode( $key, ENT_COMPAT, 'UTF-8' )] = $entry; |
|
| 81 | 81 | } |
| 82 | - } catch (Exception $e) { |
|
| 83 | - glsr_log()->error($e->getMessage()); |
|
| 82 | + } catch( Exception $e ) { |
|
| 83 | + glsr_log()->error( $e->getMessage() ); |
|
| 84 | 84 | } |
| 85 | 85 | return $entries; |
| 86 | 86 | } |
@@ -91,18 +91,18 @@ discard block |
||
| 91 | 91 | * @param bool $intersect |
| 92 | 92 | * @return static |
| 93 | 93 | */ |
| 94 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 94 | + public function filter( $filterWith = null, $entries = null, $intersect = true ) |
|
| 95 | 95 | { |
| 96 | - if (!is_array($entries)) { |
|
| 96 | + if( !is_array( $entries ) ) { |
|
| 97 | 97 | $entries = $this->results; |
| 98 | 98 | } |
| 99 | - if (!is_array($filterWith)) { |
|
| 99 | + if( !is_array( $filterWith ) ) { |
|
| 100 | 100 | $filterWith = $this->translations(); |
| 101 | 101 | } |
| 102 | - $keys = array_flip(wp_list_pluck($filterWith, 'id')); |
|
| 102 | + $keys = array_flip( wp_list_pluck( $filterWith, 'id' ) ); |
|
| 103 | 103 | $this->results = $intersect |
| 104 | - ? array_intersect_key($entries, $keys) |
|
| 105 | - : array_diff_key($entries, $keys); |
|
| 104 | + ? array_intersect_key( $entries, $keys ) |
|
| 105 | + : array_diff_key( $entries, $keys ); |
|
| 106 | 106 | return $this; |
| 107 | 107 | } |
| 108 | 108 | |
@@ -110,20 +110,20 @@ discard block |
||
| 110 | 110 | * @param string $template |
| 111 | 111 | * @return string |
| 112 | 112 | */ |
| 113 | - public function render($template, array $entry) |
|
| 113 | + public function render( $template, array $entry ) |
|
| 114 | 114 | { |
| 115 | 115 | $data = array_combine( |
| 116 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 116 | + array_map( function( $key ) { return 'data.'.$key; }, array_keys( $entry ) ), |
|
| 117 | 117 | $entry |
| 118 | 118 | ); |
| 119 | 119 | $data['data.class'] = $data['data.error'] = ''; |
| 120 | - if (false === array_search($entry['s1'], wp_list_pluck($this->entries(), 'msgid'))) { |
|
| 120 | + if( false === array_search( $entry['s1'], wp_list_pluck( $this->entries(), 'msgid' ) ) ) { |
|
| 121 | 121 | $data['data.class'] = 'is-invalid'; |
| 122 | - $data['data.error'] = _x('This custom translation is no longer valid as the original text has been changed or removed.', 'admin-text', 'site-reviews'); |
|
| 122 | + $data['data.error'] = _x( 'This custom translation is no longer valid as the original text has been changed or removed.', 'admin-text', 'site-reviews' ); |
|
| 123 | 123 | } |
| 124 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 125 | - 'context' => array_map('esc_html', $data), |
|
| 126 | - ]); |
|
| 124 | + return glsr( Template::class )->build( 'partials/translations/'.$template, [ |
|
| 125 | + 'context' => array_map( 'esc_html', $data ), |
|
| 126 | + ] ); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | public function renderAll() |
| 134 | 134 | { |
| 135 | 135 | $rendered = ''; |
| 136 | - foreach ($this->all() as $index => $entry) { |
|
| 136 | + foreach( $this->all() as $index => $entry ) { |
|
| 137 | 137 | $entry['index'] = $index; |
| 138 | 138 | $entry['prefix'] = OptionManager::databaseKey(); |
| 139 | - $rendered.= $this->render($entry['type'], $entry); |
|
| 139 | + $rendered .= $this->render( $entry['type'], $entry ); |
|
| 140 | 140 | } |
| 141 | 141 | return $rendered; |
| 142 | 142 | } |
@@ -145,25 +145,25 @@ discard block |
||
| 145 | 145 | * @param bool $resetAfterRender |
| 146 | 146 | * @return string |
| 147 | 147 | */ |
| 148 | - public function renderResults($resetAfterRender = true) |
|
| 148 | + public function renderResults( $resetAfterRender = true ) |
|
| 149 | 149 | { |
| 150 | 150 | $rendered = ''; |
| 151 | - foreach ($this->results as $id => $entry) { |
|
| 151 | + foreach( $this->results as $id => $entry ) { |
|
| 152 | 152 | $data = [ |
| 153 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 153 | + 'desc' => $this->getEntryString( $entry, 'msgctxt' ), |
|
| 154 | 154 | 'id' => $id, |
| 155 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 156 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 155 | + 'p1' => $this->getEntryString( $entry, 'msgid_plural' ), |
|
| 156 | + 's1' => $this->getEntryString( $entry, 'msgid' ), |
|
| 157 | 157 | ]; |
| 158 | 158 | $text = !empty($data['p1']) |
| 159 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 159 | + ? sprintf( '%s | %s', $data['s1'], $data['p1'] ) |
|
| 160 | 160 | : $data['s1']; |
| 161 | - $rendered.= $this->render('result', [ |
|
| 162 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 163 | - 'text' => wp_strip_all_tags($text), |
|
| 164 | - ]); |
|
| 161 | + $rendered .= $this->render( 'result', [ |
|
| 162 | + 'entry' => json_encode( $data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ), |
|
| 163 | + 'text' => wp_strip_all_tags( $text ), |
|
| 164 | + ] ); |
|
| 165 | 165 | } |
| 166 | - if ($resetAfterRender) { |
|
| 166 | + if( $resetAfterRender ) { |
|
| 167 | 167 | $this->reset(); |
| 168 | 168 | } |
| 169 | 169 | return $rendered; |
@@ -191,18 +191,18 @@ discard block |
||
| 191 | 191 | * @param string $needle |
| 192 | 192 | * @return static |
| 193 | 193 | */ |
| 194 | - public function search($needle = '') |
|
| 194 | + public function search( $needle = '' ) |
|
| 195 | 195 | { |
| 196 | 196 | $this->reset(); |
| 197 | - $needle = trim(strtolower($needle)); |
|
| 198 | - foreach ($this->entries() as $key => $entry) { |
|
| 199 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 200 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 201 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 202 | - if (in_array($needle, [$single, $plural])) { |
|
| 197 | + $needle = trim( strtolower( $needle ) ); |
|
| 198 | + foreach( $this->entries() as $key => $entry ) { |
|
| 199 | + $single = strtolower( $this->getEntryString( $entry, 'msgid' ) ); |
|
| 200 | + $plural = strtolower( $this->getEntryString( $entry, 'msgid_plural' ) ); |
|
| 201 | + if( strlen( $needle ) < static::SEARCH_THRESHOLD ) { |
|
| 202 | + if( in_array( $needle, [$single, $plural] ) ) { |
|
| 203 | 203 | $this->results[$key] = $entry; |
| 204 | 204 | } |
| 205 | - } elseif (Str::contains(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 205 | + } elseif( Str::contains( sprintf( '%s %s', $single, $plural ), $needle ) ) { |
|
| 206 | 206 | $this->results[$key] = $entry; |
| 207 | 207 | } |
| 208 | 208 | } |
@@ -216,10 +216,10 @@ discard block |
||
| 216 | 216 | public function translations() |
| 217 | 217 | { |
| 218 | 218 | static $translations; |
| 219 | - if (empty($translations)) { |
|
| 220 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
| 219 | + if( empty($translations) ) { |
|
| 220 | + $settings = glsr( OptionManager::class )->get( 'settings' ); |
|
| 221 | 221 | $translations = isset($settings['strings']) |
| 222 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
| 222 | + ? $this->normalizeSettings( (array)$settings['strings'] ) |
|
| 223 | 223 | : []; |
| 224 | 224 | } |
| 225 | 225 | return $translations; |
@@ -229,28 +229,28 @@ discard block |
||
| 229 | 229 | * @param string $key |
| 230 | 230 | * @return string |
| 231 | 231 | */ |
| 232 | - protected function getEntryString(array $entry, $key) |
|
| 232 | + protected function getEntryString( array $entry, $key ) |
|
| 233 | 233 | { |
| 234 | 234 | return isset($entry[$key]) |
| 235 | - ? implode('', (array) $entry[$key]) |
|
| 235 | + ? implode( '', (array)$entry[$key] ) |
|
| 236 | 236 | : ''; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
| 240 | 240 | * @return array |
| 241 | 241 | */ |
| 242 | - protected function normalize(array $entries) |
|
| 242 | + protected function normalize( array $entries ) |
|
| 243 | 243 | { |
| 244 | 244 | $keys = [ |
| 245 | 245 | 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
| 246 | 246 | ]; |
| 247 | - array_walk($entries, function (&$entry) use ($keys) { |
|
| 248 | - foreach ($keys as $key) { |
|
| 247 | + array_walk( $entries, function( &$entry ) use ($keys) { |
|
| 248 | + foreach( $keys as $key ) { |
|
| 249 | 249 | try { |
| 250 | - $entry = $this->normalizeEntryString($entry, $key); |
|
| 251 | - } catch (\TypeError $error) { |
|
| 252 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 253 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 250 | + $entry = $this->normalizeEntryString( $entry, $key ); |
|
| 251 | + } catch( \TypeError $error ) { |
|
| 252 | + glsr_log()->once( 'error', 'Translation/normalize', $error ); |
|
| 253 | + glsr_log()->once( 'debug', 'Translation/normalize', $entry ); |
|
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | }); |
@@ -261,10 +261,10 @@ discard block |
||
| 261 | 261 | * @param string $key |
| 262 | 262 | * @return array |
| 263 | 263 | */ |
| 264 | - protected function normalizeEntryString(array $entry, $key) |
|
| 264 | + protected function normalizeEntryString( array $entry, $key ) |
|
| 265 | 265 | { |
| 266 | - if (isset($entry[$key])) { |
|
| 267 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
| 266 | + if( isset($entry[$key]) ) { |
|
| 267 | + $entry[$key] = $this->getEntryString( $entry, $key ); |
|
| 268 | 268 | } |
| 269 | 269 | return $entry; |
| 270 | 270 | } |
@@ -272,15 +272,15 @@ discard block |
||
| 272 | 272 | /** |
| 273 | 273 | * @return array |
| 274 | 274 | */ |
| 275 | - protected function normalizeSettings(array $strings) |
|
| 275 | + protected function normalizeSettings( array $strings ) |
|
| 276 | 276 | { |
| 277 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 278 | - $strings = array_filter($strings, 'is_array'); |
|
| 279 | - foreach ($strings as &$string) { |
|
| 277 | + $defaultString = array_fill_keys( ['id', 's1', 's2', 'p1', 'p2'], '' ); |
|
| 278 | + $strings = array_filter( $strings, 'is_array' ); |
|
| 279 | + foreach( $strings as &$string ) { |
|
| 280 | 280 | $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
| 281 | - $string = wp_parse_args($string, $defaultString); |
|
| 281 | + $string = wp_parse_args( $string, $defaultString ); |
|
| 282 | 282 | } |
| 283 | - return array_filter($strings, function ($string) { |
|
| 283 | + return array_filter( $strings, function( $string ) { |
|
| 284 | 284 | return !empty($string['id']); |
| 285 | 285 | }); |
| 286 | 286 | } |
@@ -20,17 +20,17 @@ discard block |
||
| 20 | 20 | /** |
| 21 | 21 | * {@inheritdoc} |
| 22 | 22 | */ |
| 23 | - public function build(array $args = []) |
|
| 23 | + public function build( array $args = [] ) |
|
| 24 | 24 | { |
| 25 | - $this->args = $this->normalize($args); |
|
| 26 | - if ($this->args['total'] > 1) { |
|
| 27 | - return glsr(Template::class)->build('templates/pagination', [ |
|
| 25 | + $this->args = $this->normalize( $args ); |
|
| 26 | + if( $this->args['total'] > 1 ) { |
|
| 27 | + return glsr( Template::class )->build( 'templates/pagination', [ |
|
| 28 | 28 | 'context' => [ |
| 29 | - 'links' => glsr()->filterString('paginate_links', $this->buildLinks(), $this->args), |
|
| 29 | + 'links' => glsr()->filterString( 'paginate_links', $this->buildLinks(), $this->args ), |
|
| 30 | 30 | 'loader' => '<div class="glsr-loader"></div>', |
| 31 | - 'screen_reader_text' => _x('Site Reviews navigation', 'screen reader text', 'site-reviews'), |
|
| 31 | + 'screen_reader_text' => _x( 'Site Reviews navigation', 'screen reader text', 'site-reviews' ), |
|
| 32 | 32 | ], |
| 33 | - ]); |
|
| 33 | + ] ); |
|
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
@@ -39,16 +39,16 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | protected function buildFauxLinks() |
| 41 | 41 | { |
| 42 | - $links = (array) paginate_links(wp_parse_args(['type' => 'array'], $this->args)); |
|
| 42 | + $links = (array)paginate_links( wp_parse_args( ['type' => 'array'], $this->args ) ); |
|
| 43 | 43 | $pattern = '/(href=["\'])([^"\']*?)(["\'])/i'; |
| 44 | - foreach ($links as &$link) { |
|
| 45 | - if (preg_match($pattern, $link, $matches)) { |
|
| 46 | - $page = glsr(Helper::class)->getPageNumber(Arr::get($matches, 2)); |
|
| 47 | - $replacement = sprintf('data-page="%d" href="#"', $page); |
|
| 48 | - $link = str_replace(Arr::get($matches, 0), $replacement, $link); |
|
| 44 | + foreach( $links as &$link ) { |
|
| 45 | + if( preg_match( $pattern, $link, $matches ) ) { |
|
| 46 | + $page = glsr( Helper::class )->getPageNumber( Arr::get( $matches, 2 ) ); |
|
| 47 | + $replacement = sprintf( 'data-page="%d" href="#"', $page ); |
|
| 48 | + $link = str_replace( Arr::get( $matches, 0 ), $replacement, $link ); |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | - return implode("\n", $links); |
|
| 51 | + return implode( "\n", $links ); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -56,23 +56,23 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | protected function buildLinks() |
| 58 | 58 | { |
| 59 | - return glsr(OptionManager::class)->getBool('settings.reviews.pagination.url_parameter') |
|
| 60 | - ? paginate_links(wp_parse_args(['type' => 'plain'], $this->args)) |
|
| 59 | + return glsr( OptionManager::class )->getBool( 'settings.reviews.pagination.url_parameter' ) |
|
| 60 | + ? paginate_links( wp_parse_args( ['type' => 'plain'], $this->args ) ) |
|
| 61 | 61 | : $this->buildFauxLinks(); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * @return array |
| 66 | 66 | */ |
| 67 | - protected function normalize(array $args) |
|
| 67 | + protected function normalize( array $args ) |
|
| 68 | 68 | { |
| 69 | - if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
| 69 | + if( $baseUrl = Arr::get( $args, 'baseUrl' ) ) { |
|
| 70 | 70 | $args['base'] = $baseUrl.'%_%'; |
| 71 | 71 | } |
| 72 | - $args = wp_parse_args(array_filter($args), [ |
|
| 73 | - 'current' => glsr(Helper::class)->getPageNumber(), |
|
| 72 | + $args = wp_parse_args( array_filter( $args ), [ |
|
| 73 | + 'current' => glsr( Helper::class )->getPageNumber(), |
|
| 74 | 74 | 'total' => 1, |
| 75 | - ]); |
|
| 76 | - return glsr(Style::class)->paginationArgs($args); |
|
| 75 | + ] ); |
|
| 76 | + return glsr( Style::class )->paginationArgs( $args ); |
|
| 77 | 77 | } |
| 78 | 78 | } |
@@ -10,10 +10,10 @@ discard block |
||
| 10 | 10 | /** |
| 11 | 11 | * {@inheritdoc} |
| 12 | 12 | */ |
| 13 | - protected function handle($value = null) |
|
| 13 | + protected function handle( $value = null ) |
|
| 14 | 14 | { |
| 15 | - if (!$this->isHidden()) { |
|
| 16 | - return $this->wrap($this->percentages(), 'span'); |
|
| 15 | + if( !$this->isHidden() ) { |
|
| 16 | + return $this->wrap( $this->percentages(), 'span' ); |
|
| 17 | 17 | } |
| 18 | 18 | } |
| 19 | 19 | |
@@ -22,21 +22,21 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | protected function percentages() |
| 24 | 24 | { |
| 25 | - $percentages = preg_filter('/$/', '%', glsr(Rating::class)->percentages($this->ratings)); |
|
| 26 | - $ratingRange = range(glsr()->constant('MAX_RATING', Rating::class), 1); |
|
| 27 | - return array_reduce($ratingRange, function ($carry, $level) use ($percentages) { |
|
| 28 | - $label = $this->ratingLabel($level); |
|
| 29 | - $bar = $this->ratingBar($level, $percentages); |
|
| 30 | - $info = $this->ratingInfo($level, $percentages); |
|
| 25 | + $percentages = preg_filter( '/$/', '%', glsr( Rating::class )->percentages( $this->ratings ) ); |
|
| 26 | + $ratingRange = range( glsr()->constant( 'MAX_RATING', Rating::class ), 1 ); |
|
| 27 | + return array_reduce( $ratingRange, function( $carry, $level ) use ($percentages) { |
|
| 28 | + $label = $this->ratingLabel( $level ); |
|
| 29 | + $bar = $this->ratingBar( $level, $percentages ); |
|
| 30 | + $info = $this->ratingInfo( $level, $percentages ); |
|
| 31 | 31 | $value = $label.$bar.$info; |
| 32 | - $value = glsr()->filterString('summary/wrap/bar', $value, $this->args, [ |
|
| 33 | - 'info' => wp_strip_all_tags($info, true), |
|
| 32 | + $value = glsr()->filterString( 'summary/wrap/bar', $value, $this->args, [ |
|
| 33 | + 'info' => wp_strip_all_tags( $info, true ), |
|
| 34 | 34 | 'rating' => $level, |
| 35 | - ]); |
|
| 36 | - return $carry.glsr(Builder::class)->div([ |
|
| 35 | + ] ); |
|
| 36 | + return $carry.glsr( Builder::class )->div( [ |
|
| 37 | 37 | 'class' => 'glsr-bar', |
| 38 | 38 | 'text' => $value, |
| 39 | - ]); |
|
| 39 | + ] ); |
|
| 40 | 40 | }); |
| 41 | 41 | } |
| 42 | 42 | |
@@ -44,41 +44,41 @@ discard block |
||
| 44 | 44 | * @param int $level |
| 45 | 45 | * @return string |
| 46 | 46 | */ |
| 47 | - protected function ratingBar($level, array $percentages) |
|
| 47 | + protected function ratingBar( $level, array $percentages ) |
|
| 48 | 48 | { |
| 49 | - $background = glsr(Builder::class)->span([ |
|
| 49 | + $background = glsr( Builder::class )->span( [ |
|
| 50 | 50 | 'class' => 'glsr-bar-background-percent', |
| 51 | 51 | 'style' => 'width:'.$percentages[$level], |
| 52 | - ]); |
|
| 53 | - return glsr(Builder::class)->span([ |
|
| 52 | + ] ); |
|
| 53 | + return glsr( Builder::class )->span( [ |
|
| 54 | 54 | 'class' => 'glsr-bar-background', |
| 55 | 55 | 'text' => $background, |
| 56 | - ]); |
|
| 56 | + ] ); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | 60 | * @param int $level |
| 61 | 61 | * @return string |
| 62 | 62 | */ |
| 63 | - protected function ratingInfo($level, array $percentages) |
|
| 63 | + protected function ratingInfo( $level, array $percentages ) |
|
| 64 | 64 | { |
| 65 | - $count = glsr()->filterString('summary/counts', $percentages[$level], $this->ratings[$level]); |
|
| 66 | - return glsr(Builder::class)->span([ |
|
| 65 | + $count = glsr()->filterString( 'summary/counts', $percentages[$level], $this->ratings[$level] ); |
|
| 66 | + return glsr( Builder::class )->span( [ |
|
| 67 | 67 | 'class' => 'glsr-bar-percent', |
| 68 | 68 | 'text' => $count, |
| 69 | - ]); |
|
| 69 | + ] ); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | 73 | * @param int $level |
| 74 | 74 | * @return string |
| 75 | 75 | */ |
| 76 | - protected function ratingLabel($level) |
|
| 76 | + protected function ratingLabel( $level ) |
|
| 77 | 77 | { |
| 78 | - $label = $this->args->get('labels.'.$level); |
|
| 79 | - return glsr(Builder::class)->span([ |
|
| 78 | + $label = $this->args->get( 'labels.'.$level ); |
|
| 79 | + return glsr( Builder::class )->span( [ |
|
| 80 | 80 | 'class' => 'glsr-bar-label', |
| 81 | 81 | 'text' => $label, |
| 82 | - ]); |
|
| 82 | + ] ); |
|
| 83 | 83 | } |
| 84 | 84 | } |
@@ -9,10 +9,10 @@ discard block |
||
| 9 | 9 | /** |
| 10 | 10 | * {@inheritdoc} |
| 11 | 11 | */ |
| 12 | - protected function handle($value = null) |
|
| 12 | + protected function handle( $value = null ) |
|
| 13 | 13 | { |
| 14 | - if (!$this->isHidden()) { |
|
| 15 | - return $this->wrap($this->text(), 'span'); |
|
| 14 | + if( !$this->isHidden() ) { |
|
| 15 | + return $this->wrap( $this->text(), 'span' ); |
|
| 16 | 16 | } |
| 17 | 17 | } |
| 18 | 18 | |
@@ -21,11 +21,11 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | protected function text() |
| 23 | 23 | { |
| 24 | - $max = glsr()->constant('MAX_RATING', Rating::class); |
|
| 25 | - $num = (int) array_sum($this->ratings); |
|
| 26 | - $rating = glsr(Rating::class)->average($this->ratings); |
|
| 24 | + $max = glsr()->constant( 'MAX_RATING', Rating::class ); |
|
| 25 | + $num = (int)array_sum( $this->ratings ); |
|
| 26 | + $rating = glsr( Rating::class )->average( $this->ratings ); |
|
| 27 | 27 | $text = $this->args->text; |
| 28 | - if (empty($text)) { |
|
| 28 | + if( empty($text) ) { |
|
| 29 | 29 | $text = _nx( |
| 30 | 30 | '{rating} out of {max} stars (based on {num} review)', |
| 31 | 31 | '{rating} out of {max} stars (based on {num} reviews)', |
@@ -34,6 +34,6 @@ discard block |
||
| 34 | 34 | 'site-reviews' |
| 35 | 35 | ); |
| 36 | 36 | } |
| 37 | - return str_replace(['{rating}','{max}','{num}'], [$rating, $max, $num], $text); |
|
| 37 | + return str_replace( ['{rating}', '{max}', '{num}'], [$rating, $max, $num], $text ); |
|
| 38 | 38 | } |
| 39 | 39 | } |
@@ -45,14 +45,14 @@ discard block |
||
| 45 | 45 | * @param int $roundBy |
| 46 | 46 | * @return float |
| 47 | 47 | */ |
| 48 | - public function average(array $ratingCounts, $roundBy = 1) |
|
| 48 | + public function average( array $ratingCounts, $roundBy = 1 ) |
|
| 49 | 49 | { |
| 50 | - $average = array_sum($ratingCounts); |
|
| 51 | - if ($average > 0) { |
|
| 52 | - $average = $this->totalSum($ratingCounts) / $average; |
|
| 50 | + $average = array_sum( $ratingCounts ); |
|
| 51 | + if( $average > 0 ) { |
|
| 52 | + $average = $this->totalSum( $ratingCounts ) / $average; |
|
| 53 | 53 | } |
| 54 | - $roundedAverage = round($average, intval($roundBy)); |
|
| 55 | - return glsr()->filterFloat('rating/average', $roundedAverage, $ratingCounts, $average); |
|
| 54 | + $roundedAverage = round( $average, intval( $roundBy ) ); |
|
| 55 | + return glsr()->filterFloat( 'rating/average', $roundedAverage, $ratingCounts, $average ); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | */ |
| 61 | 61 | public function emptyArray() |
| 62 | 62 | { |
| 63 | - return array_fill_keys(range(0, glsr()->constant('MAX_RATING', __CLASS__)), 0); |
|
| 63 | + return array_fill_keys( range( 0, glsr()->constant( 'MAX_RATING', __CLASS__ ) ), 0 ); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
@@ -73,47 +73,47 @@ discard block |
||
| 73 | 73 | * @param int $confidencePercentage |
| 74 | 74 | * @return int|float |
| 75 | 75 | */ |
| 76 | - public function lowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
| 76 | + public function lowerBound( array $upDownCounts = [0, 0], $confidencePercentage = 95 ) |
|
| 77 | 77 | { |
| 78 | - $numRatings = array_sum($upDownCounts); |
|
| 79 | - if ($numRatings < 1) { |
|
| 78 | + $numRatings = array_sum( $upDownCounts ); |
|
| 79 | + if( $numRatings < 1 ) { |
|
| 80 | 80 | return 0; |
| 81 | 81 | } |
| 82 | 82 | $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
| 83 | 83 | $phat = 1 * $upDownCounts[1] / $numRatings; |
| 84 | - return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
| 84 | + return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt( ($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings )) / (1 + $z * $z / $numRatings); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | 88 | * @return int|float |
| 89 | 89 | */ |
| 90 | - public function overallPercentage(array $ratingCounts) |
|
| 90 | + public function overallPercentage( array $ratingCounts ) |
|
| 91 | 91 | { |
| 92 | - return round($this->average($ratingCounts) * 100 / glsr()->constant('MAX_RATING', __CLASS__), 2); |
|
| 92 | + return round( $this->average( $ratingCounts ) * 100 / glsr()->constant( 'MAX_RATING', __CLASS__ ), 2 ); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | 96 | * @return array |
| 97 | 97 | */ |
| 98 | - public function percentages(array $ratingCounts) |
|
| 98 | + public function percentages( array $ratingCounts ) |
|
| 99 | 99 | { |
| 100 | - $total = array_sum($ratingCounts); |
|
| 101 | - foreach ($ratingCounts as $index => $count) { |
|
| 102 | - if (empty($count)) { |
|
| 100 | + $total = array_sum( $ratingCounts ); |
|
| 101 | + foreach( $ratingCounts as $index => $count ) { |
|
| 102 | + if( empty($count) ) { |
|
| 103 | 103 | continue; |
| 104 | 104 | } |
| 105 | 105 | $ratingCounts[$index] = $count / $total * 100; |
| 106 | 106 | } |
| 107 | - return $this->roundedPercentages($ratingCounts); |
|
| 107 | + return $this->roundedPercentages( $ratingCounts ); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
| 111 | 111 | * @return float |
| 112 | 112 | */ |
| 113 | - public function ranking(array $ratingCounts) |
|
| 113 | + public function ranking( array $ratingCounts ) |
|
| 114 | 114 | { |
| 115 | - return glsr()->filterFloat('rating/ranking', |
|
| 116 | - $this->rankingUsingImdb($ratingCounts), |
|
| 115 | + return glsr()->filterFloat( 'rating/ranking', |
|
| 116 | + $this->rankingUsingImdb( $ratingCounts ), |
|
| 117 | 117 | $ratingCounts, |
| 118 | 118 | $this |
| 119 | 119 | ); |
@@ -129,15 +129,15 @@ discard block |
||
| 129 | 129 | * @param int $confidencePercentage |
| 130 | 130 | * @return int|float |
| 131 | 131 | */ |
| 132 | - public function rankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
| 132 | + public function rankingUsingImdb( array $ratingCounts, $confidencePercentage = 70 ) |
|
| 133 | 133 | { |
| 134 | - $avgRating = $this->average($ratingCounts); |
|
| 134 | + $avgRating = $this->average( $ratingCounts ); |
|
| 135 | 135 | // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
| 136 | 136 | // This could also be the average score of all items instead of a fixed value. |
| 137 | - $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING', __CLASS__); // prior, 70% = 3.5 |
|
| 137 | + $bayesMean = ($confidencePercentage / 100) * glsr()->constant( 'MAX_RATING', __CLASS__ ); // prior, 70% = 3.5 |
|
| 138 | 138 | // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
| 139 | 139 | $bayesMinimal = 10; // confidence |
| 140 | - $numOfReviews = array_sum($ratingCounts); |
|
| 140 | + $numOfReviews = array_sum( $ratingCounts ); |
|
| 141 | 141 | return $avgRating > 0 |
| 142 | 142 | ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
| 143 | 143 | : 0; |
@@ -153,48 +153,48 @@ discard block |
||
| 153 | 153 | * @param int $confidencePercentage |
| 154 | 154 | * @return float |
| 155 | 155 | */ |
| 156 | - public function rankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
| 156 | + public function rankingUsingZScores( array $ratingCounts, $confidencePercentage = 90 ) |
|
| 157 | 157 | { |
| 158 | - $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING', __CLASS__); |
|
| 159 | - $weight = $this->weight($ratingCounts, $ratingCountsSum); |
|
| 160 | - $weightPow2 = $this->weight($ratingCounts, $ratingCountsSum, true); |
|
| 158 | + $ratingCountsSum = array_sum( $ratingCounts ) + glsr()->constant( 'MAX_RATING', __CLASS__ ); |
|
| 159 | + $weight = $this->weight( $ratingCounts, $ratingCountsSum ); |
|
| 160 | + $weightPow2 = $this->weight( $ratingCounts, $ratingCountsSum, true ); |
|
| 161 | 161 | $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
| 162 | - return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
| 162 | + return $weight - $zScore * sqrt( ($weightPow2 - pow( $weight, 2 )) / ($ratingCountsSum + 1) ); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
| 166 | 166 | * @param int $target |
| 167 | 167 | * @return array |
| 168 | 168 | */ |
| 169 | - protected function roundedPercentages(array $percentages, $totalPercent = 100) |
|
| 169 | + protected function roundedPercentages( array $percentages, $totalPercent = 100 ) |
|
| 170 | 170 | { |
| 171 | - array_walk($percentages, function (&$percent, $index) { |
|
| 171 | + array_walk( $percentages, function( &$percent, $index ) { |
|
| 172 | 172 | $percent = [ |
| 173 | 173 | 'index' => $index, |
| 174 | - 'percent' => floor($percent), |
|
| 175 | - 'remainder' => fmod($percent, 1), |
|
| 174 | + 'percent' => floor( $percent ), |
|
| 175 | + 'remainder' => fmod( $percent, 1 ), |
|
| 176 | 176 | ]; |
| 177 | 177 | }); |
| 178 | - $indexes = wp_list_pluck($percentages, 'index'); |
|
| 179 | - $remainders = wp_list_pluck($percentages, 'remainder'); |
|
| 180 | - array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
| 178 | + $indexes = wp_list_pluck( $percentages, 'index' ); |
|
| 179 | + $remainders = wp_list_pluck( $percentages, 'remainder' ); |
|
| 180 | + array_multisort( $remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages ); |
|
| 181 | 181 | $i = 0; |
| 182 | - if (array_sum(wp_list_pluck($percentages, 'percent')) > 0) { |
|
| 183 | - while (array_sum(wp_list_pluck($percentages, 'percent')) < $totalPercent) { |
|
| 182 | + if( array_sum( wp_list_pluck( $percentages, 'percent' ) ) > 0 ) { |
|
| 183 | + while( array_sum( wp_list_pluck( $percentages, 'percent' ) ) < $totalPercent ) { |
|
| 184 | 184 | ++$percentages[$i]['percent']; |
| 185 | 185 | ++$i; |
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | - array_multisort($indexes, SORT_DESC, $percentages); |
|
| 189 | - return array_combine($indexes, wp_list_pluck($percentages, 'percent')); |
|
| 188 | + array_multisort( $indexes, SORT_DESC, $percentages ); |
|
| 189 | + return array_combine( $indexes, wp_list_pluck( $percentages, 'percent' ) ); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /** |
| 193 | 193 | * @return int |
| 194 | 194 | */ |
| 195 | - protected function totalSum(array $ratingCounts) |
|
| 195 | + protected function totalSum( array $ratingCounts ) |
|
| 196 | 196 | { |
| 197 | - return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
| 197 | + return array_reduce( array_keys( $ratingCounts ), function( $carry, $index ) use ($ratingCounts) { |
|
| 198 | 198 | return $carry + ($index * $ratingCounts[$index]); |
| 199 | 199 | }); |
| 200 | 200 | } |
@@ -204,12 +204,12 @@ discard block |
||
| 204 | 204 | * @param bool $powerOf2 |
| 205 | 205 | * @return float |
| 206 | 206 | */ |
| 207 | - protected function weight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
| 207 | + protected function weight( array $ratingCounts, $ratingCountsSum, $powerOf2 = false ) |
|
| 208 | 208 | { |
| 209 | - return array_reduce(array_keys($ratingCounts), |
|
| 210 | - function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
| 209 | + return array_reduce( array_keys( $ratingCounts ), |
|
| 210 | + function( $count, $rating ) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
| 211 | 211 | $ratingLevel = $powerOf2 |
| 212 | - ? pow($rating, 2) |
|
| 212 | + ? pow( $rating, 2 ) |
|
| 213 | 213 | : $rating; |
| 214 | 214 | return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
| 215 | 215 | } |