logger = $logger;
$this->get_logger()->debug( 'Admin constructor called', [
'plugin' => 'double-opt-in',
'class' => __CLASS__,
'method' => __METHOD__,
] );
add_action( 'admin_init', [ $this, 'addHooks' ] );
$this->get_logger()->debug( 'Hook admin_init registered', [
'plugin' => 'double-opt-in',
] );
add_action( 'admin_enqueue_scripts', [ $this, 'addStyles' ] );
$this->get_logger()->debug( 'Hook admin_enqueue_scripts registered', [
'plugin' => 'double-opt-in',
] );
}
public function get_logger() {
return $this->logger;
}
/**
* Add the styles for the form
*/
public function addStyles( $hook ) {
$this->get_logger()->debug( 'Enqueuing admin styles and scripts', [
'plugin' => 'double-opt-in',
'class' => __CLASS__,
'method' => __METHOD__,
'hook' => $hook,
] );
wp_enqueue_script(
'f12-cf7-doubleoptin-admin',
plugins_url( 'assets/f12-cf7-popup.js', __FILE__ ),
[ 'jquery' ]
);
$this->get_logger()->debug( 'Admin popup script enqueued', [
'plugin' => 'double-opt-in',
] );
wp_localize_script(
'f12-cf7-doubleoptin-admin',
'doi',
[
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'f12_doi_details' ),
]
);
$this->get_logger()->debug( 'Admin popup script localized', [
'plugin' => 'double-opt-in',
] );
wp_enqueue_script(
'f12-cf7-doubleoptin-templateloader',
plugins_url( 'assets/f12-cf7-templateloader.js', __FILE__ ),
[ 'jquery' ]
);
$this->get_logger()->debug( 'Template loader script enqueued', [
'plugin' => 'double-opt-in',
] );
wp_localize_script(
'f12-cf7-doubleoptin-templateloader',
'templateloader',
[
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'f12_doi_templateloader' ),
'label_placeholder' => __( 'Please wait while we load the template...', 'double-opt-in' ),
]
);
$this->get_logger()->debug( 'Template loader script localized', [
'plugin' => 'double-opt-in',
] );
}
/**
* Add the hooks responsible to handle wordpress functions
*/
public function addHooks() {
$this->get_logger()->debug( 'Registering CF7 admin hooks', [
'plugin' => 'double-opt-in',
'class' => __CLASS__,
'method' => __METHOD__,
] );
add_filter( 'wpcf7_editor_panels', [ $this, 'addPanel' ], 10, 1 );
$this->get_logger()->debug( 'Filter wpcf7_editor_panels registered', [
'plugin' => 'double-opt-in',
] );
add_action( 'wpcf7_save_contact_form', [ $this, 'save' ], 10, 3 );
$this->get_logger()->debug( 'Action wpcf7_save_contact_form registered', [
'plugin' => 'double-opt-in',
] );
}
/**
* Add a custom panel to the contact form 7 options
*/
public function addPanel( array $panels ) {
$this->get_logger()->debug( 'Adding CF7 editor panel', [
'plugin' => 'double-opt-in',
'class' => __CLASS__,
'method' => __METHOD__,
] );
$panels['optin'] = [
'title' => __( 'Double-Opt-in', 'double-opt-in' ),
'callback' => [ $this, 'render' ],
];
$this->get_logger()->debug( 'CF7 editor panel added', [
'plugin' => 'double-opt-in',
] );
return $panels;
}
/**
* On Contact Form save store the information in the database
*
* @param \WPCF7_ContactForm $contact_form
* @param $args
* @param $context
*/
public function save( $contact_form, $args, $context ) {
$postID = $contact_form->id();
$this->get_logger()->debug( 'Saving CF7 Double Opt-In settings', [
'plugin' => 'double-opt-in',
'class' => __CLASS__,
'method' => __METHOD__,
'post_id' => $postID,
] );
// Validate nonce
if ( ! isset( $_POST['f12_cf7_doubleoptin_save_form_nonce'] ) || ! wp_verify_nonce( $_POST['f12_cf7_doubleoptin_save_form_nonce'], 'f12_cf7_doubleoptin_save_form_action' ) ) {
$this->get_logger()->warning( 'Nonce validation failed during save', [
'plugin' => 'double-opt-in',
'post_id' => $postID,
] );
return;
}
// Check if the double opt-in settings is set
if ( ! $postID || ! isset( $_POST['doubleoptin'] ) ) {
update_post_meta( $postID, 'f12-cf7-doubleoptin', [] );
$this->get_logger()->debug( 'No doubleoptin data found, cleared settings', [
'plugin' => 'double-opt-in',
'post_id' => $postID,
] );
return;
}
$parameter = SanitizeHelper::sanitize_array( $_POST['doubleoptin'] );
$metadata = CF7DoubleOptIn::getInstance()->getParameter( $postID );
foreach ( $metadata as $key => $value ) {
if ( isset( $parameter[ $key ] ) ) {
if ( $key === 'body' ) {
$metadata[ $key ] = $parameter[ $key ];
} elseif ( $key === 'enable' ) {
$metadata[ $key ] = (int) $parameter[ $key ];
} elseif ( $key === 'sender' ) {
$metadata[ $key ] = $parameter[ $key ];
} else {
$metadata[ $key ] = $parameter[ $key ];
}
} elseif ( $key === 'enable' ) {
$metadata[ $key ] = 0;
}
}
$metadata = apply_filters( 'f12_cf7_doubleoptin_metadata_cf7', $metadata );
$metadata = apply_filters( 'f12_cf7_doubleoptin_save_form', $metadata );
update_post_meta( $postID, 'f12-cf7-doubleoptin', $metadata );
$this->get_logger()->info( 'Double Opt-In settings saved', [
'plugin' => 'double-opt-in',
'post_id' => $postID,
'data' => $metadata,
] );
}
/**
* Return an option list containing all tags for the condition and a default field to disable the condition.
*
* @param \WPCF7_ContactForm $post
*
* @return array
*/
private function getTags( $post ) {
$this->get_logger()->debug( 'Fetching CF7 form tags', [
'plugin' => 'double-opt-in',
'class' => __CLASS__,
'method' => __METHOD__,
'post_id' => $post->id() ?? null,
] );
$tags = [];
$arrayTags = $post->scan_form_tags();
foreach ( $arrayTags as $formTag /** @var \WPCF7_FormTag $formTag */ ) {
if ( ! empty( $formTag->name ) ) {
$tags[ $formTag->name ] = $formTag->name;
}
}
$this->get_logger()->debug( 'Form tags fetched', [
'plugin' => 'double-opt-in',
'count' => count( $tags ),
] );
return $tags;
}
/**
* Show the backend double opt in options
*
* @param \WPCF7_ContactForm $post
*/
public function render( $post ) {
if ( ! $post ) {
return;
}
$metadata = CF7DoubleOptIn::getInstance()->getParameter( $post->id() );
$id = "doubleoptin";
?>
- 1,
'orderBy' => 'name',
'order' => 'ASC'
);
$list = Category::get_list( $atts, $numberOfPages );
$response = '';
foreach ( $list as $Category /** @var Category $Category */ ) {
$selected = "";
if ( $Category->get_id() == $metadata['category'] ) {
$selected = 'selected="selected"';
}
$response .= '';
}
?>
__( 'Disabled', 'double-opt-in' ) ), $this->getTags( $post ) ), array(
'id' => array( $id . '-conditions' ),
'class' => array(
'large-text',
'code'
)
) );
$ConditionList->setOptionSelectedByKey( $metadata['conditions'] );
echo wp_kses( $ConditionList->get(), array(
'select' => array( 'name' => array() ),
'option' => array(
'value' => array(),
'selected' => array()
)
) );
?>