diff --git a/includes/admin/class-noptin-admin.php b/includes/admin/class-noptin-admin.php index 35c4ce18..b8827e45 100644 --- a/includes/admin/class-noptin-admin.php +++ b/includes/admin/class-noptin-admin.php @@ -17,7 +17,6 @@ * * @since 1.0.0 */ - class Noptin_Admin { /** @@ -25,6 +24,7 @@ class Noptin_Admin { * * @access public * @since 1.0.0 + * @var string|null */ public $admin_path = null; @@ -33,19 +33,24 @@ class Noptin_Admin { * * @access public * @since 1.0.0 + * @var string|null */ public $admin_url = null; /** + * The main admin class instance. + * * @access private - * @var obj $instance The one true noptin + * @var Noptin_Admin $instance * @since 1.0.0 */ private static $instance = null; /** + * Contains an array of any admin notices to be displayed in this session. + * * @access public - * @var array $notices Notices + * @var array $notices Notices * @since 1.1.2 */ private $notices = array( @@ -56,7 +61,7 @@ class Noptin_Admin { ); /** - * Background Sync + * Background Sync. * * @var Noptin_Background_Sync * @access public @@ -166,7 +171,7 @@ private function init_hooks() { * * @access public * @since 1.0.0 - * @return self::$instance + * @return void */ public function enqeue_scripts() { global $pagenow, $current_screen; @@ -182,7 +187,7 @@ public function enqeue_scripts() { $page = $_GET['page']; } - if ( ! empty( $current_screen->post_type ) && in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) { + if ( ! empty( $current_screen->post_type ) && in_array( $pagenow, noptin_parse_list( 'post.php post-new.php' ), true ) ) { $page = $current_screen->post_type; } @@ -265,10 +270,10 @@ public function enqeue_scripts() { wp_enqueue_script( 'noptin-subscribers', $this->assets_url . 'js/dist/subscribers.js', array( 'sweetalert2' ), $version, true ); $params = array( - 'ajaxurl' => admin_url( 'admin-ajax.php' ), - 'nonce' => wp_create_nonce( 'noptin_subscribers' ), + 'ajaxurl' => admin_url( 'admin-ajax.php' ), + 'nonce' => wp_create_nonce( 'noptin_subscribers' ), ); - + // localize and enqueue the script with all of the variable inserted. wp_localize_script( 'noptin-subscribers', 'noptinSubscribers', $params ); @@ -280,7 +285,7 @@ public function enqeue_scripts() { * * @access public * @since 1.0.0 - * @return self::$instance + * @return void */ public function add_menu_page() { @@ -371,7 +376,7 @@ public function add_menu_page() { * * @access public * @since 1.0.0 - * @return self::$instance + * @return void */ public function remove_menus() { remove_submenu_page( 'index.php', 'noptin-welcome' ); @@ -382,7 +387,7 @@ public function remove_menus() { * * @access public * @since 1.0.0 - * @return self::$instance + * @return void */ public function welcome_screen_content() { include $this->admin_path . 'welcome-screen.php'; @@ -393,7 +398,7 @@ public function welcome_screen_content() { * * @access public * @since 1.0.0 - * @return self::$instance + * @return void */ public function render_main_page() { @@ -436,7 +441,7 @@ public function render_main_page() { * * @access public * @since 1.1.2 - * @return self::$instance + * @return void */ public function render_email_campaigns_page() { @@ -467,7 +472,7 @@ public function render_email_campaigns_page() { * * @access public * @since 1.0.0 - * @return self::$instance + * @return void */ public function render_subscribers_page() { @@ -503,7 +508,7 @@ public function render_subscribers_page() { * * @access public * @since 1.1.1 - * @return self::$instance + * @return void */ public function render_subscribers_overview_page() { @@ -525,7 +530,7 @@ public function render_subscribers_overview_page() { if ( ! empty( $_POST['noptin_nonce'] ) && wp_verify_nonce( $_POST['noptin_nonce'], 'noptin' ) ) { // Delete. - if ( ! empty( $_POST['action'] ) && 'delete' == $_POST['action'] ) { + if ( ! empty( $_POST['action'] ) && 'delete' === $_POST['action'] ) { if ( ! empty( $_POST['email'] ) && is_array( $_POST['email'] ) ) { foreach ( $_POST['email'] as $email ) { @@ -569,11 +574,12 @@ public function render_subscribers_overview_page() { /** - * Displays a single subscriber + * Displays a single subscriber. * + * @param int $subscriber The subscriber to display. * @access public * @since 1.1.1 - * @return self::$instance + * @return void */ public function render_single_subscriber_page( $subscriber = 0 ) { @@ -650,17 +656,17 @@ public function save_optin_form() { do_action( 'noptin_before_save_form', $this ); // Prepare the args. - $ID = trim( $_POST['state']['id'] ); + $id = trim( $_POST['state']['id'] ); $state = $_POST['state']; $status = 'draft'; - if ( 'true' == $state['optinStatus'] ) { + if ( true === (bool) $state['optinStatus'] ) { $status = 'publish'; } $postarr = array( 'post_title' => $state['optinName'], - 'ID' => $ID, + 'ID' => $id, 'post_content' => $_POST['html'], 'post_status' => $status, ); @@ -675,20 +681,20 @@ public function save_optin_form() { $_POST['state']['showPostTypes'] = array(); } - update_post_meta( $ID, '_noptin_state', $_POST['state'] ); - update_post_meta( $ID, '_noptin_optin_type', $_POST['state']['optinType'] ); + update_post_meta( $id, '_noptin_state', $_POST['state'] ); + update_post_meta( $id, '_noptin_optin_type', $_POST['state']['optinType'] ); // Ensure impressions and subscriptions are set. // to prevent the form from being hidden when the user sorts by those fields. - $sub_count = get_post_meta( $ID, '_noptin_subscribers_count', true ); - $form_views = get_post_meta( $ID, '_noptin_form_views', true ); + $sub_count = get_post_meta( $id, '_noptin_subscribers_count', true ); + $form_views = get_post_meta( $id, '_noptin_form_views', true ); if ( empty( $sub_count ) ) { - update_post_meta( $ID, '_noptin_subscribers_count', 0 ); + update_post_meta( $id, '_noptin_subscribers_count', 0 ); } if ( empty( $form_views ) ) { - update_post_meta( $ID, '_noptin_form_views', 0 ); + update_post_meta( $id, '_noptin_form_views', 0 ); } /** @@ -706,7 +712,7 @@ public function save_optin_form() { * * @access public * @since 1.0.0 - * @return self::$instance + * @return void */ public function save_optin_form_as_template() { @@ -735,7 +741,7 @@ public function save_optin_form_as_template() { foreach ( $fields as $field ) { - if ( 'optinType' == $field ) { + if ( 'optinType' === $field ) { continue; } @@ -743,12 +749,12 @@ public function save_optin_form_as_template() { $value = stripslashes_deep( $_POST['state'][ $field ] ); - if ( 'false' == $value ) { + if ( 'false' === $value ) { $data[ $field ] = false; continue; } - if ( 'true' == $value ) { + if ( 'true' === $value ) { $data[ $field ] = true; continue; } @@ -782,9 +788,12 @@ public function save_optin_form_as_template() { /** * Retrieves the subscribers list,, limited to 100 * + * @param int $page The page to retrieve. + * @param string $meta_key Filter subscribers by a meta key. + * @param string $meta_value Filter subscribers by a meta value. * @access public * @since 1.0.0 - * @return self::$instance + * @return array|null */ public function get_subscribers( $page = 1, $meta_key = '_subscriber_via', $meta_value = false ) { global $wpdb; @@ -815,7 +824,7 @@ public function get_subscribers( $page = 1, $meta_key = '_subscriber_via', $meta * * @access public * @since 1.0.5 - * @return self::$instance + * @return void */ public function maybe_do_action() { @@ -836,7 +845,7 @@ public function maybe_do_action() { } // Subscriber actions. - if ( isset( $_GET['page'] ) && 'noptin-subscribers' == $_GET['page'] ) { + if ( isset( $_GET['page'] ) && 'noptin-subscribers' === $_GET['page'] ) { // Maybe delete an email subscriber. if ( ! empty( $_GET['delete-subscriber'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'noptin-subscriber' ) ) { @@ -861,10 +870,10 @@ public function maybe_do_action() { } // Campaign actions. - if ( isset( $_GET['page'] ) && 'noptin-email-campaigns' == $_GET['page'] ) { + if ( isset( $_GET['page'] ) && 'noptin-email-campaigns' === $_GET['page'] ) { // Delete multiple campaigns. - if ( ! empty( $_GET['action'] ) && 'delete' == $_GET['action'] && wp_verify_nonce( $_GET['_wpnonce'], 'bulk-ids' ) ) { + if ( ! empty( $_GET['action'] ) && 'delete' === $_GET['action'] && wp_verify_nonce( $_GET['_wpnonce'], 'bulk-ids' ) ) { $ids = array(); if ( isset( $_REQUEST['id'] ) && is_array( $_REQUEST['id'] ) ) { @@ -880,19 +889,19 @@ public function maybe_do_action() { } // Docs page. - if ( isset( $_GET['page'] ) && 'noptin-docs' == $_GET['page'] ) { + if ( isset( $_GET['page'] ) && 'noptin-docs' === trim( $_GET['page'] ) ) { $url = sprintf( 'https://noptin.com/guide/introduction/?utm_medium=plugin-dashboard&utm_campaign=documentation-link&utm_source=%s', urlencode( get_home_url() ) ); wp_redirect( $url, 301 ); exit; } // Tools. - if ( isset( $_GET['page'] ) && 'noptin-tools' == $_GET['page'] && ! empty( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'noptin_tool' ) ) { + if ( isset( $_GET['page'] ) && 'noptin-tools' === $_GET['page'] && ! empty( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'noptin_tool' ) ) { // Sync subscribers. - if ( isset( $_GET['tool'] ) && 'sync_users' == $_GET['tool'] ) { + if ( isset( $_GET['tool'] ) && 'sync_users' === trim( $_GET['tool'] ) ) { - if( get_option( 'noptin_subscribers_syncing' ) ) { + if ( get_option( 'noptin_subscribers_syncing' ) ) { $this->show_error( __( 'Your WordPress users and subscribers are already syncing.', 'newsletter-optin-box' ) ); } else { add_option( 'noptin_subscribers_syncing', 1 ); @@ -901,11 +910,8 @@ public function maybe_do_action() { $this->bg_sync->save()->dispatch(); $this->show_info( __( 'Your WordPress users and subscribers are now syncing in the background.', 'newsletter-optin-box' ) ); } - } - } - // Ensure that this is our page... if ( ! isset( $_GET['page'] ) || 'noptin-forms' !== $_GET['page'] ) { return; @@ -917,21 +923,21 @@ public function maybe_do_action() { } // Is the user deleting an optin form? - if ( 'delete' == $_GET['action'] ) { + if ( 'delete' === $_GET['action'] ) { noptin_delete_optin_form( $_GET['delete'] ); wp_safe_redirect( admin_url( 'admin.php?page=noptin-forms' ) ); exit; } // Is the user duplicating an optin form? - if ( 'duplicate' == $_GET['action'] ) { + if ( 'duplicate' === $_GET['action'] ) { $form = noptin_duplicate_optin_form( $_GET['duplicate'] ); wp_safe_redirect( admin_url( "admin.php?page=noptin-forms&form_id=$form" ) ); exit; } // Is the user creating a new optin form? - if ( 'new' == $_GET['action'] ) { + if ( 'new' === $_GET['action'] ) { $form = noptin_create_optin_form(); if ( is_int( $form ) ) { wp_safe_redirect( admin_url( "admin.php?page=noptin-forms&form_id=$form&created=1" ) ); @@ -946,6 +952,7 @@ public function maybe_do_action() { /** * Displays a success notice * + * @param string $msg The message to qeue. * @access public * @since 1.1.2 */ @@ -957,6 +964,7 @@ public function show_success( $msg ) { * Displays a error notice * * @access public + * @param string $msg The message to qeue. * @since 1.1.2 */ public function show_error( $msg ) { @@ -967,6 +975,7 @@ public function show_error( $msg ) { * Displays a warning notice * * @access public + * @param string $msg The message to qeue. * @since 1.1.2 */ public function show_warning( $msg ) { @@ -977,6 +986,7 @@ public function show_warning( $msg ) { * Displays a info notice * * @access public + * @param string $msg The message to qeue. * @since 1.1.2 */ public function show_info( $msg ) { @@ -989,7 +999,7 @@ public function show_info( $msg ) { * @access public * @since 1.1.2 */ - public function show_notices( $msg ) { + public function show_notices() { foreach ( $this->notices as $type => $messages ) { diff --git a/includes/admin/class-noptin-email-campaigns-admin.php b/includes/admin/class-noptin-email-campaigns-admin.php index 07189441..bdb12b14 100644 --- a/includes/admin/class-noptin-email-campaigns-admin.php +++ b/includes/admin/class-noptin-email-campaigns-admin.php @@ -5,7 +5,7 @@ */ /** - * email campaigns table class. + * Email campaigns table class. */ class Noptin_Email_Campaigns_Admin { @@ -181,12 +181,6 @@ function get_automation_triggers() { 'support_delay' => __( 'After new content is published', 'newsletter-optin-box' ), 'support_filter' => true, ), - /* - 'post_digests' => array( - 'title' => __( "Post Digests", 'newsletter-optin-box' ), - 'description' => __( "Send a daily, weekly or monthly digest of new blog posts.", 'newsletter-optin-box' ), - 'support_filter'=> true, - ),*/ 'welcome_email' => array( 'title' => __( 'Welcome Email', 'newsletter-optin-box' ), 'description' => __( 'Introduce yourself to new subscribers or set up a series of welcome emails to act as an email course.', 'newsletter-optin-box' ), @@ -430,12 +424,12 @@ function maybe_delete_stats( $post_id ) { function maybe_send_campaign( $new_status, $old_status, $post ) { // Maybe abort early. - if ( 'publish' != $new_status || 'publish' == $old_status ) { + if ( 'publish' !== $new_status || 'publish' === $old_status ) { return; } // Ensure this is a newsletter campaign. - if ( 'noptin-campaign' == $post->post_type && 'newsletter' == get_post_meta( $post->ID, 'campaign_type', true ) ) { + if ( 'noptin-campaign' === $post->post_type && 'newsletter' === get_post_meta( $post->ID, 'campaign_type', true ) ) { $this->send_campaign( $post ); } diff --git a/includes/admin/class-noptin-settings.php b/includes/admin/class-noptin-settings.php index abe338d5..ab644609 100644 --- a/includes/admin/class-noptin-settings.php +++ b/includes/admin/class-noptin-settings.php @@ -194,15 +194,6 @@ public static function get_settings() { 'placeholder' => get_option( 'blogname' ), ), - /* - 'company_logo' => array( - 'el' => 'input', - 'type' => 'image', - 'section' => 'sender', - 'label' => __( 'Logo', 'newsletter-optin-box' ), - 'description' => __( "Appears on top of emails. Leave blank to use your website's logo or the default image", 'newsletter-optin-box' ), - ),*/ - 'address' => array( 'el' => 'input', 'section' => 'sender', diff --git a/includes/admin/class-noptin-tools.php b/includes/admin/class-noptin-tools.php index 60d3914a..246d3493 100644 --- a/includes/admin/class-noptin-tools.php +++ b/includes/admin/class-noptin-tools.php @@ -37,7 +37,7 @@ public static function output() { do_action( "noptin_admin_tool_$tool" ); } else { - + /** * Runs when displaying a list of all available tools. * @@ -48,7 +48,7 @@ public static function output() { /** * Runs after displaying the tools page. - * + * * @since 1.2.3 */ do_action( 'noptin_after_admin_tools' ); diff --git a/includes/class-noptin-ajax.php b/includes/class-noptin-ajax.php index ccff0263..3ba2ce57 100644 --- a/includes/class-noptin-ajax.php +++ b/includes/class-noptin-ajax.php @@ -197,21 +197,20 @@ public function import_subscribers() { 'meta' => 'meta', ); - foreach( $subscribers as $subscriber ) { - + foreach ( $subscribers as $subscriber ) { + // Prepare subscriber fields. - foreach( $subscriber as $key => $value ) { + foreach ( $subscriber as $key => $value ) { $lowercase = strtolower( $key ); - if( isset( $mappings[ $lowercase ] ) ) { + if ( isset( $mappings[ $lowercase ] ) ) { $subscriber[ $mappings[ $lowercase ] ] = $value; unset( $subscriber[ $key ] ); } - } // Ensure that there is a unique email address. - if( empty( $subscriber[ 'email' ] ) || ! is_email( $subscriber['email'] ) || noptin_email_exists( $subscriber['email'] ) ) { + if ( empty( $subscriber['email'] ) || ! is_email( $subscriber['email'] ) || noptin_email_exists( $subscriber['email'] ) ) { continue; } @@ -546,7 +545,7 @@ public function add_subscriber() { update_post_meta( $form->ID, '_noptin_subscribers_count', $count + 1 ); // msg. - if ( $form->subscribeAction == 'message' ) { + if ( $form->subscribeAction === 'message' ) { $result['msg'] = $form->successMessage; } else { // redirects. diff --git a/includes/functions.php b/includes/functions.php index 9b12baff..ed86de59 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -94,11 +94,12 @@ function delete_noptin_subscriber_meta( $subscriber_id, $meta_key, $meta_value = * @param string $campaign_id The opened email campaign. * @access public * @since 1.2.0 + * @return void */ function log_noptin_subscriber_campaign_open( $subscriber_id, $campaign_id ) { $opened_campaigns = get_noptin_subscriber_opened_campaigns( $subscriber_id ); - if ( ! in_array( $campaign_id, $opened_campaigns ) ) { + if ( ! in_array( (int) $campaign_id, $opened_campaigns, true ) ) { $opened_campaigns[] = $campaign_id; update_noptin_subscriber_meta( $subscriber_id, '_opened_campaigns', $opened_campaigns ); update_noptin_subscriber_meta( $subscriber_id, "_campaign_{$campaign_id}_opened", 1 ); @@ -121,6 +122,7 @@ function log_noptin_subscriber_campaign_open( $subscriber_id, $campaign_id ) { * @param int $subscriber_id Subscriber ID. * @access public * @since 1.2.0 + * @return int[] Array of opened campaigns. */ function get_noptin_subscriber_opened_campaigns( $subscriber_id ) { @@ -128,7 +130,7 @@ function get_noptin_subscriber_opened_campaigns( $subscriber_id ) { if ( empty( $opened_campaigns ) ) { $opened_campaigns = array(); } - return array_map( 'intval', $opened_campaigns ); + return array_map( 'intval', noptin_parse_list( $opened_campaigns ) ); } @@ -136,13 +138,14 @@ function get_noptin_subscriber_opened_campaigns( $subscriber_id ) { * Checks whether a subscriber opened a given campaign * * @param int $subscriber_id Subscriber ID. + * @param int $campaign_id The campaign to check for. * @access public * @since 1.2.0 */ function did_noptin_subscriber_open_campaign( $subscriber_id, $campaign_id ) { $opened_campaigns = get_noptin_subscriber_opened_campaigns( $subscriber_id ); - return in_array( $campaign_id, $opened_campaigns ); + return in_array( (int) $campaign_id, $opened_campaigns, true ); } @@ -165,8 +168,8 @@ function log_noptin_subscriber_campaign_click( $subscriber_id, $campaign_id, $li $clicked_campaigns[ $campaign_id ] = array(); } - if ( ! in_array( $link, $clicked_campaigns[ $campaign_id ] ) ) { - $clicked_campaigns[ $campaign_id ][] = $link; + if ( ! in_array( $link, $clicked_campaigns[ $campaign_id ], true ) ) { + $clicked_campaigns[ $campaign_id ][] = noptin_clean( $link ); update_noptin_subscriber_meta( $subscriber_id, '_clicked_campaigns', $clicked_campaigns ); update_noptin_subscriber_meta( $subscriber_id, "_campaign_{$campaign_id}_clicked", 1 ); @@ -198,7 +201,9 @@ function get_noptin_subscriber_clicked_campaigns( $subscriber_id ) { /** * Checks whether a subscriber clicked on a link in a given campaign * - * @param int $subscriber_id Subscriber ID. + * @param int $subscriber_id Subscriber ID. + * @param int $campaign_id The campaign id to check for a click from. + * @param string $link Optional. The specific link to check for. * @access public * @since 1.2.0 */ @@ -214,7 +219,7 @@ function did_noptin_subscriber_click_campaign( $subscriber_id, $campaign_id, $li return true; } - return in_array( $link, $clicked_campaigns[ $campaign_id ] ); + return in_array( noptin_clean( $link ), $clicked_campaigns[ $campaign_id ], true ); } @@ -299,6 +304,8 @@ function get_noptin_options() { * Retrieves an option from the db * * @return mixed|null option or null + * @param string $key The option key. + * @param mixed $default The default value for the option. * @access public * @since 1.0.5 */ @@ -325,7 +332,8 @@ function get_noptin_option( $key, $default = null ) { /** * Updates noptin options * - * @return array + * @return void + * @param array $options The updated Noptin options. * @access public * @since 1.0.5 */ @@ -340,7 +348,9 @@ function update_noptin_options( $options ) { /** * Updates a single option * - * @return array + * @return void + * @param string $key The key to update. + * @param mixed $value The new value. * @access public * @since 1.0.5 */ @@ -379,7 +389,7 @@ function get_noptin_action_page() { ) ); - if( empty( $page ) ) { + if ( empty( $page ) ) { return 0; } @@ -395,6 +405,9 @@ function get_noptin_action_page() { * Returns the noptin action url * * @return sting + * @param string $action The action to execute. + * @param string $value Optional. The value to pass to the action handler. + * @param bool $empty Optional. Whether or not to use an empty template. * @access public * @since 1.0.6 */ @@ -577,7 +590,7 @@ function add_noptin_subscriber( $fields ) { // Insert additional meta data. foreach ( $fields as $field => $value ) { - if ( isset( $database_fields[ $field ] ) || 'name' == $field ) { + if ( isset( $database_fields[ $field ] ) || 'name' === $field ) { continue; } @@ -604,7 +617,7 @@ function update_noptin_subscriber( $subscriber_id, $details = array() ) { // Ensure the subscriber exists. $subscriber = get_noptin_subscriber( $subscriber_id ); - if( empty( $subscriber ) ) { + if ( empty( $subscriber ) ) { return false; } @@ -617,8 +630,8 @@ function update_noptin_subscriber( $subscriber_id, $details = array() ) { if ( isset( $fields['name'] ) ) { $names = noptin_split_subscriber_name( $fields['name'] ); - $fields['first_name'] = empty( $fields['first_name'] ) ? $names[0] : trim( $fields['first_name'] ); - $fields['second_name']= empty( $fields['second_name'] ) ? $names[1] : trim( $fields['second_name'] ); + $fields['first_name'] = empty( $fields['first_name'] ) ? $names[0] : trim( $fields['first_name'] ); + $fields['second_name'] = empty( $fields['second_name'] ) ? $names[1] : trim( $fields['second_name'] ); unset( $fields['name'] ); } @@ -632,14 +645,14 @@ function update_noptin_subscriber( $subscriber_id, $details = array() ) { unset( $fields['id'] ); } - foreach( noptin_parse_list( 'email first_name second_name confirm_key date_created active confirmed' ) as $field ) { - if( isset( $fields[ $field ] ) ) { + foreach ( noptin_parse_list( 'email first_name second_name confirm_key date_created active confirmed' ) as $field ) { + if ( isset( $fields[ $field ] ) ) { $to_update[ $field ] = noptin_clean( $fields[ $field ] ); unset( $fields[ $field ] ); } } - if( ! empty( $to_update ) ) { + if ( ! empty( $to_update ) ) { $wpdb->update( $table, $to_update, array( 'id' => $subscriber_id ) ); } @@ -749,7 +762,7 @@ function noptin_subscribers_table_exists() { global $wpdb; $table = get_noptin_subscribers_table_name(); - return $table == $wpdb->get_var( "SHOW TABLES LIKE '$table'" ); + return $table === $wpdb->get_var( "SHOW TABLES LIKE '$table'" ); } /** @@ -762,7 +775,7 @@ function noptin_subscribers_meta_table_exists() { global $wpdb; $table = get_noptin_subscribers_meta_table_name(); - return $table == $wpdb->get_var( "SHOW TABLES LIKE '$table'" ); + return $table === $wpdb->get_var( "SHOW TABLES LIKE '$table'" ); } /** @@ -830,8 +843,9 @@ function noptin_delete_optin_form( $id ) { /** * Duplicates an optin form. - * + * * @since 1.0.5 + * @return int */ function noptin_duplicate_optin_form( $id ) { $form = noptin_get_optin_form( $id ); @@ -841,8 +855,9 @@ function noptin_duplicate_optin_form( $id ) { /** * Returns all optin forms. - * + * * @since 1.0.4 + * @return array */ function noptin_get_optin_forms( $meta_key = '', $meta_value = '', $compare = '=' ) { $args = array( @@ -866,7 +881,7 @@ function noptin_get_optin_forms( $meta_key = '', $meta_value = '', $compare = '= /** * Returns post types. - * + * * @since 1.0.4 */ function noptin_get_post_types() { @@ -888,8 +903,9 @@ function noptin_get_post_types() { /** * Checks whether an optin form should be displayed. - * + * * @since 1.0.7 + * @return bool */ function noptin_should_show_optins() { @@ -907,8 +923,9 @@ function noptin_should_show_optins() { /** * Returns opt-in forms stats. - * + * * @since 1.0.7 + * @return array */ function noptin_get_optin_stats() { global $wpdb; @@ -927,8 +944,9 @@ function noptin_get_optin_stats() { /** * Returns color themess. - * + * * @since 1.0.7 + * @return array */ function noptin_get_color_themes() { return apply_filters( @@ -963,8 +981,9 @@ function noptin_get_color_themes() { /** * Returns optin templates. - * + * * @since 1.0.7 + * @return array */ function noptin_get_optin_templates() { $custom_templates = get_option( 'noptin_templates' ); @@ -982,8 +1001,9 @@ function noptin_get_optin_templates() { /** * Returns opt-in form properties. - * + * * @since 1.0.5 + * @return array */ function noptin_get_form_design_props() { return apply_filters( @@ -1024,8 +1044,9 @@ function noptin_get_form_design_props() { /** * Returns form field props. - * + * * @since 1.0.5 + * @return array */ function noptin_get_form_field_props() { return apply_filters( 'noptin_form_field_props', array( 'fields', 'fieldTypes' ) ); @@ -1034,8 +1055,10 @@ function noptin_get_form_field_props() { /** * Function noptin editor localize. - * + * + * @param array $state the current editor state. * @since 1.0.5 + * @return void */ function noptin_localize_optin_editor( $state ) { $props = noptin_get_form_design_props(); @@ -1061,7 +1084,7 @@ function noptin_localize_optin_editor( $state ) { /** * Function noptin editor localize. - * + * * @since 1.0.5 */ function noptin_form_template_form_props() { @@ -1074,7 +1097,7 @@ function noptin_form_template_form_props() { /** * Function noptin editor localize. - * + * * @since 1.0.5 */ function noptin_form_template_wrapper_props() { @@ -1102,7 +1125,7 @@ function noptin_form_template_wrapper_props() { /** * This will replace the first half of a string with "*" characters. * - * @param string $string + * @param string $string The string to obfuscate. * @since 1.1.0 * @return string */ @@ -1114,6 +1137,9 @@ function noptin_obfuscate_string( $string ) { } /** + * Callback to obfuscate an email address. + * + * @param string $m The mail to obfuscate. * @internal * @ignore */ @@ -1127,7 +1153,7 @@ function _noptin_obfuscate_email_addresses_callback( $m ) { /** * Obfuscates email addresses in a string. * - * @param $string String possibly containing email address. + * @param string $string possibly containing email address. * @since 1.1.0 * @return string */ @@ -1137,8 +1163,9 @@ function noptin_obfuscate_email_addresses( $string ) { /** * Returns a link to add a new newsletter campaign. - * + * * @since 1.2.0 + * @return string */ function get_noptin_new_newsletter_campaign_url() { @@ -1153,8 +1180,10 @@ function get_noptin_new_newsletter_campaign_url() { /** * Returns a link to edit a newsletter. - * + * * @since 1.2.0 + * @param int $id The campaign's id. + * @return string. */ function get_noptin_newsletter_campaign_url( $id ) { @@ -1169,9 +1198,11 @@ function get_noptin_newsletter_campaign_url( $id ) { } /** - * Returns a link to edit an automation campaign. - * + * Returns a link to edit an automation campaign. + * * @since 1.2.0 + * @param int $id The campaign's id. + * @return string. */ function get_noptin_automation_campaign_url( $id ) { @@ -1186,15 +1217,18 @@ function get_noptin_automation_campaign_url( $id ) { } /** - * Checks if a given post is a noptin campaign. - * + * Checks if a given post is a noptin campaign. + * + * @param int|WP_Post $post The post to check for. + * @param bool|string $campaign_type Optional. Specify if you need to check for a specific campaign type. * @since 1.2.0 + * @return bool. */ function is_noptin_campaign( $post, $campaign_type = false ) { $campaign = get_post( $post ); - if ( empty( $campaign ) || 'noptin-campaign' != $campaign->post_type ) { + if ( empty( $campaign ) || 'noptin-campaign' !== $campaign->post_type ) { return false; } @@ -1202,14 +1236,15 @@ function is_noptin_campaign( $post, $campaign_type = false ) { return true; } - return trim( $campaign_type ) == get_post_meta( $campaign->ID, 'campaign_type', true ); + return trim( $campaign_type ) === get_post_meta( $campaign->ID, 'campaign_type', true ); } /** * Returns the default newsletter subject. - * + * * @since 1.2.0 + * @return string */ function get_noptin_default_newsletter_subject() { @@ -1226,8 +1261,9 @@ function get_noptin_default_newsletter_subject() { /** * Returns the default newsletter preview text. - * + * * @since 1.2.0 + * @return string */ function get_noptin_default_newsletter_preview_text() { @@ -1244,8 +1280,9 @@ function get_noptin_default_newsletter_preview_text() { /** * Returns the default newsletter body. - * + * * @since 1.2.0 + * @return string. */ function get_noptin_default_newsletter_body() { @@ -1263,29 +1300,36 @@ function get_noptin_default_newsletter_body() { /** * Returns a path to the includes dir. - * + * + * @param string $append The path to append to the include dir path. + * @return string * @since 1.2.0 */ function get_noptin_include_dir( $append = '' ) { - return get_noptin_plugin_path("includes/$append"); + return get_noptin_plugin_path( "includes/$append" ); } /** * Returns a path to the noptin dir. - * + * * @since 1.2.3 + * @param string $append The path to append to the include dir path. + * @return string */ function get_noptin_plugin_path( $append = '' ) { return plugin_dir_path( Noptin::$file ) . $append; } /** - * Includes a file. - * + * Includes a file. + * + * @param string $file The file path. + * @param array $args Defaults to an empty array. * @since 1.2.0 */ function noptin_ob_get_clean( $file, $args = array() ) { + // phpcs:ignore WordPress.PHP.DontExtract.extract_extract extract( $args ); ob_start(); include $file; @@ -1294,8 +1338,10 @@ function noptin_ob_get_clean( $file, $args = array() ) { } /** - * Notifies the site admin when there is a new subscriber. - * + * Notifies the site admin when there is a new subscriber. + * + * @param int $id The id of the new subscriber. + * @param array $fields The subscription field values. * @since 1.2.0 */ function noptin_new_subscriber_notify( $id, $fields ) { @@ -1372,7 +1418,7 @@ function get_noptin_capability( $capalibilty = 'manage_noptin' ) { * Gets and includes template files. * * @since 1.2.2 - * @param mixed $template_name + * @param mixed $template_name The file name of the template to load. * @param array $args (default: array()). * @param string $template_path (default: 'noptin'). * @param string $default_path (default: 'templates'). @@ -1398,7 +1444,7 @@ function get_noptin_template( $template_name, $args = array(), $template_path = * $default_path / $template_name * * @since 1.2.2 - * @param string $template_name + * @param string $template_name The template's file name. * @param string $template_path (default: 'noptin'). * @param string|bool $default_path (default: 'templates') False to not load a default. * @return string @@ -1433,76 +1479,78 @@ function locate_noptin_template( $template_name, $template_path = 'noptin', $def * Fetches the current user's ip address. * * @since 1.2.3 - * @param string $ip_address * @return string */ function noptin_get_user_ip() { - $ip = ''; - - if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { - $ip = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); - } elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { - $ip = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); - } elseif( !empty( $_SERVER['REMOTE_ADDR'] ) ) { - $ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); - } + $ip = ''; + + if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { + $ip = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); + } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { + $ip = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); + } elseif ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) { + $ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); + } - return apply_filters( 'noptin_get_user_ip', $ip ); + return apply_filters( 'noptin_get_user_ip', $ip ); } /** * GeoLocates an ip address. * * @since 1.2.3 - * @param string $ip_address + * @param string $ip_address Optional. The ip address to located. Default's to the current user's IP Address. * @return bool|array */ function noptin_locate_ip_address( $ip_address = '' ) { // Prepare ip address. - if( empty( $ip_address ) ) { + if ( empty( $ip_address ) ) { $ip_address = noptin_get_user_ip(); } // Retrieve API key. $api_key = get_noptin_option( 'ipgeolocation_io_api_key' ); - if( empty( $api_key ) || empty( $ip_address ) ) { + if ( empty( $api_key ) || empty( $ip_address ) ) { return false; } // Geolocate the ip. - $response = wp_remote_get( 'https://api.ipgeolocation.io/ipgeo', array( - 'apiKey' => $api_key, - 'ip' => $ip_address, - 'fields' => 'city,continent_name,country_name,country_code2,state_prov,zipcode,country_flag,currency,time_zone', - ) ); + $response = wp_remote_get( + 'https://api.ipgeolocation.io/ipgeo', + array( + 'apiKey' => $api_key, + 'ip' => $ip_address, + 'fields' => 'city,continent_name,country_name,country_code2,state_prov,zipcode,country_flag,currency,time_zone', + ) + ); $geo = json_decode( wp_remote_retrieve_body( $response ), true ); - if( empty( $geo ) ) { + if ( empty( $geo ) ) { log_noptin_message( __( 'Error fetching GeoLocation information.', 'newsletter-optin-box' ) ); return false; } - if( ! empty( $geo['time_zone'] ) ) { + if ( ! empty( $geo['time_zone'] ) ) { $geo['time_zone'] = $geo['time_zone']['name']; } - if( ! empty( $geo['currency'] ) ) { + if ( ! empty( $geo['currency'] ) ) { $geo['currency'] = $geo['currency']['name']; } - if( ! empty( $geo['continent_name'] ) ) { + if ( ! empty( $geo['continent_name'] ) ) { $geo['continent'] = $geo['continent_name']; unset( $geo['continent_name'] ); } - if( ! empty( $geo['country_name'] ) ) { + if ( ! empty( $geo['country_name'] ) ) { $geo['country'] = $geo['country_name']; unset( $geo['country_name'] ); } - if( ! empty( $geo['state_prov'] ) ) { + if ( ! empty( $geo['state_prov'] ) ) { $geo['state'] = $geo['state_prov']; unset( $geo['state_prov'] ); } @@ -1510,9 +1558,9 @@ function noptin_locate_ip_address( $ip_address = '' ) { $return = array(); $fields = noptin_parse_list( 'continent country state city zipcode calling_code languages country_flag currency time_zone' ); - foreach( $fields as $field ) { - if( ! empty( $geo[$field] ) ) { - $return[$field] = $geo[$field]; + foreach ( $fields as $field ) { + if ( ! empty( $geo[ $field ] ) ) { + $return[ $field ] = $geo[ $field ]; } } return noptin_clean( $return ); @@ -1546,32 +1594,32 @@ function noptin_clean( $var ) { if ( is_array( $var ) ) { return array_map( 'noptin_clean', $var ); - } + } - if ( is_object( $var ) ) { + if ( is_object( $var ) ) { $object_vars = get_object_vars( $var ); foreach ( $object_vars as $property_name => $property_value ) { $var->$property_name = noptin_clean( $property_value ); - } - return $var; + } + return $var; } - - return is_string( $var ) ? sanitize_text_field( $var ) : $var; + + return is_string( $var ) ? sanitize_text_field( $var ) : $var; } /** * Logs a message. * * @since 1.2.3 - * @param mixed $message - * @param string $code. + * @param mixed $message The message to log. + * @param string $code Optional. The error code. * @see get_logged_noptin_messages * @return bool. */ function log_noptin_message( $message, $code = 'error' ) { // Scalars only please. - if( ! is_scalar( $message ) ) { + if ( ! is_scalar( $message ) ) { $message = print_r( $message, true ); } @@ -1583,7 +1631,7 @@ function log_noptin_message( $message, $code = 'error' ) { // Then, strip some tags. $message = wp_kses( $message, 'user_description' ); - + // Next, retrieve the array of existing logged messages. $messages = get_logged_noptin_messages(); @@ -1607,17 +1655,17 @@ function log_noptin_message( $message, $code = 'error' ) { * @return array. */ function get_logged_noptin_messages() { - + // Retrieve the logged messages. $messages = get_option( 'noptin_logged_messages', array() ); // Ensure it is an array... - if( ! is_array( $messages ) ) { + if ( ! is_array( $messages ) ) { $messages = array(); } // ... of no more than 20 elements. - if( 20 < count( $messages ) ) { + if ( 20 < count( $messages ) ) { $messages = array_slice( $messages, -20 ); update_option( 'noptin_logged_messages', $messages ); } @@ -1635,31 +1683,33 @@ function get_logged_noptin_messages() { * @return void. */ function sync_users_to_noptin_subscribers( $users_to_sync = array() ) { - + // Arrays only please. $users_to_sync = array_filter( array_map( 'absint', noptin_parse_list( $users_to_sync ) ) ); - foreach( array_unique( $users_to_sync ) as $user_id ) { + foreach ( array_unique( $users_to_sync ) as $user_id ) { // Get the user data... $user_info = get_userdata( $user_id ); // ... and abort if it is missing. - if( empty( $user_info ) ) { + if ( empty( $user_info ) ) { continue; } // If the user is not yet subscribed, subscribe them. - add_noptin_subscriber( array( - 'email' => $user_info->user_email, - 'name' => $user_info->display_name, - '_subscriber_via' => 'users_sync' - ) ); + add_noptin_subscriber( + array( + 'email' => $user_info->user_email, + 'name' => $user_info->display_name, + '_subscriber_via' => 'users_sync', + ) + ); // Then update the subscriber. $subscriber = get_noptin_subscriber_by_email( $user_info->user_email ); - if( empty( $subscriber ) ) { + if ( empty( $subscriber ) ) { continue; } @@ -1668,20 +1718,19 @@ function sync_users_to_noptin_subscribers( $users_to_sync = array() ) { $to_update = array( 'description' => $user_info->description, 'website' => esc_url( $user_info->user_url ), - 'wp_user_id' => $user_info->ID, + 'wp_user_id' => $user_info->ID, ); $to_update = apply_filters( 'noptin_sync_users_to_subscribers', $to_update, $subscriber, $user_info ); - foreach( $to_update as $key => $value ) { - if( is_null( $value ) ) { - unset( $to_update[$key] ); + foreach ( $to_update as $key => $value ) { + if ( is_null( $value ) ) { + unset( $to_update[ $key ] ); } } - if( ! empty( $to_update ) ) { + if ( ! empty( $to_update ) ) { update_noptin_subscriber( $subscriber->id, $to_update ); } - } } @@ -1695,17 +1744,17 @@ function sync_users_to_noptin_subscribers( $users_to_sync = array() ) { * @return void. */ function sync_noptin_subscribers_to_users( $subscribers_to_sync = array() ) { - + // Arrays only please. $subscribers_to_sync = array_filter( array_map( 'absint', noptin_parse_list( $subscribers_to_sync ) ) ); - foreach( array_unique( $subscribers_to_sync ) as $subscriber_id ) { + foreach ( array_unique( $subscribers_to_sync ) as $subscriber_id ) { // Get the subscriber data... $subscriber = get_noptin_subscriber( $subscriber_id ); // ... and abort if it is missing. - if( empty( $subscriber ) ) { + if ( empty( $subscriber ) ) { continue; } @@ -1725,12 +1774,14 @@ function sync_noptin_subscribers_to_users( $subscribers_to_sync = array() ) { ); $user_id = wp_insert_user( $args ); - if( is_wp_error( $user_id ) ) { - log_noptin_message( sprintf( - __( 'WordPress returned the error: %s when syncing subscriber %s', 'newsletter-optin-box' ), - $user_id->get_error_message(), - $subscriber->email - )); + if ( is_wp_error( $user_id ) ) { + log_noptin_message( + sprintf( + __( 'WordPress returned the error: %s when syncing subscriber %s', 'newsletter-optin-box' ), + $user_id->get_error_message(), + $subscriber->email + ) + ); continue; } @@ -1747,17 +1798,18 @@ function sync_noptin_subscribers_to_users( $subscribers_to_sync = array() ) { * Generates a unique username for new users. * * @since 1.2.3 - * @param string $prefix + * @param string $prefix The prefix to use for the generated user name. * @return string. */ function noptin_generate_user_name( $prefix = '' ) { - + // If prefix is an email, retrieve the part before the email. - $prefix = strtok( $prefix, '@' ); + $prefix = strtok( $prefix, '@' ); // Trim to 4 characters max. $prefix = sanitize_user( substr( $prefix, 0, 4 ) ); + // phpcs:ignore Generic.Commenting.DocComment.MissingShort /** @ignore */ $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) { diff --git a/includes/load.php b/includes/load.php index 0eab5909..2f4ed2df 100644 --- a/includes/load.php +++ b/includes/load.php @@ -1,37 +1,37 @@ -getMessage() ); require_once $plugin_path . 'includes/load.php'; } @@ -231,7 +240,7 @@ public function init() { do_action( 'before_noptin_init', $this ); // Bg processes. - $this->bg_mailer = new Noptin_Background_Mailer(); + $this->bg_mailer = new Noptin_Background_Mailer(); $this->post_notifications = new Noptin_New_Post_Notify(); $this->post_notifications->init(); @@ -245,8 +254,8 @@ public function init() { $this->post_types = new Noptin_Post_Types(); // Form types. - $this->popups = new Noptin_Popups(); - $this->inpost = new Noptin_Inpost(); + $this->popups = new Noptin_Popups(); + $this->inpost = new Noptin_Inpost(); // Integrations. $this->integrations = new Noptin_Integrations(); @@ -265,6 +274,7 @@ public function init() { /** * Class autoloader * + * @param string $class_name The name of the class to load. * @access public * @since 1.2.3 * @return void @@ -276,15 +286,15 @@ private function autoload( $class_name ) { // Make sure this is our class. if ( false === strpos( $class_name, 'noptin' ) ) { - return; + return; } - - $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php'; + + $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php'; // Load the class. - if( file_exists( "{$plugin_path}includes/$file_name" ) ) { + if ( file_exists( "{$plugin_path}includes/$file_name" ) ) { include "{$plugin_path}includes/$file_name"; - } elseif( file_exists( "{$plugin_path}includes/admin/$file_name" ) ) { + } elseif ( file_exists( "{$plugin_path}includes/admin/$file_name" ) ) { include "{$plugin_path}includes/admin/$file_name"; } @@ -398,7 +408,7 @@ public function register_blocks() { * * @access public * @since 1.0.2 - * @return self::$instance + * @return void */ public function register_widget() { register_widget( 'Noptin_Widget' ); @@ -409,6 +419,7 @@ public function register_widget() { * Filters the body classes * * @access public + * @param array $classes Array of existing class names. * @since 1.1.1 * @return array */ @@ -459,6 +470,8 @@ public function load_plugin_textdomain() { $textdomain = 'newsletter-optin-box'; $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(); + + // phpcs:ignore Generic.Commenting.DocComment.MissingShort /** @ignore */ $locale = apply_filters( 'plugin_locale', $locale, $textdomain ); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index b798ec72..397c01dd 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -15,6 +15,7 @@ */node_modules/* */vendor/* */tests/* + */includes/class-noptin-emogrifier.php @@ -22,6 +23,26 @@ * + + * + + + + * + + + + * + + + + */noptin.php + + + + * + + diff --git a/templates/single-subscriber.php b/templates/single-subscriber.php index 5597769c..f5631054 100644 --- a/templates/single-subscriber.php +++ b/templates/single-subscriber.php @@ -1,7 +1,7 @@
- active ? 'Active' : 'Inactive'; ?> - confirmed ? 'Confirmed' : 'Not Confirmed'; ?> + active ? 'Active' : 'Inactive'; ?> + confirmed ? 'Confirmed' : 'Not Confirmed'; ?>
@@ -51,14 +51,12 @@ } else { $value = esc_html( $value ); } - - } // Rename some fields. - if ( '_subscriber_via' == $key ) { + if ( '_subscriber_via' === $key ) { if ( is_numeric( $value ) ) { $form = noptin_get_optin_form( $value ); diff --git a/uninstall.php b/uninstall.php index b697ff76..b8a5c742 100644 --- a/uninstall.php +++ b/uninstall.php @@ -11,14 +11,12 @@ // Delete subscribers table. $table = $wpdb->prefix . 'noptin_subscribers'; -if ( $wpdb->get_var( "SHOW TABLES LIKE '$table'" ) == $table ) { - $sql = "DROP TABLE $table"; - $wpdb->query( $sql ); +if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) { + $wpdb->query( $wpdb->prepare( 'DROP TABLE %s', $table ) ); } // Delete subscribers meta table. $table = $wpdb->prefix . 'noptin_subscriber_meta'; -if ( $wpdb->get_var( "SHOW TABLES LIKE '$table'" ) == $table ) { - $sql = "DROP TABLE $table"; - $wpdb->query( $sql ); +if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) { + $wpdb->query( $wpdb->prepare( 'DROP TABLE %s', $table ) ); }