nps-notice.php
| 1 | <?php |
| 2 | /** |
| 3 | * SRFM NPS Notice. |
| 4 | * |
| 5 | * @since 1.2.2 |
| 6 | * |
| 7 | * @package sureforms |
| 8 | */ |
| 9 | |
| 10 | namespace SRFM\Inc; |
| 11 | |
| 12 | use Nps_Survey; |
| 13 | use SRFM\Inc\Database\Tables\Entries; |
| 14 | use SRFM\Inc\Traits\Get_Instance; |
| 15 | |
| 16 | // Exit if accessed directly. |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | if ( ! class_exists( 'Nps_Notice' ) ) { |
| 22 | |
| 23 | /** |
| 24 | * Class Nps_Notice |
| 25 | */ |
| 26 | class Nps_Notice { |
| 27 | use Get_Instance; |
| 28 | |
| 29 | /** |
| 30 | * Array of allowed screens where the NPS survey should be displayed. |
| 31 | * This ensures that the NPS survey is only displayed on SureForms pages. |
| 32 | * |
| 33 | * @var array<string> |
| 34 | * @since 1.2.2 |
| 35 | */ |
| 36 | private static $allowed_screens = [ |
| 37 | 'toplevel_page_sureforms_menu', |
| 38 | 'sureforms_page_sureforms_form_settings', |
| 39 | 'sureforms_page_sureforms_entries', |
| 40 | 'edit-sureforms_form', |
| 41 | ]; |
| 42 | |
| 43 | /** |
| 44 | * Constructor. |
| 45 | * |
| 46 | * @since 1.2.2 |
| 47 | */ |
| 48 | private function __construct() { |
| 49 | add_action( 'admin_footer', [ $this, 'show_nps_notice' ], 999 ); |
| 50 | add_filter( 'nps_survey_post_data', [ $this, 'update_nps_survey_post_data' ] ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Count the number of published forms and number form submissions. |
| 55 | * Return whether the NPS survey should be shown or not. |
| 56 | * |
| 57 | * @since 1.2.2 |
| 58 | * @return bool |
| 59 | */ |
| 60 | public function maybe_display_nps_survey() { |
| 61 | $form_count = wp_count_posts( SRFM_FORMS_POST_TYPE )->publish; // Get the number of published forms. |
| 62 | $entries_count = Entries::get_total_entries_by_status( '' ); // Get the number of form submissions. |
| 63 | |
| 64 | // Show the NPS survey if there are at least 3 published forms or 3 form submissions. |
| 65 | if ( $form_count >= 3 || $entries_count >= 3 ) { |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Render NPS Survey |
| 73 | * |
| 74 | * @since 1.2.2 |
| 75 | * @return void |
| 76 | */ |
| 77 | public function show_nps_notice() { |
| 78 | // Ensure the Nps_Survey class exists before proceeding. |
| 79 | if ( ! class_exists( 'Nps_Survey' ) ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Display the NPS Survey only on SureForms pages and avoid conflicts with other plugins. |
| 84 | if ( ! Helper::is_sureforms_admin_page() ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Check if the constant WEEK_IN_SECONDS is already defined. |
| 90 | * This ensures that the constant is not redefined if it's already set by WordPress or other parts of the code. |
| 91 | */ |
| 92 | if ( ! defined( 'WEEK_IN_SECONDS' ) ) { |
| 93 | // Define the WEEK_IN_SECONDS constant with the value of 604800 seconds (equivalent to 7 days). |
| 94 | define( 'WEEK_IN_SECONDS', 604800 ); |
| 95 | } |
| 96 | |
| 97 | // Display the NPS survey. |
| 98 | Nps_Survey::show_nps_notice( |
| 99 | 'nps-survey-sureforms', |
| 100 | [ |
| 101 | 'show_if' => $this->maybe_display_nps_survey(), |
| 102 | 'dismiss_timespan' => 2 * WEEK_IN_SECONDS, |
| 103 | 'display_after' => 0, |
| 104 | 'plugin_slug' => 'sureforms', |
| 105 | 'show_on_screens' => self::$allowed_screens, |
| 106 | 'message' => [ |
| 107 | 'logo' => esc_url( plugin_dir_url( __DIR__ ) . 'admin/assets/sureforms-logo.png' ), |
| 108 | 'plugin_name' => __( 'Quick Question!', 'sureforms' ), |
| 109 | 'nps_rating_message' => __( 'How would you rate SureForms? Love it, hate it, or somewhere in between? Your honest answer helps us understand how we\'re doing.', 'sureforms' ), |
| 110 | 'feedback_title' => __( 'Thanks a lot for your feedback! 😍', 'sureforms' ), |
| 111 | 'feedback_content' => __( 'Thanks for being part of the SureForms community! Got feedback or suggestions? We\'d love to hear it.', 'sureforms' ), |
| 112 | 'plugin_rating_link' => esc_url( 'https://wordpress.org/support/plugin/sureforms/reviews/#new-post' ), |
| 113 | 'plugin_rating_title' => __( 'Thank you for your feedback', 'sureforms' ), |
| 114 | 'plugin_rating_content' => __( 'We value your input. How can we improve your experience?', 'sureforms' ), |
| 115 | 'plugin_rating_button_string' => __( 'Rate SureForms', 'sureforms' ), |
| 116 | 'rating_min_label' => __( 'Hate it!', 'sureforms' ), |
| 117 | 'rating_max_label' => __( 'Love it!', 'sureforms' ), |
| 118 | ], |
| 119 | |
| 120 | ] |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Update the NPS survey post data. |
| 126 | * Add SureForms plugin version to the NPS survey post data. |
| 127 | * |
| 128 | * @param array<mixed> $post_data NPS survey post data. |
| 129 | * @since 1.4.0 |
| 130 | * @return array<mixed> |
| 131 | */ |
| 132 | public function update_nps_survey_post_data( $post_data ) { |
| 133 | if ( isset( $post_data['plugin_slug'] ) && 'sureforms' === $post_data['plugin_slug'] ) { |
| 134 | $post_data['plugin_version'] = SRFM_VER; |
| 135 | } |
| 136 | |
| 137 | return $post_data; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 |