| 1 |
<?php |
| 2 |
namespace Depicter\WordPress; |
| 3 |
|
| 4 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 5 |
|
| 6 |
class DeactivationFeedbackService |
| 7 |
{ |
| 8 |
|
| 9 |
/** |
| 10 |
* DeactivationFeedbackService constructor. |
| 11 |
*/ |
| 12 |
public function __construct(){ |
| 13 |
add_action( 'current_screen', [ $this, 'check_current_screen' ] ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Check if current screen is plugins page then print feedback markup |
| 18 |
*/ |
| 19 |
public function check_current_screen() { |
| 20 |
if ( in_array( get_current_screen()->id, [ 'plugins', 'plugins-network' ] ) ) { |
| 21 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts']); |
| 22 |
add_action( 'admin_footer', [ $this, 'enqueue_feedback_dialog_scripts' ] ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Enqueue required scripts in plugins page |
| 28 |
*/ |
| 29 |
public function enqueueScripts() { |
| 30 |
\Depicter::core()->assets()->enqueueScript( 'depicter-admin', \Depicter::core()->assets()->getUrl() . '/resources/scripts/admin/index.js', ['jquery'], true ); |
| 31 |
wp_localize_script('depicter-admin', 'depDeactivationParams',[ |
| 32 |
'ajaxUrl' => esc_url( admin_url( 'admin-ajax.php' ) ), |
| 33 |
]); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return array[] |
| 38 |
*/ |
| 39 |
protected function reasonsList() { |
| 40 |
return [ |
| 41 |
[ |
| 42 |
'key' => 'no-longer-need-it', |
| 43 |
'text' => __( 'I no longer need the plugin', 'depicter' ), |
| 44 |
'user-description' => '' |
| 45 |
], |
| 46 |
[ |
| 47 |
'key' => 'broke-my-site', |
| 48 |
'text' => __( 'It broke my site', 'depicter' ), |
| 49 |
'user-description' => '' |
| 50 |
], |
| 51 |
[ |
| 52 |
'key' => 'found-better-plugin', |
| 53 |
'text' => __( 'I found a better plugin or solution', 'depicter' ), |
| 54 |
'user-description' => __( 'Please share which plugin?', 'depicter' ), |
| 55 |
], |
| 56 |
[ |
| 57 |
'key' => 'does-not-work', |
| 58 |
'text' => __( 'I couldn\'t get the plugin to work', 'depicter' ), |
| 59 |
'user-description' => __( 'How we can improve it?', 'depicter' ), |
| 60 |
], |
| 61 |
[ |
| 62 |
'key' => 'temporary', |
| 63 |
'text' => __( 'It\'s a temporary deactivation', 'depicter' ), |
| 64 |
'user-description' => '' |
| 65 |
], |
| 66 |
[ |
| 67 |
'key' => 'deactivation-other', |
| 68 |
'text' => __( 'Other', 'depicter' ), |
| 69 |
'user-description' => __( 'What we can do better?', 'depicter' ), |
| 70 |
] |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Render feedback markup |
| 76 |
*/ |
| 77 |
public function enqueue_feedback_dialog_scripts() { |
| 78 |
\Depicter::render('admin/survey/feedback', [ |
| 79 |
'reasons' => $this->reasonsList() |
| 80 |
] ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param $feedback |
| 85 |
* |
| 86 |
* @return bool |
| 87 |
* @throws GuzzleException |
| 88 |
*/ |
| 89 |
public function sendFeedback( $feedback ) { |
| 90 |
if ( empty( $feedback['userDescription'] ) ) { |
| 91 |
foreach( $this->reasonsList() as $reason ){ |
| 92 |
if ( $reason['key'] == $feedback['issueRelatesTo'] ) { |
| 93 |
$feedback['userDescription'] = $reason['text']; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
return ( in_array( $feedback['issueRelatesTo'], wp_list_pluck( $this->reasonsList(), 'key' ) ) || $feedback['issueRelatesTo'] == 'skip' ) && |
| 98 |
\Depicter::client()->reportIssue( $feedback ); |
| 99 |
} |
| 100 |
} |
| 101 |
|