# booktics/1.0.28/core/module/mailpoet/Controller/mailpoet-controller.php

Booktics – Appointment Booking Calendar for Service Businesses, version 1.0.28. 182 lines.

- Page: https://pluginprobe.com/plugins/booktics/1.0.28/code/core/module/mailpoet/Controller/mailpoet-controller.php
- Raw: https://pluginprobe.com/plugins/booktics/1.0.28/raw/core/module/mailpoet/Controller/mailpoet-controller.php
- Modified: 2026-07-29T07:25:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/booktics/1.0.28/code/core/module/mailpoet/Controller/mailpoet-controller.php#L10-L20`.

```php
<?php
/**
 * MailPoet Controller
 *
 * Exposes the MailPoet plugin lifecycle (install/activate) and list lookup
 * to the admin integration card.
 *
 * @package Booktics/Module/Mailpoet
 */

namespace Booktics\Module\Mailpoet\Controller;

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

use Booktics\Abstracts\Base_Rest_Controller;
use Arraytics\ToolsSdk\PluginManager;

class Mailpoet_Controller extends Base_Rest_Controller {
	/**
	 * The namespace of this controller's route.
	 *
	 * @var string
	 */
	protected $namespace = 'booktics/v1';

	/**
	 * The base of this controller's route.
	 *
	 * @var string
	 */
	protected $rest_base = 'mailpoet';

	/**
	 * MailPoet plugin slug (wp.org).
	 *
	 * @var string
	 */
	const PLUGIN_SLUG = 'mailpoet';

	/**
	 * Register the routes.
	 *
	 * @return void
	 */
	public function register_routes(): void {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			array(
				array(
					'methods'             => \WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_status' ),
					'permission_callback' => array( $this, 'permissions_check' ),
				),
				array(
					'methods'             => \WP_REST_Server::CREATABLE,
					'callback'            => array( $this, 'update_plugin_status' ),
					'permission_callback' => array( $this, 'permissions_check' ),
				),
			)
		);
	}

	/**
	 * Permission check.
	 *
	 * @return bool
	 */
	public function permissions_check() {
		return current_user_can( 'manage_options' );
	}

	/**
	 * Get current MailPoet plugin status and (when active) its lists.
	 *
	 * @return \WP_HTTP_Response
	 */
	public function get_status() {
		return $this->response( $this->build_status_payload() );
	}

	/**
	 * Install or activate the MailPoet plugin.
	 *
	 * @param \WP_REST_Request $request Request.
	 * @return \WP_HTTP_Response
	 */
	public function update_plugin_status( $request ) {
		$params = json_decode( $request->get_body(), true );
		$status = isset( $params['status'] ) ? sanitize_text_field( $params['status'] ) : '';

		if ( ! in_array( $status, array( 'install', 'activate' ), true ) ) {
			return $this->error( __( 'Invalid status provided', 'booktics' ), 422, 'invalid_status' );
		}

		if ( 'install' === $status ) {
			$result = PluginManager::install_plugin( self::PLUGIN_SLUG );
			if ( $result ) {
				PluginManager::activate_plugin( self::PLUGIN_SLUG );
			}
		} else {
			$result = PluginManager::activate_plugin( self::PLUGIN_SLUG );
		}

		if ( false === $result || is_wp_error( $result ) ) {
			return $this->error(
				/* translators: %s: action */
				sprintf( __( 'Could not %s MailPoet', 'booktics' ), $status ),
				500,
				'operation_failed'
			);
		}

		return $this->response(
			$this->build_status_payload(),
			/* translators: %s: action */
			sprintf( __( 'MailPoet %s successfully', 'booktics' ), $status )
		);
	}

	/**
	 * Build the status payload (plugin_status + lists when active).
	 *
	 * @return array
	 */
	private function build_status_payload() {
		$plugin_status = $this->resolve_plugin_status();

		return array(
			'plugin_status' => $plugin_status,
			'lists'         => 'active' === $plugin_status ? $this->get_lists() : array(),
		);
	}

	/**
	 * Resolve the plugin status: install | activate | active.
	 *
	 * @return string
	 */
	private function resolve_plugin_status() {
		if ( ! PluginManager::is_installed( self::PLUGIN_SLUG ) ) {
			return 'install';
		}

		if ( ! PluginManager::is_activated( self::PLUGIN_SLUG ) ) {
			return 'activate';
		}

		return 'active';
	}

	/**
	 * Get MailPoet lists as {id, name} pairs.
	 *
	 * @return array
	 */
	private function get_lists() {
		if ( ! class_exists( '\MailPoet\API\API' ) ) {
			return array();
		}

		try {
			$lists = \MailPoet\API\API::MP( 'v1' )->getLists();
		} catch ( \Throwable $e ) {
			return array();
		}

		$result = array();
		foreach ( (array) $lists as $list ) {
			if ( empty( $list['id'] ) ) {
				continue;
			}
			$result[] = array(
				'id'   => (string) $list['id'],
				'name' => isset( $list['name'] ) ? $list['name'] : '',
			);
		}

		return $result;
	}
}

```
