| @@ -1,0 +1,101 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking - Libraries | |
| 4 | + * @subpackage system | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2018 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Helper class used to collect the feedback of the users. | |
| 16 | + * Should be attached to `plugin_action_links` WordPress filter. | |
| 17 | + * | |
| 18 | + * @since 1.0 | |
| 19 | + */ | |
| 20 | +class VikBookingFeedback | |
| 21 | +{ | |
| 22 | + /** | |
| 23 | + * Attaches a feeback modal to the deactivation button. | |
| 24 | + * | |
| 25 | + * @param array $actions An array of plugin action links. | |
| 26 | + * @param string $plugin Path to the plugin file relative to the plugins directory. | |
| 27 | + * | |
| 28 | + * @return array The filtered actions. | |
| 29 | + */ | |
| 30 | + public static function deactivate($actions, $plugin) | |
| 31 | + { | |
| 32 | + // make sure the plugin is the correct one and the deactivation link is available | |
| 33 | + if ($plugin != 'vikbooking/vikbooking.php' || !isset($actions['deactivate'])) | |
| 34 | + { | |
| 35 | + return $actions; | |
| 36 | + } | |
| 37 | + | |
| 38 | + $input = JFactory::getApplication()->input; | |
| 39 | + | |
| 40 | + // check whether the safe-word is set in the request | |
| 41 | + if ($input->getUint('feedback', 1) === 0) | |
| 42 | + { | |
| 43 | + // skip deactivation feedback | |
| 44 | + return $actions; | |
| 45 | + } | |
| 46 | + | |
| 47 | + // check if the user already submitted the feedback in the last week | |
| 48 | + if ($input->cookie->getBool('vikbooking_feedback')) | |
| 49 | + { | |
| 50 | + // feedback already done, avoid to ask for it one more time | |
| 51 | + return $actions; | |
| 52 | + } | |
| 53 | + | |
| 54 | + // extract deactivation URL from link | |
| 55 | + if (!preg_match("/href=\"([^\"]*)\"/i", $actions['deactivate'], $match)) | |
| 56 | + { | |
| 57 | + // unable to extract URL from deactivation link | |
| 58 | + return $actions; | |
| 59 | + } | |
| 60 | + | |
| 61 | + $deactivate_url = end($match); | |
| 62 | + | |
| 63 | + // define URL to popup thickbox | |
| 64 | + $url = '#TB_inline?width=500&height=400&inlineId=vikbooking-feedback'; | |
| 65 | + | |
| 66 | + $__title = __('Feedback', 'vikbooking'); | |
| 67 | + | |
| 68 | + // add support for feedback | |
| 69 | + $actions['deactivate'] = sprintf( | |
| 70 | + '<a href="%s" class="thickbox" aria-label="%s" data-name="%s">%s</a>', | |
| 71 | + esc_attr($url), | |
| 72 | + esc_attr(sprintf(__('Deactivate %s', 'plugin'), 'VikBooking')), | |
| 73 | + esc_attr($__title), | |
| 74 | + __('Deactivate') | |
| 75 | + ); | |
| 76 | + | |
| 77 | + // append thickbox to admin footer | |
| 78 | + add_action('admin_footer', function() use ($deactivate_url) | |
| 79 | + { | |
| 80 | + // make URL safe for JS | |
| 81 | + $deactivate_url_js = str_replace('&', '&', $deactivate_url); | |
| 82 | + | |
| 83 | + VikBookingLoader::import('update.license'); | |
| 84 | + | |
| 85 | + $data = array( | |
| 86 | + 'url' => $deactivate_url_js, | |
| 87 | + 'pro' => (bool) VikBookingLicense::isPro(), | |
| 88 | + ); | |
| 89 | + | |
| 90 | + // display feedback thickbox | |
| 91 | + echo JLayoutHelper::render( | |
| 92 | + 'html.feedback.thickbox', | |
| 93 | + $data, | |
| 94 | + null, | |
| 95 | + array('component' => 'com_vikbooking') | |
| 96 | + ); | |
| 97 | + }); | |
| 98 | + | |
| 99 | + return $actions; | |
| 100 | + } | |
| 101 | +} | |