1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Abstracts\Controller; |
|
|
|
|
6
|
|
|
use GeminiLabs\SiteReviews\Application; |
7
|
|
|
use GeminiLabs\SiteReviews\Commands\SubmitReview; |
8
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
9
|
|
|
use GeminiLabs\SiteReviews\Handlers\EnqueueAssets; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Validator\ValidateReview; |
11
|
|
|
|
12
|
|
|
class PublicController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return void |
16
|
|
|
* @action wp_enqueue_scripts |
17
|
|
|
*/ |
18
|
|
|
public function enqueueAssets() |
19
|
|
|
{ |
20
|
|
|
(new EnqueueAssets)->handle(); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return string |
25
|
|
|
* @filter script_loader_tag |
26
|
|
|
*/ |
27
|
|
|
public function filterEnqueuedScripts( $tag, $handle ) |
28
|
|
|
{ |
29
|
|
|
return $handle == Application::ID.'/google-recaptcha' |
30
|
|
|
&& glsr( OptionManager::class )->get( 'settings.reviews-form.recaptcha.integration' ) == 'custom' |
31
|
|
|
? str_replace( ' src=', ' async defer src=', $tag ) |
32
|
|
|
: $tag; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add a variable to query_vars for custom pagination |
37
|
|
|
* @param array $vars |
38
|
|
|
* @return array |
39
|
|
|
* @filter query_vars |
40
|
|
|
* @todo remove need for dirty hack |
41
|
|
|
*/ |
42
|
|
|
public function filterQueryVars( $vars ) |
43
|
|
|
{ |
44
|
|
|
$vars[] = Application::PAGED_QUERY_VAR; |
45
|
|
|
// dirty hack to fix a form submission with a field that has "name" as name |
46
|
|
|
if( filter_input( INPUT_POST, 'action' ) == 'submit-review' |
47
|
|
|
&& !is_null( filter_input( INPUT_POST, 'gotcha' ))) { |
48
|
|
|
$index = array_search( 'name', $vars, true ); |
49
|
|
|
if( false !== $index ) { |
50
|
|
|
unset( $vars[$index] ); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return $vars; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function postSubmitReview( array $request ) |
60
|
|
|
{ |
61
|
|
|
$validated = glsr( ValidateReview::class )->validate( $request ); |
62
|
|
|
if( !empty( $validated->error )) { |
63
|
|
|
return $validated->request; |
64
|
|
|
} |
65
|
|
|
if( $validated->recaptchaIsUnset )return; |
66
|
|
|
return $this->execute( new SubmitReview( $validated->request )); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: