Creating Custom Action

Actions play a vital role in automated funnels. Actions are executed after any benchmarks are completed by contact and perform a series of steps on the server-side to move forward with an automated process. But sometimes you need to do something a little special. So we’ve made creating your custom actions super easy!

If you would like to create your custom funnel actions follow these steps. We are going to give an example of Apply Note action of funnel builder.

1. Getting Template

To start building your own benchmark download extension template (click here) and open that in your code editor. For a detailed description of all the classes and functionality please follow the documentation link (click here).

2. Setting Up Defaults

Once you get your template update all the TODO list and personalize your extension.

3. Setting Up your Action

Once all the initialization completes. Create a file for action. You can create file In includes > steps > apply-note.

Open the file and assign a class name for the action. Add namespace for the class using which file is imported using an autoloader. Make sure you entered valid namespace based on the file located inside your project because namespace help autoloader to find the location of the file.

All the actions inside Groundhogg extends the Action class from Groundhogg and action class contains a various abstract method which can help developers to build Action for Groundhogg effectively and easily.

<?php
namespace GroundhoggExtension\Steps;
use Groundhogg\Steps\Actions\Action;

class Apply_Note extends Action
{
    public function get_name()
    {
        // TODO: Implement get_name() method.
    }

    public function get_type()
    {
        // TODO: Implement get_type() method.
    }

    public function get_description()
    {
        // TODO: Implement get_description() method.
    }

    public function get_icon()
    {
        // TODO: Implement get_icon() method.
    }

    public function settings( $step )
    {
        // TODO: Implement settings() method.
    }

    public function save( $step )
    {
        // TODO: Implement save() method.
    }
    
    public function run ()
    {
        //TODO : Implement run() method.
    }
}

4. Initializing Benchmark

Once you have a list of methods which you need to be implemented you can start customizing these methods. You can start with the methods which initialize the action. 

    /**
     * Get the element name
     *
     * @return string
     */
    public function get_name()
    {
        return _x( 'Apply Note', 'step_name', 'groundhogg' );
    }

    /**
     * Get the element type
     *
     * @return string
     */
    public function get_type()
    {
        return 'apply_note';
    }

    /**
     * Get the description
     *
     * @return string
     */
    public function get_description()
    {
        return _x( 'Add a note to the notes section of a contact.', 'step_description', 'groundhogg' );
    }

    /**
     * Get the icon URL
     *
     * @return string
     */
    public function get_icon()
    {
        return GROUNDHOGG_EXTENSION_ASSETS_URL . '/images/apply-note.png';
    }
<br>

5. Implementing and saving settings for Action 

Actions can have different settings and you can create various settings for your actions by overriding the settings method of the Action class. Displaying new settings comes with the responsibility of storing them. You can use the save method of the Action class to store the values of your custom settings.these settings are getting stored in the database when the user clicks on the Update button inside the funnel builder.

Apply note contains one setting which is note content. This note is applied to contact whenever this action is completed. 

    /**
     * @param $step Step
     */
    public function settings( $step )
    {
        $this->start_controls_section();

        $this->add_control( 'note_text', [
            'label'         => __( 'Content:', 'groundhogg' ),
            'type'          => HTML::TEXTAREA,
            'default'       => "This contact is super awesome!",
            'description'   => __( 'Use any valid replacement codes.', 'groundhogg' ),
            'field'         => [
                'cols'  => 64,
                'rows'  => 4
            ],
        ] );
        $this->end_controls_section();
    }

    /**
     * Save the step settings
     *
     * @param $step Step
     */
    public function save( $step )
    {
        $this->save_setting( 'note_text', sanitize_textarea_field( $this->get_posted_data( 'note_text', "" ) ) );
    }
<br>

6. Implementing the run method

The run method is another important method while implementing actions. This method performs the desired operations on the contact this method implementation varies based on the action. In this scenario, the run method adds a note.

    /**
     * Process the apply note step...
     *
     * @param $contact Contact
     * @param $event Event
     *
     * @return true;
     */
    public function run( $contact, $event )
    {
        $note = $this->get_setting( 'note_text' );

        $finished_note = sanitize_textarea_field( Plugin::$instance->replacements->process( $note, $contact->get_id() ) );

        $contact->add_note( $finished_note );

        return true;
    }
<br>

7. Putting all together

The final result of generated action looks like follows.

<?php
namespace GroundhoggExtension\Steps;

use Groundhogg\Contact;
use Groundhogg\Event;
use Groundhogg\HTML;
use Groundhogg\Plugin;
use Groundhogg\Step;

if ( ! defined( 'ABSPATH' ) ) exit;

class Apply_Note extends Action
{ 
    /**
     * get the element name
     *
     * @return string
     */
    public function get_name()
    {
        return _x( 'Apply Note', 'step_name', 'groundhogg' );
    }


    /**
     * Get the element type
     *
     * @return string
     */
    public function get_type()
    {
        return 'apply_note';
    }


    /**
     * Get the description
     *
     * @return string
     */
    public function get_description()
    {
        return _x( 'Add a note to the notes section of a contact.', 'step_description', 'groundhogg' );
    }


    /**
     * Get the icon URL
     *
     * @return string
     */
    public function get_icon()
    {
        return GROUNDHOGG_EXTENSION_ASSETS_URL . '/images/apply-note.png';
    }


    /**
     * @param $step Step
     */
    public function settings( $step )
    {
        $this->start_controls_section();

        $this->add_control( 'note_text', [
            'label'         => __( 'Content:', 'groundhogg' ),
            'type'          => HTML::TEXTAREA,
            'default'       => "This contact is super awesome!",
            'description'   => __( 'Use any valid replacement codes.', 'groundhogg' ),
            'field'         => [
                'cols'  => 64,
                'rows'  => 4
            ],
        ] );

        $this->end_controls_section();
    }


    /**
     * Save the step settings
     *
     * @param $step Step
     */
    public function save( $step )
    {
        $this->save_setting( 'note_text', sanitize_textarea_field( $this->get_posted_data( 'note_text', "" ) ) );
    }

    /**
     * Process the apply note step...
     *
     * @param $contact Contact
     * @param $event Event
     *
     * @return true;
     */
    public function run( $contact, $event )
    {
        $note = $this->get_setting( 'note_text' );

        $finished_note = sanitize_textarea_field( Plugin::$instance->replacements->process( $note, $contact->get_id() ) );

        $contact->add_note( $finished_note );

        return true;
    }
}
<br>

8. Creating Object Of Action

After completing all the steps above your action is ready for the operation but, you will not able to find this action in your funnel yet. To view This action into a funnel builder you need to create an object of an action.

To do that, open the plugin file and add the following code. It registers a new action and now you will be able to see working action in your funnel builder.

FILE: your_extension_folder/includes/plugin.php
  /**
     * Register Benchmark and Action.
     * @param \Groundhogg\Steps\Manager $manager
     */
    public function register_funnel_steps($manager)
    {
        $manager->add_step( new Apply_Notes() );
    }
<br>
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us