Change the operation of the hook myCRED Gravity Forms

Asked

Viewed 204 times

3

I have a site with a scoring system using the plugin for Wordpress myCRED and Gravity Forms.

Mycred offers a hook to automatically give pre-defined points to users each time they send a message via Gravity Forms forms, and as an additional option, you can override the hook’s default settings by creating a hidden field in the form named mycred_amount and the default value for this form.

What I would like to do is change the operation of that hook, so that points are added via wp-admin when an administrator edit the hidden field mycred_amount in the message editor.

My attempt

Original code:

<?php

/**
 * Gravity Forms
 * @since 1.4
 * @version 1.1
 */
if ( defined( 'myCRED_VERSION' ) ) {

    /**
     * Register Hook
     * @since 1.4
     * @version 1.0
     */
    add_filter( 'mycred_setup_hooks', 'gravity_forms_myCRED_Hook' );
    function gravity_forms_myCRED_Hook( $installed ) {
        $installed['gravityform'] = array(
            'title'       => __( 'Gravityform Submissions', 'mycred' ),
            'description' => __( 'Awards %_plural% for successful form submissions.', 'mycred' ),
            'callback'    => array( 'myCRED_Gravity_Forms' )
        );
        return $installed;
    }

    /**
     * Gravity Forms Hook
     * @since 1.4
     * @version 1.1
     */
    if ( ! class_exists( 'myCRED_Gravity_Forms' ) && class_exists( 'myCRED_Hook' ) ) {
        class myCRED_Gravity_Forms extends myCRED_Hook {

            /**
             * Construct
             */
            function __construct( $hook_prefs, $type = 'mycred_default' ) {
                parent::__construct( array(
                    'id'       => 'gravityform',
                    'defaults' => array()
                ), $hook_prefs, $type );
            }

            /**
             * Run
             * @since 1.4
             * @version 1.0
             */
            public function run() {
                add_action( 'gform_after_submission', array( $this, 'form_submission' ), 10, 2 );
            }

            /**
             * Successful Form Submission
             * @since 1.4
             * @version 1.1
             */
            public function form_submission( $lead, $form ) {
                // Login is required
                if ( ! is_user_logged_in() || ! isset( $lead['form_id'] ) ) return;

                // Prep
                $user_id = absint( $lead['created_by'] );
                $form_id = absint( $lead['form_id'] );

                // Make sure form is setup and user is not excluded
                if ( ! isset( $this->prefs[ $form_id ] ) || $this->core->exclude_user( $user_id ) ) return;

                // Default values
                $amount = $this->prefs[ $form_id ]['creds'];
                $entry = $this->prefs[ $form_id ]['log'];

                // See if the form contains myCRED fields that override these defaults
                if ( isset( $form['fields'] ) && ! empty( $form['fields'] ) ) {
                    foreach ( (array) $form['fields'] as $field ) {

                        // Amount override
                        if ( $field['label'] == 'mycred_amount' ) {
                            $amount = $this->core->number( $field['defaultValue'] );
                        }

                        // Entry override
                        if ( $field['label'] == 'mycred_entry' ) {
                            $entry = sanitize_text_field( $field['defaultValue'] );
                        }

                    }
                }

                // Amount can not be zero
                if ( $amount == 0 ) return;

                // Execute
                $this->core->add_creds(
                    'gravity_form_submission',
                    $user_id,
                    $amount,
                    $entry,
                    $form_id,
                    '',
                    $this->mycred_type
                );
            }

            /**
             * Preferences for Gravityforms Hook
             * @since 1.4
             * @version 1.0
             */
            public function preferences() {
                $prefs = $this->prefs;
                $forms = RGFormsModel::get_forms();

                // No forms found
                if ( empty( $forms ) ) {
                    echo '<p>' . __( 'No forms found.', 'mycred' ) . '</p>';
                    return;
                }

                // Loop though prefs to make sure we always have a default setting
                foreach ( $forms as $form ) {
                    if ( ! isset( $prefs[ $form->id ] ) ) {
                        $prefs[ $form->id ] = array(
                            'creds' => 1,
                            'log'   => ''
                        );
                    }
                }

                // Set pref if empty
                if ( empty( $prefs ) ) $this->prefs = $prefs;

                // Loop for settings
                foreach ( $forms as $form ) { ?>

<label for="<?php echo $this->field_id( array( $form->id, 'creds' ) ); ?>" class="subheader"><?php echo $form->title; ?></label>
<ol>
    <li>
        <div class="h2"><input type="text" name="<?php echo $this->field_name( array( $form->id, 'creds' ) ); ?>" id="<?php echo $this->field_id( array( $form->id, 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs[ $form->id ]['creds'] ); ?>" size="8" /></div>
    </li>
    <li class="empty">&nbsp;</li>
    <li>
        <label for="<?php echo $this->field_id( array( $form->id, 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
        <div class="h2"><input type="text" name="<?php echo $this->field_name( array( $form->id, 'log' ) ); ?>" id="<?php echo $this->field_id( array( $form->id, 'log' ) ); ?>" value="<?php echo esc_attr( $prefs[ $form->id ]['log'] ); ?>" class="long" /></div>
        <span class="description"><?php echo $this->available_template_tags( array( 'general' ) ); ?></span>
    </li>
</ol>
<?php           }
            }
        }
    }
}
?>

My amendment (rows 49 and 57):

<?php

/**
 * Gravity Forms
 * @since 1.4
 * @version 1.1
 */
if ( defined( 'myCRED_VERSION' ) ) {

    /**
     * Register Hook
     * @since 1.4
     * @version 1.0
     */
    add_filter( 'mycred_setup_hooks', 'gravity_forms_myCRED_Hook' );
    function gravity_forms_myCRED_Hook( $installed ) {
        $installed['gravityform'] = array(
            'title'       => __( 'Gravityform Submissions', 'mycred' ),
            'description' => __( 'Awards %_plural% for successful form submissions.', 'mycred' ),
            'callback'    => array( 'myCRED_Gravity_Forms' )
        );
        return $installed;
    }

    /**
     * Gravity Forms Hook
     * @since 1.4
     * @version 1.1
     */
    if ( ! class_exists( 'myCRED_Gravity_Forms' ) && class_exists( 'myCRED_Hook' ) ) {
        class myCRED_Gravity_Forms extends myCRED_Hook {

            /**
             * Construct
             */
            function __construct( $hook_prefs, $type = 'mycred_default' ) {
                parent::__construct( array(
                    'id'       => 'gravityform',
                    'defaults' => array()
                ), $hook_prefs, $type );
            }

            /**
             * Run
             * @since 1.4
             * @version 1.0
             */
            public function run() {
                add_action( 'gform_after_update_entry', array( $this, 'update_entry' ), 10, 2 );
            }

            /**
             * Successful Form Submission
             * @since 1.4
             * @version 1.1
             */
            public function update_entry( $form, $entry_id ) {
                // Login is required
                if ( ! is_user_logged_in() || ! isset( $lead['form_id'] ) ) return;

                // Prep
                $user_id = absint( $lead['created_by'] );
                $form_id = absint( $lead['form_id'] );

                // Make sure form is setup and user is not excluded
                if ( ! isset( $this->prefs[ $form_id ] ) || $this->core->exclude_user( $user_id ) ) return;

                // Default values
                $amount = $this->prefs[ $form_id ]['creds'];
                $entry = $this->prefs[ $form_id ]['log'];

                // See if the form contains myCRED fields that override these defaults
                if ( isset( $form['fields'] ) && ! empty( $form['fields'] ) ) {
                    foreach ( (array) $form['fields'] as $field ) {

                        // Amount override
                        if ( $field['label'] == 'mycred_amount' ) {
                            $amount = $this->core->number( $field['defaultValue'] );
                        }

                        // Entry override
                        if ( $field['label'] == 'mycred_entry' ) {
                            $entry = sanitize_text_field( $field['defaultValue'] );
                        }

                    }
                }

                // Amount can not be zero
                if ( $amount == 0 ) return;

                // Execute
                $this->core->add_creds(
                    'gravity_form_submission',
                    $user_id,
                    $amount,
                    $entry,
                    $form_id,
                    '',
                    $this->mycred_type
                );
            }

            /**
             * Preferences for Gravityforms Hook
             * @since 1.4
             * @version 1.0
             */
            public function preferences() {
                $prefs = $this->prefs;
                $forms = RGFormsModel::get_forms();

                // No forms found
                if ( empty( $forms ) ) {
                    echo '<p>' . __( 'No forms found.', 'mycred' ) . '</p>';
                    return;
                }

                // Loop though prefs to make sure we always have a default setting
                foreach ( $forms as $form ) {
                    if ( ! isset( $prefs[ $form->id ] ) ) {
                        $prefs[ $form->id ] = array(
                            'creds' => 1,
                            'log'   => ''
                        );
                    }
                }

                // Set pref if empty
                if ( empty( $prefs ) ) $this->prefs = $prefs;

                // Loop for settings
                foreach ( $forms as $form ) { ?>

<label for="<?php echo $this->field_id( array( $form->id, 'creds' ) ); ?>" class="subheader"><?php echo $form->title; ?></label>
<ol>
    <li>
        <div class="h2"><input type="text" name="<?php echo $this->field_name( array( $form->id, 'creds' ) ); ?>" id="<?php echo $this->field_id( array( $form->id, 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs[ $form->id ]['creds'] ); ?>" size="8" /></div>
    </li>
    <li class="empty">&nbsp;</li>
    <li>
        <label for="<?php echo $this->field_id( array( $form->id, 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
        <div class="h2"><input type="text" name="<?php echo $this->field_name( array( $form->id, 'log' ) ); ?>" id="<?php echo $this->field_id( array( $form->id, 'log' ) ); ?>" value="<?php echo esc_attr( $prefs[ $form->id ]['log'] ); ?>" class="long" /></div>
        <span class="description"><?php echo $this->available_template_tags( array( 'general' ) ); ?></span>
    </li>
</ol>
<?php           }
            }
        }
    }
}
?>

"Basically, "the idea is to use the action gform_after_update_entry instead of gform_after_submission.

All help/suggestion is very welcome, thank you!

1 answer

0

First suggestion is does not edit the original plugin file!

Despite Gravity Forms its a very popular plugin I’ve never worked with it, but from what I understand you already know the action you want to use and the class and function. Just create a function that calls this class with the action you want.

I haven’t tried it, but I believe something like this will work:

Add functions.php to your theme

<?php
//cria a função que vai ser executada na ação
function meu_update_entry($form, $entry_id){
  $update_cred = new myCRED_Gravity_Forms;// cria uma nova instância do myCRED_Gravity_Forms
  $update_cred->update_entry($form, $entry_id); // chama a função da classe que atualiza os pontos
}
//adiciona a ação
add_action("gform_after_update_entry","meu_update_entry", 10, 2);

?>
  • Thank you very much, Ricardo!

  • My first attempts were to filter the class and function via hook in functions.php, but I need this to work right away, as soon as I "run" and understand exactly how it works, I intend to create an addon to avoid updates losing. Your action unfortunately didn’t work, it wouldn’t be better to filter the function form_submission myCRED? example: add_filter("form_submission","meu_update_entry", 10, 2);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.