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 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 own custom actions super easy!

If you would like to create your own Funnel Actions follow these steps. We are going to give the example of adding notes to contact.

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 documentation link (click here).

2. Setting Up Defaults

Once 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.

Implement the abstract method.

<?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. Initialize Action

You need to implement all the methods inherited in the provious step to implement your action. You can assign basic property of action by implementing the following methods.

  /**
     * 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';
    }

5. Implementing and saving settings for Action

Apply note contains one setting which is note content. This note is applied to contact whenever this action is completed. Implement settings method to create settings for the action. Storing operation of these settings can be achieved by implementing a save method of the action.


    /**
     * @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', "" ) ) );
    }

6. Implementing 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;

    }

7. Putting all together

The final result of generated action looks like follow.

<?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
{ 
    /**
     * ] et 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;

    }
}

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 funnel builder you need to create an object of an action.

To do that, open 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.

   /**
     * Register Benchmark and Action.
     * @param \Groundhogg\Steps\Manager $manager
     */
    public function register_funnel_steps($manager)
    {
        $manager->add_step( new Apply_Notes() );
    }
Was this article helpful to you? Yes No

How can we help?