Advertisement
Guest User

essaChanges

a guest
Sep 3rd, 2020
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.15 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Site plugin for essa.org.uk
  4. Description: Site specific code for essa.org.uk
  5. Version: 0.1
  6. Author: Plain Sailing Web Solutions
  7. Author URI: https://plainsailingweb.com
  8.  
  9. https://eventespresso.com/wiki/customize-checkout-registration-button-text/
  10. for changing the Proceed to Payment Options, Finalize Registration, Update Attendee Information buttons text
  11. Also deals with changing View Details (on event list page) and Register Now (on single event pages)
  12.  
  13. http://eventespresso.com/wiki/ee4-multiple-event-registration-add-on/#customizations
  14. Deals with Event Cart terminology
  15.  
  16. https://gist.github.com/joshfeck/4028f3345d69bd312862b64b94f07e26
  17.  
  18. */
  19.  
  20. class essaChanges
  21. {
  22.     //
  23.     // CLASS ATTRIBUTES
  24.     // field names for Event Espresso registration
  25.     private $address;
  26.     private $address2;
  27.     private $city;
  28.     private $state;
  29.     private $country;
  30.     private $zip;
  31.     private $phone;
  32.     private const MOBILE_PHONE_NUMBER = 'Mobile Phone Number';
  33.     private const GROUP_NAME = 'Name of Scout/Guide/Youth Group if applicable';
  34.     private $custom_questions_to_populate;
  35.     //
  36.     // array for the user data held by wp-members plugin
  37.     private $wpmem_fields;
  38.     //
  39.     // field values for Event Espresso registration
  40.     private $address_value;
  41.     private $address2_value;
  42.     private $city_value;
  43.     private $state_value;
  44.     private $country_iso2_value = 'GB'; // we want to default the country field as we can't remove it.
  45.     private $zip_value;
  46.     private $phone_value;
  47.     private $mobile_phone_value;
  48.     private $group_name_value;
  49.     //
  50.     /**
  51.      * Class constructor.
  52.      */
  53.     final public function __construct()
  54.     {
  55.         # Enable customisation of the CSS in the EE admin screens
  56. #        add_action('admin_enqueue_scripts', array($this,'custom_css_for_ee_admin', 20) );
  57.        #
  58.        # Do not show the admin bar unless the user has administrator role
  59.        add_action('after_setup_theme', array($this,'remove_admin_bar') );
  60.         #
  61.        # Customise the page shown after log in
  62. #        add_filter( 'wpmem_login_redirect', array($this,'my_login_redirect'), 10, 2 );
  63.        #
  64.        # Customise the forgotten password link
  65.        add_filter( 'wpmem_forgot_link', array($this,'my_forgot_link') );
  66.         #
  67.        # Customise the text displayed if you attempt to purchase an event and are not logged in
  68.        add_filter( 'gettext', array($this,'tw_ee_mycustom_filter_gettext'), 10, 3 );
  69.         #
  70.        # Customise the text of the submit buttons in the checkout registration process
  71. #        add_filter( 'FHEE__EE_SPCO_Reg_Step__set_submit_button_text___submit_button_text',
  72. #            array($this,'ee_spco_submit_button_text'), 10, 2 );
  73.        #
  74.        # Get the value for certain fields from the user record
  75.        add_filter( 'FHEE__EEM_Answer__get_attendee_question_answer_value__answer_value',
  76.             array($this,'jf_filter_address_answer_for_wpuser'), 10, 4 );
  77.         #
  78.        # This should retrieve previous answer value for emergency contact information from previous registrations
  79.        add_filter('FHEE__EE_SPCO_Reg_Step_Attendee_Information___generate_question_input__input_constructor_args',
  80.             array($this,'my_question_input'), 10, 4);
  81.         #
  82.        # Renaming the screens for wp-members plugin to Account to avoid confusion with registering for an event
  83.        add_filter( 'wpmem_default_text', array($this, 'my_text_for_wp_members') );
  84.         #
  85.        $this->custom_questions_to_populate = array
  86.         (   self::GROUP_NAME
  87.         ,   self::MOBILE_PHONE_NUMBER
  88.         ,   'Emergency Contact Name (15)'
  89.         ,   'Emergency Contact Phone Number (16)'
  90.         ,   'Emergency Contact Relationship (17)'
  91.         );
  92.     }
  93.     #
  94.    # PRIVATE CLASS METHODS
  95.    private function user_data()
  96.     {
  97.         if ( count($this->wpmem_fields) == 0)
  98.         {
  99.             $this->wpmem_fields = WP_Members_User::user_data();
  100.             #
  101.            $this->address_value = $this->wpmem_fields["billing_address_1"];
  102.             $this->address2_value = $this->wpmem_fields["billing_address_2"];
  103.             $this->city_value = $this->wpmem_fields["billing_city"];
  104.             $this->state_value = $this->wpmem_fields["billing_state"];
  105.             $this->zip_value = $this->wpmem_fields["billing_postcode"];
  106.             $this->phone_value = $this->wpmem_fields["billing_phone"];
  107.             #
  108.            $this->mobile_phone_value = $this->wpmem_fields["mobile_phone_number"];
  109.             $this->group_name_value = $this->wpmem_fields["group_name"];
  110.         }
  111.         return $wpmem_fields;
  112.     }
  113.     #
  114.    # CLASS METHODS
  115.    public function custom_css_for_ee_admin()
  116.     {
  117.         $css_txn = '.transactions .column-TXN_ID {
  118.                width: 8%
  119.            }
  120.            .transactions .column-TXN_timestamp {
  121.                width: 16%;
  122.            }
  123.            .transactions .column-actions {
  124.                width: 16%;
  125.            }';
  126.         wp_add_inline_style('espresso_txn', $css_txn);
  127.         #
  128.        $css_event = 'select#venue {display:none;}';
  129.         wp_add_inline_style('events-admin-css', $css_event);
  130.     }
  131.     #
  132.    public function my_text_for_wp_members( $text )
  133.     {
  134.         $text['login_heading'] = 'Account holder log in';
  135.         $text['register_link_before'] = '';
  136.         $text['register_link'] = '';
  137.         $text['profile_edit'] = 'Edit my Account';
  138.         $text['profile_heading'] = 'Edit my Account';
  139.         $text['profile_submit'] = 'Update Account';
  140.         $text['register_submit'] = 'Create Account';
  141.         $text['register_heading'] = 'New Account Creation';
  142.         $text['success'] = 'Congratulations! Your account was created successfully.<br /><br />You may now log in using the password you entered. It has also been emailed to you.';
  143.  
  144.         return $text;
  145.     }
  146.     public function remove_admin_bar()
  147.     {
  148.         if ( !current_user_can('administrator') && !is_admin() )
  149.         {
  150.             show_admin_bar(false);
  151.         }
  152.     } # end remove_admin_bar
  153.    #
  154.    public function my_login_redirect( $redirect_to, $user_id )
  155.     {
  156.         return home_url( '/registration-checkout/?event_cart=view#checkout' );
  157.     } # end my_login_redirect
  158.    #
  159.    public function my_forgot_link( $str )
  160.     {
  161.         return get_site_url() . '/forgotten-password/';
  162.     } # end my_forgot_link
  163.    #
  164.    # Customise the text db2_field_display_size(stmt, column)ed if you attempt to purchase an event and are not logged in
  165.    public function tw_ee_mycustom_filter_gettext( $translated, $original, $domain )
  166.     {
  167.         // This is an array of original strings
  168.         // and what they should be replaced with
  169.         $strings = array(
  170.             'The event you have selected requires logging in before you can register. You can %sregister for an account here%s if you don\'t have a login.' =>
  171.             'To purchase the event(s) you have selected, you need to be logged in to your account. Please enter your account username and password to log in. If you don\'t already have an account, please click "Create an Account".<br /><br /><button>%sCreate an Account%s</button><br /><br />',
  172.         );
  173.         #
  174.        # See if the current string is in the $strings array
  175.        # If so, replace its translation
  176.        if ( isset( $strings[$original] ) ) {
  177.             # This accomplishes the same thing as __()
  178.            # but without running it through the filter again
  179.            $translations = get_translations_for_domain( $domain );
  180.             $translated = $translations->translate( $strings[$original] );
  181.         }
  182.         return $translated;
  183.     } # end tw_ee_mycustom_filter_gettext
  184.    #
  185.    # Customise the button text for buttons in the Checkout/registration process
  186.    public function ee_filter_spco_submit_button_text( $submit_button_text, EE_Checkout $checkout )
  187.     {
  188.         if ( ! $checkout instanceof EE_Checkout ||
  189.              ! $checkout->current_step instanceof EE_SPCO_Reg_Step ||
  190.              ! $checkout->next_step instanceof EE_SPCO_Reg_Step )
  191.         {
  192.             return $submit_button_text;
  193.         }
  194.  
  195.         // $checkout->revisit - whether this is the first time thru SPCO or not : false = first visit, true = return visit (ie repay or edit)
  196.  
  197.         // details for the *current* reg step
  198.         // $checkout->current_step->slug();  ex: 'attendee_information', 'payment_options', 'finalize_registration'
  199.         // $checkout->current_step->name();
  200.  
  201.         // details for the NEXT SPCO Reg Step that follows the CURRENT SPCO Reg Step
  202.         // $checkout->next_step->slug();
  203.         // $checkout->next_step->name();
  204.  
  205.         if ( $checkout->next_step->slug() == 'finalize_registration' )
  206.         {
  207.             $submit_button_text = __( 'Go to Finalise Registration step', 'event_espresso' );
  208.         }
  209.         else if ( $checkout->current_step->slug() == 'attendee_information' && $checkout->revisit )
  210.         {
  211.             $submit_button_text =
  212.                 sprintf( __( 'Welcome back, you can go to %1$s', 'event_espresso' ), $checkout->next_step->name() );
  213.         }
  214.         else if ( $checkout->next_step->slug() == 'attendee_information' )
  215.         {
  216.             $submit_button_text = sprintf( __( 'Login and go to %1$s step', 'event_espresso' ), $checkout->next_step->name() );
  217.         }
  218.         else {
  219.             $submit_button_text = sprintf( __( 'Finish %1$s and go to %2$s', 'event_espresso' ), $checkout->current_step->name(), $checkout->next_step->name() );
  220.         }
  221.         return $submit_button_text;
  222.     } # end ee_filter_spco_submit_button_text
  223.    #
  224.    # PRIVATE METHOD CALLED BY THE NEXT FUNCTION
  225.    private function getSystemQuestionNames()
  226.     {
  227.         if( class_exists( 'EED_WP_Users_SPCO' ) )
  228.         {
  229.             global $current_user;
  230.             $attendee = EED_WP_Users_SPCO::get_attendee_for_user( $current_user );
  231.             if(  defined( 'EEM_Attendee::system_question_address' ) )
  232.             {
  233.                 $this->address = EEM_Attendee::system_question_address;
  234.                 $this->address2 = EEM_Attendee::system_question_address2;
  235.                 $this->city = EEM_Attendee::system_question_city;
  236.                 $this->state = EEM_Attendee::system_question_state;
  237.                 $this->country = EEM_Attendee::system_question_country;
  238.                 $this->zip = EEM_Attendee::system_question_zip;
  239.                 $this->phone = EEM_Attendee::system_question_phone;
  240.             }
  241.         }
  242.     } # end getSystemQuestionNames
  243.    #
  244.    public function jf_filter_address_answer_for_wpuser(
  245.         $value, EE_Registration $registration, $question_id, $system_question_name = null )
  246.     {
  247.         // only fill for primary registrant and not in admin area
  248.         if ( ! $registration->is_primary_registrant() || is_admin() )
  249.         {
  250.             return $value;
  251.         }
  252.         //
  253.         if ( empty($value) )
  254.         {
  255.             $this->getSystemQuestionNames();
  256.             //
  257.             // first, see if we can get the values from wp-members then test whether there is any data. For a user who
  258.             // registered before this upgrade and has not amended their user profile, this may be true. In this case,
  259.             // get the system fields values from a previous registration
  260.             $this->wpmem_fields = $this->user_data();
  261.             if ( count($this->wpmem_fields) == 0 )
  262.             {
  263.                 if ( $current_user instanceof WP_User && $attendee instanceof EE_Attendee )
  264.                 {
  265.                     $this->address_value = $attendee->get( 'ATT_address' );
  266.                     $this->address2_value = $attendee->get( 'ATT_address2' );
  267.                     $this->city_value = $attendee->get( 'ATT_city' );
  268.                     $this->state_value = $attendee->get( 'STA_ID' );
  269.                     $this->zip_value = $attendee->get( 'ATT_zip' );
  270.                     $this->phone_value = $attendee->get( 'ATT_phone' );
  271.                 }
  272.             }
  273.             //
  274.             switch ( $system_question_name )
  275.             {
  276.                 case $this->address :
  277.                     $value = $this->address_value;
  278.                     break;
  279.                 case $this->address2 :
  280.                     $value = $this->address2_value;
  281.                     break;
  282.                 case $this->city :
  283.                     $value = $this->city_value;
  284.                     break;
  285.                 case $this->country :
  286.                     $value = $this->country_iso2_value;
  287.                     break;
  288.                 case $this->state :
  289.                     $value = $this->state_value;
  290.                     break;
  291.                 case $this->zip :
  292.                     $value = $this->zip_value;
  293.                     break;
  294.                 case $this->phone :
  295.                     $value = $this->phone_value;
  296.                     break;
  297.                 default:
  298.             }
  299.         }
  300.         return $value;
  301.     } # end jf_filter_address_answer_for_wpuser
  302.    #
  303.    /**
  304.      * Code snippet that extends the Event Espresso WP User integration addon,
  305.      * so that we also remember the user's last answer to custom questions too.
  306.      */
  307.     function my_question_input(
  308.         $input_args, EE_Registration $registration = null, EE_Question $question = null, EE_Answer $answer = null )
  309.     {
  310.         /**
  311.         For this section we only want to fill in the mobile phone number and the group name from $this->wpmem_fields
  312.         We can leave the emergency contact details being populated as they are from previous registrations
  313.         BUT we need to ensure we do not pre-populate the following question:
  314.         $question = #I agree to ESSA's booking terms & conditions (23)#
  315.         nor any of the participant information
  316.         */
  317.  
  318.         if  (   is_admin() ||
  319.                 ! in_array($question->display_text(), $this->custom_questions_to_populate) ||
  320.                 ! $registration->is_primary_registrant() ||
  321.                 $question->system_ID  )
  322.         {
  323.             return $input_args;
  324.         }
  325.         #
  326.        if ( class_exists( 'EED_WP_Users_SPCO' ) )
  327.         {
  328.             switch ($question->display_text() )
  329.             {
  330.                 case self::MOBILE_PHONE_NUMBER :
  331.                     $input_args[ 'default' ] = $this->mobile_phone_value;
  332.                     break;
  333.                
  334.                 case self::GROUP_NAME :
  335.                     $input_args[ 'default' ] = $this->group_name_value;
  336.                     break;
  337.                
  338.                 default:
  339.                     # this code executes for the three emergency contact questions
  340.                    global $current_user;
  341.                     $attendee = EED_WP_Users_SPCO::get_attendee_for_user( $current_user );
  342.                     if (    $question instanceof EE_Question &&
  343.                             $registration instanceof EE_Registration &&
  344.                             $attendee instanceof EE_Attendee )
  345.                     {
  346.                         $prev_answer_value = EEM_Answer::instance()->get_var(
  347.                                 array(
  348.                                     array(
  349.                                         'Registration.ATT_ID' => $attendee->ID(),
  350.                                         'QST_ID' => $question->ID()
  351.                                     ),
  352.                                     'order_by' => array(
  353.                                         'ANS_ID' => 'DESC'
  354.                                     ),
  355.                                     'limit' => 1
  356.                                 ),
  357.                                 'ANS_value' );
  358.                     }
  359.                     #
  360.                    if ( $prev_answer_value )
  361.                     {
  362.                         $field_obj = EEM_Answer::instance()->field_settings_for( 'ANS_value' );
  363.                         $prev_answer_value = $field_obj->prepare_for_get( $field_obj->prepare_for_set_from_db( $prev_answer_value ) );
  364.                         $input_args[ 'default' ] = $prev_answer_value;
  365.                     }  
  366.                     break;
  367.             } # end switch
  368.        }
  369.         return $input_args;
  370.     } # end my_question_input
  371. } # end class essaChanges
  372.  
  373. $essaChanges = new essaChanges();
  374.  
  375. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement