base = OMAPI::get_instance();
/*
* Increased priority to get around: https://github.com/elementor/elementor/issues/19709
*/
add_action( 'wp_enqueue_scripts', array( $this, 'register_js' ), 999 );
}
/**
* Register widget JS.
*
* @since 2.16.2
*/
public function register_js() {
$script_id = $this->base->plugin_slug . '-elementor';
wp_register_script(
$script_id,
$this->base->url . 'assets/dist/js/elementor.min.js',
array( 'jquery' ),
$this->base->asset_version(),
true
);
OMAPI_Utils::add_inline_script( $script_id, 'OMAPI', $this->base->blocks->get_data_for_js() );
}
/**
* Get widget name.
*
* Retrieve shortcode widget name.
*
* @see https://code.elementor.com/methods/elementor-controls_stack-get_name/
*
* @since 2.2.0
*
* @return string Widget name.
*/
public function get_name() {
return 'optinmonster';
}
/**
* Get widget title.
*
* Retrieve widget title.
*
* @see https://code.elementor.com/methods/elementor-element_base-get_title/
*
* @since 2.2.0
*
* @return string Widget title.
*/
public function get_title() {
return __( 'OptinMonster', 'optin-monster-api' );
}
/**
* Get widget icon.
*
* Retrieve widget icon.
*
* @see https://code.elementor.com/methods/elementor-widget_base-get_icon/
*
* @since 2.2.0
*
* @return string Widget icon.
*/
public function get_icon() {
return 'icon-optinmonster';
}
/**
* Get widget keywords.
*
* Retrieve the list of keywords the widget belongs to.
*
* @see https://code.elementor.com/methods/elementor-widget_base-get_keywords/
*
* @since 2.2.0
*
* @return array Widget keywords.
*/
public function get_keywords() {
return array(
'popup',
'form',
'forms',
'campaign',
'email',
'conversion',
'contact form',
);
}
/**
* Get widget categories.
*
* @see https://code.elementor.com/methods/elementor-widget_base-get_categories/
*
* @since 2.2.0
*
* @return array Widget categories.
*/
public function get_categories() {
return array(
'basic',
);
}
/**
* Handle registering elementor editor JS assets.
*
* @see https://code.elementor.com/methods/elementor-element_base-get_script_depends/
*
* @since 2.2.0
*
* @return array
*/
public function get_script_depends() {
return Plugin::instance()->preview->is_preview_mode()
? array( $this->base->plugin_slug . '-elementor' )
: array();
}
/**
* Handle registering elementor editor CSS assets.
*
* @see https://code.elementor.com/methods/elementor-element_base-get_style_depends/
*
* @since 2.2.0
*
* @return array
*/
public function get_style_depends() {
$css_handle = $this->base->plugin_slug . '-elementor-frontend';
wp_register_style(
$css_handle,
$this->base->url . 'assets/dist/css/elementor-frontend.min.css',
array(),
$this->base->asset_version()
);
return array( $css_handle );
}
/**
* Register widget controls.
*
* Adds different input fields to allow the user to change and customize the widget settings.
*
* @see https://code.elementor.com/methods/elementor-controls_stack-_register_controls/
*
* @since 2.2.0
*/
protected function register_controls() {
$this->start_controls_section(
'section_om_campaign',
array(
'label' => esc_html__( 'OptinMonster Campaign', 'optin-monster-api' ),
'tab' => Controls_Manager::TAB_CONTENT,
)
);
if ( ! $this->base->blocks->has_sites() ) {
$this->no_sites_controls();
} elseif ( ! $this->has_inline_campaigns() ) {
$this->no_campaign_controls();
} else {
$this->campaign_controls();
}
$this->end_controls_section();
}
/**
* Register no-site controls.
*
* @since 2.2.0
*/
protected function no_sites_controls() {
$i18n = $this->base->blocks->get_data_for_js( 'i18n' );
$this->add_control(
'add_om_campaign_notice',
array(
'show_label' => false,
'type' => Controls_Manager::RAW_HTML,
'raw' => '
' . esc_html__( 'You Have Not Connected to OptinMonster', 'optin-monster-api' ) . '
' . esc_html__( 'Please create a Free Account or Connect an Existing Account', 'optin-monster-api' ) . '
',
'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
)
);
$this->add_control(
'om_create_account',
array(
'show_label' => false,
'label_block' => false,
'type' => Controls_Manager::BUTTON,
'button_type' => 'default',
'text' => $i18n['no_sites_button_create_account'],
'event' => 'elementorOMAPICreateAccount',
)
);
$this->add_control(
'om_connect_account',
array(
'show_label' => false,
'label_block' => false,
'type' => Controls_Manager::BUTTON,
'button_type' => 'default',
'separator' => 'after',
'text' => $i18n['no_sites_button_connect_account'],
'event' => 'elementorOMAPIConnectAccount',
)
);
}
/**
* Register no-campaign controls.
*
* @since 2.2.0
*/
protected function no_campaign_controls() {
$this->add_control(
'add_om_campaign_notice',
array(
'show_label' => false,
'type' => Controls_Manager::RAW_HTML,
'raw' => wp_kses(
'' . __( 'No inline campaigns available!', 'optin-monster-api' ) . '',
array(
'b' => array(),
)
),
'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
)
);
$this->add_control(
'add_campaign_btn',
array(
'show_label' => false,
'label_block' => false,
'type' => Controls_Manager::BUTTON,
'button_type' => 'default',
'separator' => 'after',
'text' => '+' . esc_html__( 'Create New Inline Campaign', 'optin-monster-api' ),
'event' => 'elementorOMAPIAddInlineBtnClick',
)
);
}
/**
* Register campaign controls.
*
* @since 2.2.0
*/
protected function campaign_controls() {
$campaigns = $this->base->blocks->get_campaign_options( true );
$campaigns = array_merge( array( '' => esc_html__( 'Select Campaign...', 'optin-monster-api' ) ), $campaigns['inline'] );
$this->add_control(
'campaign_id',
array(
'label' => esc_html__( 'Select Campaign', 'optin-monster-api' ),
'type' => Controls_Manager::SELECT,
'frontend_available' => true,
'label_block' => true,
'options' => $campaigns,
'default' => '',
)
);
$this->add_control(
'followrules',
array(
'label' => esc_html__( 'Use Output Settings', 'optin-monster-api' ),
'type' => Controls_Manager::SWITCHER,
'frontend_available' => true,
'label_on' => esc_html__( 'Yes', 'optin-monster-api' ),
'label_off' => esc_html__( 'No', 'optin-monster-api' ),
'return_value' => 'yes',
'condition' => array(
'campaign_id!' => '0',
),
)
);
$this->add_control(
'edit_campaign',
array(
'show_label' => false,
'type' => Controls_Manager::RAW_HTML,
'raw' => sprintf(
wp_kses(
/* translators: %s - OptinMonster edit link. */
__( 'Need to make changes? Edit the selected campaign.', 'optin-monster-api' ),
array(
'a' => array(
'href' => array(),
'class' => array(),
'target' => array(),
'rel' => array(),
),
)
),
esc_url( $this->base->blocks->get_data_for_js( 'editUrl' ) )
),
'condition' => array(
'campaign_id!' => '0',
),
)
);
$this->add_control(
'add_campaign_btn',
array(
'show_label' => false,
'label_block' => false,
'type' => Controls_Manager::BUTTON,
'button_type' => 'default',
'separator' => 'before',
'text' => '+' . esc_html__( 'Create New Inline Campaign', 'optin-monster-api' ),
'event' => 'elementorOMAPIAddInlineBtnClick',
)
);
}
/**
* Render widget output.
*
* @see https://code.elementor.com/methods/elementor-element_base-render/
*
* @since 2.2.0
*/
protected function render() {
if ( Plugin::instance()->editor->is_edit_mode() ) {
$this->render_edit_mode();
} else {
$this->render_frontend();
}
}
/**
* Get the editing-block render format.
*
* @since 2.2.0
*
* @return string Format html string.
*/
protected function get_render_format() {
return '
%2$s
%3$s
' . esc_html__( 'OptinMonster Campaign Error:', 'optin-monster-api' ) . '
';
}
/**
* Get the campaign-selector html.
*
* @since 2.2.0
*
* @param bool $icon Whether to include Archie icon.
*
* @return string Html string.
*/
protected function get_campaign_select_html( $icon = true ) {
$data = $this->base->blocks->get_data_for_js();
if ( ! $this->base->blocks->has_sites() ) {
$guts = '
';
} elseif ( ! $this->has_inline_campaigns() ) {
$guts = '
';
} else {
$guts = '
' . ( $icon ? '

' : '' ) . '
OptinMonster
' . esc_html__( 'Select and display your email marketing form or smart call-to-action campaigns from OptinMonster.', 'optin-monster-api' ) . '
';
}
return '' . $guts . '
';
}
/**
* Get the campaign holder html.
*
* @since 2.2.0
*
* @param string $campaign_id Campaign Id string.
*
* @return string Html.
*/
public function get_campaign_holder( $campaign_id ) {
return sprintf(
'

' . esc_html__( 'Loading Campaign...', 'optin-monster-api' ) . '
',
$campaign_id
);
}
/**
* Render widget output in edit mode.
*
* @since 2.2.0
*/
protected function render_edit_mode() {
$campaign_id = esc_attr( $this->get_settings_for_display( 'campaign_id' ) );
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
printf(
$this->get_render_format(),
$campaign_id,
! $campaign_id ? $this->get_campaign_select_html() : '',
$campaign_id ? $this->get_campaign_holder( $campaign_id ) : ''
);
// phpcs:enable
}
/**
* This method is used by the parent methods to output the backbone/underscore template.
*
* @see https://code.elementor.com/methods/elementor-element_base-_content_template/
*
* @since 2.2.0
*/
protected function content_template() {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
printf(
$this->get_render_format(),
'{{{ settings.campaign_id.replace(/[^a-zA-Z0-9]/g, "") }}}',
'<# if ( ! settings.campaign_id ) { #>' . $this->get_campaign_select_html() . '<# } #>',
'<# if ( settings.campaign_id ) { #>' . $this->get_campaign_holder( '{{{ settings.campaign_id.replace(/[^a-zA-Z0-9]/g, "") }}}' ) . '<# } #>'
);
// phpcs:enable
}
/**
* Render widget output on the frontend.
*
* @since 2.2.0
*/
protected function render_frontend() {
echo do_shortcode( $this->get_shortcode_output() );
}
/**
* Render widget as plain content.
*
* @see https://code.elementor.com/methods/elementor-widget_base-render_plain_content/
*
* @since 2.2.0
*/
public function render_plain_content() {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $this->get_shortcode_output();
}
/**
* Render shortcode.
*
* @since 2.2.0
*
* @return string Shortcode
*/
protected function get_shortcode_output() {
return sprintf(
'[optin-monster slug="%1$s"%2$s]',
esc_attr( $this->get_settings_for_display( 'campaign_id' ) ),
$this->get_settings_for_display( 'followrules' ) === 'yes' ? ' followrules="true"' : ''
);
}
/**
* Does the user have any inline campaigns created?
*
* @since 2.2.0
*
* @return boolean
*/
protected function has_inline_campaigns() {
$campaigns = $this->base->blocks->get_campaign_options();
return ! empty( $campaigns['inline'] );
}
}