${__('Text Field customization', 'planet4-engagingnetworks-backend')}
-
-
${__('The next two fields handle the validation of the text input. If both regex and function name are entered, only the regex will be used.', 'planet4-engagingnetworks-backend')}
-
-
-
-
Use the regex without leading and trailing / character
';
- }
-}
diff --git a/src/Controllers/EnformQuestionsListTable.php b/src/Controllers/EnformQuestionsListTable.php
deleted file mode 100644
index 8a6cc94ff2..0000000000
--- a/src/Controllers/EnformQuestionsListTable.php
+++ /dev/null
@@ -1,171 +0,0 @@
- false,
- ]
- );
- $this->error = '';
- $this->type = $type;
- }
-
- /**
- * Implements parent's abstract function.
- * Prepares the list of items for displaying.
- *
- * @see \WP_List_Table::prepare_items
- */
- public function prepare_items(): void
- {
- $supporter_questions = [];
- $main_settings = get_option('p4en_main_settings');
-
- if (isset($main_settings['p4en_private_api'])) {
- $ens_private_token = $main_settings['p4en_private_api'];
- $ens_api = new Ensapi($ens_private_token);
- $supporter_questions = $ens_api->get_supporter_questions();
-
- if (is_array($supporter_questions)) {
- $supporter_questions = array_filter($supporter_questions, [$this, 'check_type']);
- } else {
- $this->error = $supporter_questions . ' : ' .
- __('Could not fetch results from engaging networks', 'planet4-engagingnetworks-backend');
- }
- }
-
- $columns = $this->get_columns();
-
- $hidden = [];
- $sortable = [];
- $this->_column_headers = [$columns, $hidden, $sortable];
- $this->items = is_array($supporter_questions) ? $supporter_questions : [];
- }
-
- /**
- * Used to filter items array based on question type.
- *
- * @param array $item Item in array.
- */
- private function check_type(array $item): bool
- {
- return $this->type === $item['type'];
- }
-
- /**
- * Implements parent's abstract function.
- * Get a list of columns. The format is:
- * 'internal-name' => 'Title'
- *
- * @return array Columns array.
- */
- public function get_columns(): array
- {
- $columns = [
- 'id' => __('Id', 'planet4-engagingnetworks-backend'),
- 'name' => __('Name', 'planet4-engagingnetworks-backend'),
- 'type' => __('Type', 'planet4-engagingnetworks-backend'),
- 'actions' => __('Actions', 'planet4-engagingnetworks-backend'),
- ];
-
- return $columns;
- }
-
- /**
- * Generates content for a column that does not have each own function defined.
- *
- * @param object $item array Column data.
- * @param string $column_name Column Name.
- *
- * @return string Content for column.
- */
- // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint -- compatible with parent class
- protected function column_default($item, $column_name): string
- {
- switch ($column_name) {
- case 'id':
- case 'name':
- case 'type':
- return $item[$column_name];
- }
- return '';
- }
-
- /**
- * Generates content for the actions column.
- *
- * @param array $item Column data.
- *
- * @return string Content for actions column.
- */
- public function column_actions(array $item): string
- {
- $data_attributes = [
- 'id' => $item['id'],
- 'name' => $item['name'],
- 'type' => $item['type'],
- ];
-
- $attributes_string = '';
- foreach ($data_attributes as $attr => $value) {
- $attributes_string .= " data-$attr=\"" . esc_attr($value) . '"';
- }
-
- return '';
- }
-
- /**
- * Overrides parent function to disable nonce generation, bulk actions and pagination.
- * Used to display errors (if any) that come from en api.
- *
- * @param string $which Navigation position.
- *
- * @see \WP_List_Table::display_tablenav
- */
- // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint -- compatible with parent class
- protected function display_tablenav($which): void
- {
- if (empty($this->error) || 'top' !== $which) {
- return;
- }
- echo '
' . esc_html($this->error) . '
';
- }
-}
diff --git a/src/Controllers/EnsapiController.php b/src/Controllers/EnsapiController.php
deleted file mode 100644
index 6b610768d2..0000000000
--- a/src/Controllers/EnsapiController.php
+++ /dev/null
@@ -1,424 +0,0 @@
-authenticate($ens_private_token, $token_type);
- }
-
- /**
- * Returns the auth token. If communication is not authenticated then the auth token is an empty string.
- *
- * @return mixed The auth token.
- */
- public function is_authenticated(): mixed
- {
- return $this->ens_auth_token;
- }
-
- /**
- * Authenticates usage of ENS API calls.
- *
- * @param string $ens_private_token The private api token to be used in order to authenticate for ENS API.
- * @param string $token_name Defines the token name.
- */
- private function authenticate(string $ens_private_token, string $token_name): void
- {
- // Get cached auth token.
- $ens_auth_token = get_transient($token_name);
-
- if (!$ens_auth_token) {
- $url = self::ENS_AUTH_URL;
- // With the safe version of wp_remote_{VERB) functions, the URL is validated to avoid redirection
- // and request forgery attacks.
- $response = wp_safe_remote_post(
- $url,
- [
- 'headers' => [
- 'Content-Type' => 'application/json; charset=UTF-8',
- ],
- 'body' => $ens_private_token,
- 'timeout' => self::ENS_CALL_TIMEOUT,
- ]
- );
-
- // Communication with ENS API is authenticated.
- if (is_array($response) && \WP_Http::OK === $response['response']['code'] && $response['body']) {
- $body = json_decode($response['body'], true);
- // Time period in seconds to keep the ens_auth_token before refreshing. Typically 1 hour.
- $expiration = (int) ($body['expires'] / 1000);
- $ens_auth_token = $body['ens-auth-token'];
- set_transient($token_name, $ens_auth_token, $expiration);
- }
- }
- $this->$token_name = $ens_auth_token;
- }
-
- /**
- * Retrieves all EN pages whose type is included in the $types array.
- *
- * @param array $types Array with the types of the EN pages to be retrieved.
- * @param string $status The status of the EN pages to be retrieved.
- *
- * @return array Array with data of the retrieved EN pages.
- */
- public function get_pages_by_types_status(array $types, string $status = 'all'): array
- {
- $pages = [];
- if ($types) {
- $params['status'] = $status;
- foreach ($types as $type) {
- $params['type'] = $type;
- $response = $this->get_pages($params);
- if (!is_array($response)) {
- continue;
- }
- $pages[ $params['type'] ] = $response;
- }
- }
-
- return $pages;
- }
-
- /**
- * Gets all the information on the available pages built in EN.
- *
- * @param array $params The query parameters to be added in the url.
- *
- * @return array|string An associative array with the response
- * (under key 'body') or a string with an error message in case of a failure.
- */
- public function get_pages(array $params = [
- 'type' => self::ENS_TYPES_DEFAULT,
- 'status' => self::ENS_STATUS_DEFAULT,
- ]): array|string
- {
- $from_cache = get_transient('ens_pages_response_' . implode('_', $params));
- if ($from_cache) {
- return json_decode($from_cache, true);
- }
-
- $url = add_query_arg(
- [
- 'type' => strtolower($params['type']),
- 'status' => $params['status'],
- ],
- self::ENS_PAGES_URL
- );
-
- // With the safe version of wp_remote_{VERB) functions, the URL is validated to avoid redirection
- // and request forgery attacks.
- $response = wp_safe_remote_get(
- $url,
- [
- 'headers' => [
- 'ens-auth-token' => $this->ens_auth_token,
- ],
- 'timeout' => self::ENS_CALL_TIMEOUT,
- ]
- );
-
- if (is_wp_error($response)) {
- return $response->get_error_message() . ' ' . $response->get_error_code();
- }
-
- if (is_array($response) && \WP_Http::OK !== $response['response']['code']) {
- // Authentication failed.
- return $response['response']['message'] . ' ' . $response['response']['code'];
- }
- set_transient('ens_pages_response_' . implode('_', $params), $response['body'], self::ENS_CACHE_TTL);
-
- return json_decode($response['body'], true);
- }
-
- /**
- * Process an EN Page.
- *
- * @param int $page_id The id of the EN page that the submitted data will be sent to.
- * @param array $fields The submitted fields which will be passed to the body of the API call.
- *
- * @return array|string An associative array with the response (under key 'body')
- * or a string with an error message in case of a failure.
- */
- public function process_page(int $page_id, array $fields): array|string
- {
- $url = self::ENS_PAGES_URL . '/' . $page_id . '/process';
-
- // If Email address is found then supporter exists and its data will be updated with the values
- // inside the supporter key. Else a new supporter with this Email address will be created by EN.
- $supporter_keys_fields = [
- 'Title' => 'supporter.title',
- 'First name' => 'supporter.firstName',
- 'Last name' => 'supporter.lastName',
- 'Address 1' => 'supporter.address1',
- 'Address 2' => 'supporter.address2',
- 'City' => 'supporter.city',
- 'Country' => 'supporter.country',
- 'Position' => 'supporter.position',
- 'Postcode' => 'supporter.postcode',
- 'Email' => 'supporter.emailAddress',
- 'Phone Number' => 'supporter.phoneNumber',
- 'Date of Birth' => 'supporter.birthday',
- 'questions' => 'supporter.questions',
- ];
-
- // Supporter fields are updated only if they exist as fields within the submitted form.
- foreach ($supporter_keys_fields as $api_key => $field_name) {
- if (!isset($fields[$field_name])) {
- continue;
- }
- $supporter[$api_key] = $fields[$field_name];
- }
-
- $body = [
- 'supporter' => $supporter ?? [],
- ];
-
- // With the safe version of wp_remote_{VERB) functions, the URL is validated to avoid redirection
- // and request forgery attacks.
- $response = wp_safe_remote_post(
- $url,
- [
- 'headers' => [
- 'ens-auth-token' => $this->ens_auth_token,
- 'Content-Type' => 'application/json; charset=UTF-8',
- ],
- 'body' => wp_json_encode($body),
- 'timeout' => self::ENS_CALL_TIMEOUT,
- ]
- );
-
- // Authentication failure.
- if (is_wp_error($response)) {
- return $response->get_error_message() . ' ' . $response->get_error_code();
- }
-
- if (is_array($response) && \WP_Http::OK !== $response['response']['code']) {
- return $response['response']['message'] . ' ' . $response['response']['code'];
- }
- return $response;
- }
-
- /**
- * Gets all the supporter fields that exist in the EN client account.
- *
- * @return array|string Array with the fields or a message if something goes wrong.
- */
- public function get_supporter_fields(): array|string
- {
- $from_cache = get_transient('ens_supporter_fields_response');
- if ($from_cache) {
- return json_decode($from_cache, true);
- }
-
- $url = self::ENS_SUPPORTER_URL . '/fields';
-
- // With the safe version of wp_remote_{VERB) functions, the URL is validated to avoid redirection
- // and request forgery attacks.
- $response = wp_safe_remote_get(
- $url,
- [
- 'headers' => [
- 'ens-auth-token' => $this->ens_auth_token,
- 'Content-Type' => 'application/json; charset=UTF-8',
- ],
- 'timeout' => self::ENS_CALL_TIMEOUT,
- ]
- );
-
- // Authentication failure.
- if (is_wp_error($response)) {
- return $response->get_error_message() . ' ' . $response->get_error_code();
- }
-
- if (is_array($response) && \WP_Http::OK !== $response['response']['code']) {
- return $response['response']['message'] . ' ' . $response['response']['code'];
- }
- set_transient('ens_supporter_fields_response', (string) $response['body'], self::ENS_CACHE_TTL);
-
- return json_decode($response['body'], true);
- }
-
- /**
- * Gets all the supporter questions/optins that exist in the EN client account.
- *
- * @return array|string Array with the fields or a message if something goes wrong.
- */
- public function get_supporter_questions(): array|string
- {
- $response['body'] = get_transient('ens_supporter_questions_response');
- if ($response['body']) {
- return json_decode($response['body'], true);
- }
-
- $url = self::ENS_SUPPORTER_URL . '/questions';
-
- // With the safe version of wp_remote_{VERB) functions, the URL is validated to avoid redirection
- // and request forgery attacks.
- $response = wp_safe_remote_get(
- $url,
- [
- 'headers' => [
- 'ens-auth-token' => $this->ens_auth_token,
- 'Content-Type' => 'application/json; charset=UTF-8',
- ],
- 'timeout' => self::ENS_CALL_TIMEOUT,
- ]
- );
-
- // Authentication failure.
- if (is_wp_error($response)) {
- return $response->get_error_message() . ' ' . $response->get_error_code();
- }
-
- if (is_array($response) && \WP_Http::OK !== $response['response']['code']) {
- return $response['response']['message'] . ' ' . $response['response']['code'];
- }
- set_transient('ens_supporter_questions_response', (string) $response['body'], self::ENS_CACHE_TTL);
-
- return json_decode($response['body'], true);
- }
-
- /**
- * Gets specific questions/optin that exists in the EN client account.
- *
- * @param int $question_id The id of the question/optin.
- *
- * @return array|string Array with the fields or a message if something goes wrong.
- */
- public function get_supporter_question_by_id(int $question_id): array|string
- {
- $from_cache = get_transient('ens_supporter_question_by_id_response_' . $question_id);
- if ($from_cache) {
- return json_decode($from_cache, true);
- }
-
- $url = self::ENS_SUPPORTER_URL . '/questions/' . $question_id;
-
- // With the safe version of wp_remote_{VERB) functions, the URL is validated to avoid redirection
- // and request forgery attacks.
- $response = wp_safe_remote_get(
- $url,
- [
- 'headers' => [
- 'ens-auth-token' => $this->ens_auth_token,
- 'Content-Type' => 'application/json; charset=UTF-8',
- ],
- 'timeout' => self::ENS_CALL_TIMEOUT,
- ]
- );
-
- // Authentication failure.
- if (is_wp_error($response)) {
- return $response->get_error_message() . ' ' . $response->get_error_code();
- }
-
- if (is_array($response) && \WP_Http::OK !== $response['response']['code']) {
- return $response['response']['message'] . ' ' . $response['response']['code'];
- }
- set_transient(
- 'ens_supporter_question_by_id_response_' . $question_id,
- (string) $response['body'],
- self::ENS_CACHE_TTL,
- );
-
- return json_decode($response['body'], true);
- }
-
- /**
- * Authenticates usage of ENS API calls.
- *
- * @param string $email The supporter's email address.
- * @param bool $include_questions True if we want to include the supporters data for questions/optins.
- *
- * @return array|string An associative array with the response (under key 'body')
- * or a string with an error message in case of a failure.
- */
- public function get_supporter_by_email(string $email, bool $include_questions = true): array|string
- {
- $url = add_query_arg(
- [
- 'email' => $email,
- 'includeQuestions' => $include_questions ? 'true' : 'false',
- ],
- self::ENS_SUPPORTER_URL
- );
-
- // With the safe version of wp_remote_{VERB) functions, the URL is validated to avoid redirection
- // and request forgery attacks.
- $response = wp_safe_remote_get(
- $url,
- [
- 'headers' => [
- 'ens-auth-token' => $this->ens_auth_token,
- 'Content-Type' => 'application/json; charset=UTF-8',
- ],
- 'timeout' => self::ENS_CALL_TIMEOUT,
- ]
- );
-
- // Authentication failure.
- if (is_wp_error($response)) {
- return $response->get_error_message() . ' ' . $response->get_error_code();
- }
-
- if (is_array($response) && \WP_Http::OK !== $response['response']['code']) {
- return $response['response']['message'] . ' ' . $response['response']['code'];
- }
- return $response;
- }
-
- /**
- * Get session token for public user.
- *
- * @return mixed EN Service Token.
- */
- public function get_public_session_token(): mixed
- {
- if (!$this->ens_auth_public_token) {
- $main_settings = get_option('p4en_main_settings');
- $ens_private_token = $main_settings['p4en_frontend_private_api'];
- $this->authenticate($ens_private_token, 'ens_auth_public_token');
- }
-
- return $this->ens_auth_public_token;
- }
-}
diff --git a/src/Controllers/Menu/EnSettingsController.php b/src/Controllers/Menu/EnSettingsController.php
deleted file mode 100644
index ada1841c47..0000000000
--- a/src/Controllers/Menu/EnSettingsController.php
+++ /dev/null
@@ -1,166 +0,0 @@
-already_ran(M001EnableEnFormFeature::get_id());
- $feature_is_active = ! $migration_ran || Features::is_active('feature_engaging_networks');
-
- if ($feature_is_active && current_user_can('manage_options')) {
- add_menu_page(
- 'Engaging Networks',
- 'Engaging Networks',
- 'edit_pages',
- P4_MASTER_THEME_EN_SLUG_NAME,
- '',
- get_template_directory_uri() . '/images/en.png'
- );
-
- add_submenu_page(
- P4_MASTER_THEME_EN_SLUG_NAME,
- __('Settings', 'planet4-engagingnetworks-backend'),
- __('Settings', 'planet4-engagingnetworks-backend'),
- 'manage_options',
- 'en-settings',
- [$this, 'prepare_settings']
- );
- }
- add_action('admin_init', [$this, 'register_settings']);
- }
-
- /**
- * Render the settings page of the plugin.
- */
- public function prepare_settings(): void
- {
- $this->view->settings(
- [
- 'settings' => get_option('p4en_main_settings'),
- 'available_languages' => P4_MASTER_THEME_LANGUAGES,
- 'domain' => 'planet4-engagingnetworks-backend',
- ]
- );
- }
-
- /**
- * Register and store the settings and their data.
- */
- public function register_settings(): void
- {
- $args = [
- 'type' => 'string',
- 'group' => 'p4en_main_settings_group',
- 'description' => 'Planet 4 - EngagingNetworks settings',
- 'sanitize_callback' => [$this, 'valitize'],
- 'show_in_rest' => false,
- ];
- register_setting('p4en_main_settings_group', 'p4en_main_settings', $args);
- }
-
- /**
- * Validates and sanitizes the settings input.
- *
- * @param array $settings The associative array with the settings that are registered for the plugin.
- *
- * @return mixed Array if validation is ok, false if validation fails.
- */
- public function valitize(array $settings): mixed
- {
- if ($this->validate($settings)) {
- $this->sanitize($settings);
- }
- return $settings;
- }
-
- /**
- * Validates the settings input.
- *
- * @param array $settings The associative array with the settings that are registered for the plugin.
- */
- public function validate(array $settings): bool
- {
- if (! $settings) {
- return true;
- }
-
- $has_errors = false;
-
- if (isset($settings['p4en_public_api']) && 36 !== strlen($settings['p4en_public_api'])) {
- add_settings_error(
- 'p4en_main_settings-p4en_public_api',
- esc_attr('p4en_main_settings-p4en_public_api'),
- __('Invalid value for Public API', 'planet4-engagingnetworks-backend'),
- 'error'
- );
- $has_errors = true;
- }
- if (isset($settings['p4en_private_api']) && 36 !== strlen($settings['p4en_private_api'])) {
- add_settings_error(
- 'p4en_main_settings-p4en_private_api',
- esc_attr('p4en_main_settings-p4en_private_api'),
- __('Invalid value for Private API', 'planet4-engagingnetworks-backend'),
- 'error'
- );
- $has_errors = true;
- }
- if (isset($settings['p4en_frontend_public_api']) && 36 !== strlen($settings['p4en_frontend_public_api'])) {
- add_settings_error(
- 'p4en_main_settings-p4en_frontend_public_api',
- esc_attr('p4en_main_settings-p4en_frontend_public_api'),
- __('Invalid value for Frontend Public API', 'planet4-engagingnetworks-backend'),
- 'error'
- );
- $has_errors = true;
- }
- if (isset($settings['p4en_frontend_private_api']) && 36 !== strlen($settings['p4en_frontend_private_api'])) {
- add_settings_error(
- 'p4en_main_settings-p4en_frontend_private_api',
- esc_attr('p4en_main_settings-p4en_frontend_private_api'),
- __('Invalid value for Frontend Private API', 'planet4-engagingnetworks-backend'),
- 'error'
- );
- $has_errors = true;
- }
-
- return ! $has_errors;
- }
-
- /**
- * Sanitizes the settings input.
- *
- * @param array $settings The associative array with the settings that are registered for the plugin.
- */
- public function sanitize(array &$settings): void
- {
- if (! $settings) {
- return;
- }
-
- foreach ($settings as $name => $setting) {
- $settings[$name] = sanitize_text_field($setting);
- }
- }
-}
diff --git a/src/Controllers/Menu/EnformPostController.php b/src/Controllers/Menu/EnformPostController.php
deleted file mode 100644
index 477887d51f..0000000000
--- a/src/Controllers/Menu/EnformPostController.php
+++ /dev/null
@@ -1,480 +0,0 @@
-hooks();
- }
-
- /**
- * Class hooks.
- */
- private function hooks(): void
- {
- add_action('init', [$this, 'register_post_type']);
- add_shortcode(self::POST_TYPE, [$this, 'handle_form_shortcode']);
- add_filter('post_row_actions', [$this, 'modify_post_row_actions'], 10, 2);
-
- add_action('add_meta_boxes', [$this, 'add_form_meta_box'], 10, 2);
- add_action('add_meta_boxes', [$this, 'add_selected_meta_box'], 11, 2);
- add_action('add_meta_boxes', [$this, 'add_fields_meta_box'], 12, 2);
- add_action('add_meta_boxes', [$this, 'add_questions_custom_box']);
- add_action('add_meta_boxes', [$this, 'add_optins_custom_box']);
- add_action('save_post_' . self::POST_TYPE, [$this, 'save_fields_meta_box'], 10, 2);
-
- add_action('wp_ajax_get_supporter_question_by_id', [$this, 'get_supporter_question_by_id']);
- add_action('wp_ajax_nopriv_get_supporter_question_by_id', [$this, 'get_supporter_question_by_id']);
- }
-
- /**
- * Create menu/submenu entry.
- */
- public function create_admin_menu(): void
- {
- $current_user = wp_get_current_user();
-
- if (
- ! in_array('administrator', $current_user->roles, true) &&
- ! in_array('editor', $current_user->roles, true)
- ) {
- return;
- }
- add_submenu_page(
- P4_MASTER_THEME_EN_SLUG_NAME,
- __('All EN Forms', 'planet4-engagingnetworks-backend'),
- __('All EN Forms', 'planet4-engagingnetworks-backend'),
- 'edit_posts',
- 'edit.php?post_type=' . self::POST_TYPE
- );
-
- add_submenu_page(
- P4_MASTER_THEME_EN_SLUG_NAME,
- __('Add New', 'planet4-engagingnetworks-backend'),
- __('Add New', 'planet4-engagingnetworks-backend'),
- 'edit_posts',
- 'post-new.php?post_type=' . self::POST_TYPE
- );
-
- // Set hook after screen is determined to load assets for add/edit page.
- add_action('current_screen', [$this, 'load_assets']);
- }
-
- /**
- * Register en forms custom post type.
- */
- public function register_post_type(): void
- {
- $labels = [
- 'name' => _x('Engaging Network Forms', 'en forms', 'planet4-engagingnetworks-backend'),
- 'singular_name' => _x('Engaging Network Form', 'en form', 'planet4-engagingnetworks-backend'),
- 'menu_name' => _x('En Forms Menu', 'admin menu', 'planet4-engagingnetworks-backend'),
- 'name_admin_bar' => _x('En Form', 'add new on admin bar', 'planet4-engagingnetworks-backend'),
- 'add_new' => _x('Add New', 'en form', 'planet4-engagingnetworks-backend'),
- 'add_new_item' => __('Add New EN Form', 'planet4-engagingnetworks-backend'),
- 'new_item' => __('New EN Form', 'planet4-engagingnetworks-backend'),
- 'edit_item' => __('Edit EN Form', 'planet4-engagingnetworks-backend'),
- 'view_item' => __('View EN Form', 'planet4-engagingnetworks-backend'),
- 'all_items' => __('All EN Forms', 'planet4-engagingnetworks-backend'),
- 'search_items' => __('Search EN Forms', 'planet4-engagingnetworks-backend'),
- 'parent_item_colon' => __('Parent EN Forms:', 'planet4-engagingnetworks-backend'),
- 'not_found' => __('No en forms found.', 'planet4-engagingnetworks-backend'),
- 'not_found_in_trash' => __('No en forms found in Trash.', 'planet4-engagingnetworks-backend'),
- ];
-
- register_post_type(
- self::POST_TYPE,
- [
- 'labels' => $labels,
- 'description' => __('EN Forms', 'planet4-engagingnetworks-backend'),
- 'rewrite' => false,
- 'query_var' => false,
- 'public' => false,
- 'publicly_queryable' => false,
- 'capability_type' => 'page',
- 'has_archive' => true,
- 'hierarchical' => false,
- 'menu_position' => null,
- 'exclude_from_search' => true,
- 'map_meta_cap' => true,
- // necessary in order to use WordPress default custom post type list page.
- 'show_ui' => true,
- // hide it from menu, as we are using custom submenu pages.
- 'show_in_menu' => false,
- 'supports' => ['title'],
- 'show_in_rest' => true,
- ]
- );
-
- $custom_meta_args = [
- 'type' => 'string',
- 'single' => true,
- 'show_in_rest' => true,
- ];
- register_meta(self::POST_TYPE, self::FIELDS_META, $custom_meta_args);
-
- \register_rest_field(
- self::POST_TYPE,
- self::FIELDS_META,
- [
- 'get_callback' => function ($obj) {
- return \get_post_meta(
- (int) $obj['id'],
- self::FIELDS_META,
- true
- );
- },
- ]
- );
- }
-
- /**
- * Filter for post_row_actions. Alters edit action link and removes Quick edit action.
- *
- * @param array $actions An array of row action links. Defaults are
- * 'Edit', 'Quick Edit', 'Restore', 'Trash',
- * 'Delete Permanently', 'Preview', and 'View'.
- * @param \WP_Post $post The post object.
- *
- * @return array The filtered actions array.
- */
- public function modify_post_row_actions(array $actions, \WP_Post $post): array
- {
- // Check if post is of p4en_form_post type.
- if (self::POST_TYPE === $post->post_type) {
- /*
- * Hide Quick Edit.
- */
- $custom_actions = [
- 'inline hide-if-no-js' => '',
- ];
-
- $actions = array_merge($actions, $custom_actions);
- }
-
- return $actions;
- }
-
- /**
- * Adds shortcode for this custom post type.
- *
- * @param array $atts Array of attributes for the shortcode.
- */
- public function handle_form_shortcode(array $atts): void
- {
- global $pagenow;
-
- // Define attributes and their defaults.
- $atts = array_merge(
- [
- 'id' => 'id',
- 'en_form_style' => 'full-width',
- ],
- $atts
- );
-
- $post_id = filter_input(INPUT_GET, 'post', FILTER_VALIDATE_INT);
-
- if (
- ! is_admin() &&
- ! ('post.php' === $pagenow && $post_id && self::POST_TYPE === get_post_type($post_id)) &&
- ! ('admin-ajax.php' === $pagenow && self::POST_TYPE === get_post_type($atts['id']))
- ) {
- return;
- }
- $fields = get_post_meta($atts['id'], self::FIELDS_META, true);
-
- $data = [
- 'form_fields' => $fields,
- 'en_form_style' => $atts['en_form_style'],
- ];
-
- $this->view->enform_post($data);
- }
-
- /**
- * Creates a Meta box for the Selected Components of the current EN Form.
- *
- * @param string $post_type The current post type (unused).
- * @param WP_Post|WP_Comment $post The currently Added/Edited EN Form.
- * phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter
- */
- public function add_form_meta_box(string $post_type, $post): void
- {
- add_meta_box(
- 'meta-box-form',
- __('Form preview', 'planet4-engagingnetworks-backend'),
- [$this, 'view_meta_box_form'],
- [self::POST_TYPE],
- 'normal',
- 'high',
- $post
- );
- }
- // phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter
-
- /**
- * View an EN form.
- *
- * @param \WP_Post $post The currently Added/Edited EN Form.
- */
- public function view_meta_box_form(\WP_Post $post): void
- {
- echo do_shortcode('[' . self::POST_TYPE . ' id="' . $post->ID . '" /]');
- }
-
- /**
- * Creates a Meta box for the Selected Components of the current EN Form.
- *
- * @param string $post_type The current post type (unused).
- * @param WP_Post|WP_Comment $post The currently Added/Edited EN Form.
- * phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter
- */
- public function add_selected_meta_box(string $post_type, $post): void
- {
- add_meta_box(
- 'meta-box-selected',
- __('Selected Components', 'planet4-engagingnetworks-backend'),
- [$this, 'view_selected_meta_box'],
- [self::POST_TYPE],
- 'normal',
- 'high',
- $post
- );
- }
- // phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter
-
- /**
- * Prepares data to render the Selected Components meta box.
- *
- * @param \WP_Post $post The currently Added/Edited EN Form.
- */
- public function view_selected_meta_box(\WP_Post $post): void
- {
- $form_fields = get_post_meta($post->ID, self::FIELDS_META, true);
- $this->view->en_selected_meta_box(
- [
- 'fields' => wp_json_encode($form_fields),
- ]
- );
- }
-
- /**
- * Adds available fields custom meta box to p4en_form edit post page.
- *
- * @param string $post_type The current post type (unused).
- * @param WP_Post|WP_Comment $post The currently Added/Edited EN Form.
- * phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter
- */
- public function add_fields_meta_box(string $post_type, $post): void
- {
- add_meta_box(
- 'fields_list_box',
- __('Available Fields', 'planet4-engagingnetworks-backend'),
- [$this, 'display_fields_custom_box'],
- self::POST_TYPE,
- 'normal',
- 'high',
- $post
- );
- }
- // phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter
-
- /**
- * Display fields custom box content.
- */
- public function display_fields_custom_box(): void
- {
- $list_table = new EnformFieldsListTable();
- $list_table->prepare_items();
- $list_table->display();
- }
-
- /**
- * Adds a meta box for the EN questions.
- *
- * Adds available questions custom meta box to p4en_form edit post page.
- */
- public function add_questions_custom_box(): void
- {
- add_meta_box(
- 'questions_list_box',
- __('Available Questions', 'planet4-engagingnetworks-backend'),
- [$this, 'display_questions_custom_box'],
- self::POST_TYPE
- );
- }
-
- /**
- * Display questions custom box content.
- */
- public function display_questions_custom_box(): void
- {
- $list_table = new EnformQuestionsListTable('GEN');
- $list_table->prepare_items();
- $list_table->display();
- }
-
- /**
- * Adds available opt-ins custom meta box to p4en_form edit post page.
- */
- public function add_optins_custom_box(): void
- {
- add_meta_box(
- 'optins_list_box',
- __('Available Opt-ins', 'planet4-engagingnetworks-backend'),
- [$this, 'display_optins_custom_box'],
- self::POST_TYPE
- );
- }
-
- /**
- * Display opt-ins custom box content.
- */
- public function display_optins_custom_box(): void
- {
- $list_table = new EnformQuestionsListTable('OPT');
- $list_table->prepare_items();
- $list_table->display();
- }
-
- /**
- * Retrieves data of a specific question/opt-in.
- */
- public function get_supporter_question_by_id(): void
- {
- // If this is an ajax call.
- if (! wp_doing_ajax()) {
- return;
- }
- $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
- $main_settings = get_option('p4en_main_settings');
- $ens_private_token = $main_settings['p4en_private_api'];
- $ens_api = new Ensapi($ens_private_token);
- $response = $ens_api->get_supporter_question_by_id($id);
-
- wp_send_json($response);
- }
-
- /**
- * Add underscore templates to footer.
- */
- public function print_admin_footer_scripts(): void
- {
- $this->view->view_template('selected_enform_fields', [], 'block_templates/');
- }
-
- /**
- * Hook load new page assets conditionally based on current page.
- */
- public function load_assets(): void
- {
- global $pagenow, $typenow;
- $pages = [
- 'post.php',
- 'post-new.php',
- ];
-
- // Load assets conditionally using pagenow, typenow on new/edit form page.
- if (! in_array($pagenow, $pages, true) || self::POST_TYPE !== $typenow) {
- return;
- }
- add_action("load-$pagenow", [$this, 'load__new_page_assets']);
- add_action('admin_print_footer_scripts', [$this, 'print_admin_footer_scripts'], 1);
- }
-
- /**
- * Load assets for new/edit form page.
- */
- public function load__new_page_assets(): void
- {
- wp_enqueue_script('jquery-ui-core');
- wp_enqueue_script('jquery-ui-sortable');
- wp_enqueue_script('jquery-ui-dialog');
- wp_enqueue_script('jquery-ui-tooltip');
- wp_enqueue_style('wp-jquery-ui-dialog');
- wp_enqueue_style(
- 'p4en_admin_style_blocks',
- get_template_directory_uri() . '/admin/css/admin_en.css',
- [],
- \P4\MasterTheme\Loader::theme_file_ver('admin/css/admin_en.css'),
- );
- \P4\MasterTheme\Loader::enqueue_versioned_script(
- 'admin/js/enforms.js',
- [
- 'jquery',
- 'wp-backbone',
- ]
- );
- }
-
- /**
- * Saves the p4 enform fields of the Post.
- *
- * @param int $post_id The ID of the current Post.
- * @param \WP_Post $post The current Post.
- * phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter
- */
- public function save_fields_meta_box(int $post_id, \WP_Post $post): void
- {
- global $pagenow;
-
- // Ignore autosave.
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
- return;
- }
-
- // Check user's capabilities.
- if (! current_user_can('edit_post', $post_id)) {
- return;
- }
-
- // Check post input.
- $form_fields = filter_input(
- INPUT_POST,
- self::FIELDS_META
- );
-
- // If this is a new post then set form fields meta.
- if (! $form_fields || 'post.php' !== $pagenow) {
- return;
- }
-
- $form_fields = json_decode(($form_fields));
-
- // Store form fields meta.
- update_post_meta($post_id, self::FIELDS_META, $form_fields);
- }
- // phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter
-}
diff --git a/src/Features/EngagingNetworks.php b/src/Features/EngagingNetworks.php
deleted file mode 100644
index 86f56b59e4..0000000000
--- a/src/Features/EngagingNetworks.php
+++ /dev/null
@@ -1,46 +0,0 @@
-load();
- }
}
/**
@@ -204,7 +189,6 @@ public static function add_blocks(): void
new Blocks\Cookies();//NOSONAR
new Blocks\Counter();//NOSONAR
new Blocks\Covers();//NOSONAR
- new Blocks\ENForm();//NOSONAR
new Blocks\Gallery();//NOSONAR
new Blocks\GuestBook();//NOSONAR
new Blocks\HappyPoint();//NOSONAR
diff --git a/src/MasterBlocks.php b/src/MasterBlocks.php
index cbe915911d..bfb5f9b576 100644
--- a/src/MasterBlocks.php
+++ b/src/MasterBlocks.php
@@ -2,8 +2,6 @@
namespace P4\MasterTheme;
-use P4\MasterTheme\Controllers\EnsapiController;
-use P4\MasterTheme\Blocks\ENForm;
use P4\MasterTheme\Features\Dev\BetaBlocks;
use Twig_SimpleFilter;
@@ -203,52 +201,6 @@ private function get_p4_features(): array
return get_option('planet4_features');
}
- /**
- * Get all available EN pages.
- */
- public function get_en_pages(): array
- {
- $main_settings = get_option('p4en_main_settings');
-
- // Get EN pages only on admin panel.
- if (!is_admin() || !isset($main_settings['p4en_private_api'])) {
- return [];
- }
-
- $pages = [];
- $pages[] = $main_settings['p4en_private_api'];
- $ens_private_token = $main_settings['p4en_private_api'];
- $ens_api = new EnsapiController($ens_private_token);
- $pages = $ens_api->get_pages_by_types_status(ENForm::ENFORM_PAGE_TYPES, 'live');
- uasort(
- $pages,
- function ($a, $b) {
- return ($a['name'] ?? '') <=> ($b['name'] ?? '');
- }
- );
-
- return $pages;
- }
-
- /**
- * Get all available EN forms.
- */
- public function get_en_forms(): array
- {
- // Get EN Forms.
- $query = new \WP_Query(
- [
- 'post_status' => 'publish',
- 'post_type' => 'p4en_form',
- 'orderby' => 'post_title',
- 'order' => 'asc',
- 'suppress_filters' => false,
- 'posts_per_page' => -1,
- ]
- );
- return $query->posts;
- }
-
/**
* Add variables reflected from PHP to JS.
*/
@@ -260,8 +212,6 @@ public function reflect_js_variables(): array
'news_page_link' => $news_page ? get_permalink($news_page) : null,
'options' => $this->get_p4_options(),
'features' => $this->get_p4_features(),
- 'pages' => $this->get_en_pages(),
- 'forms' => $this->get_en_forms(),
'themeUrl' => get_template_directory_uri(),
];
}
diff --git a/src/MasterSite.php b/src/MasterSite.php
index 37b0cdb73d..37b991320a 100644
--- a/src/MasterSite.php
+++ b/src/MasterSite.php
@@ -604,14 +604,14 @@ public function p4_master_theme_setup(): void
$domains = [
'planet4-master-theme',
'planet4-master-theme-backend',
- 'planet4-engagingnetworks',
- 'planet4-engagingnetworks-backend',
+ 'blocks/planet4-blocks',
+ 'blocks/planet4-blocks-backend',
];
$locale = is_admin() ? get_user_locale() : get_locale();
foreach ($domains as $domain) {
$mofile = get_template_directory() . '/languages/' . $domain . '-' . $locale . '.mo';
- load_textdomain($domain, $mofile);
+ load_textdomain(str_replace('blocks/', '', $domain), $mofile);
}
}
diff --git a/src/Migrations/M001EnableEnFormFeature.php b/src/Migrations/M001EnableEnFormFeature.php
index 6e832402da..5670577c1c 100644
--- a/src/Migrations/M001EnableEnFormFeature.php
+++ b/src/Migrations/M001EnableEnFormFeature.php
@@ -2,7 +2,6 @@
namespace P4\MasterTheme\Migrations;
-use P4\MasterTheme\Features;
use P4\MasterTheme\MigrationRecord;
use P4\MasterTheme\MigrationScript;
@@ -19,7 +18,9 @@ class M001EnableEnFormFeature extends MigrationScript
*/
protected static function execute(MigrationRecord $record): void
{
- Features\EngagingNetworks::enable();
+ $features = get_option('planet4_features');
+ $features['feature_engaging_networks'] = true;
+ update_option('planet4_features', $features);
}
// phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter
}
diff --git a/src/Migrations/M036RemoveEnFormOptions.php b/src/Migrations/M036RemoveEnFormOptions.php
new file mode 100644
index 0000000000..8f3f432de9
--- /dev/null
+++ b/src/Migrations/M036RemoveEnFormOptions.php
@@ -0,0 +1,38 @@
+ 'p4en_form',
+ ]);
+ foreach ($posts as $post) {
+ echo 'Parsing post ', $post->ID, ': ', $post->post_title, '\n';
+ wp_delete_post($post->ID, true);
+ }
+ }
+}
diff --git a/src/Migrator.php b/src/Migrator.php
index 688a8360f6..46349bc11b 100644
--- a/src/Migrator.php
+++ b/src/Migrator.php
@@ -37,6 +37,7 @@
use P4\MasterTheme\Migrations\M033MigrateSocialMediaTwitterBlockToEmbedBlock;
use P4\MasterTheme\Migrations\M034PrePopulateOldPostsArchiveNotice;
use P4\MasterTheme\Migrations\M035MigrateCampaignCoversToP4ColumnsBlock;
+use P4\MasterTheme\Migrations\M036RemoveEnFormOptions;
/**
* Run any new migration scripts and record results in the log.
@@ -91,6 +92,7 @@ public static function migrate(): void
M033MigrateSocialMediaTwitterBlockToEmbedBlock::class,
M034PrePopulateOldPostsArchiveNotice::class,
M035MigrateCampaignCoversToP4ColumnsBlock::class,
+ M036RemoveEnFormOptions::class,
];
// Loop migrations and run those that haven't run yet.
diff --git a/src/Model/QuestionsModel.php b/src/Model/QuestionsModel.php
deleted file mode 100644
index 9b7da3ecb7..0000000000
--- a/src/Model/QuestionsModel.php
+++ /dev/null
@@ -1,125 +0,0 @@
-questions_option);
-
- if (isset($options['questions']) && ! empty($options['questions'])) {
- $questions = $options['questions'];
- foreach ($questions as $question) {
- if ((int) $question['id'] === (int) $id) {
- return $question;
- }
- }
- }
-
- return [];
- }
-
- /**
- * Retrieve all the questions.
- */
- public function get_questions(): array
- {
- $options = get_option($this->questions_option);
- $questions = $options ? array_values($options) : [];
-
- return $questions;
- }
-
- /**
- * Add question.
- *
- * @param array $question Field attributes.
- */
- public function add_question(array $question): bool
- {
- $options = get_option($this->questions_option); // Added default value for the first time.
- if (is_array($options) || false === $options) {
- $questions = array_values($options);
- $questions[] = $question;
- $updated = update_option($this->questions_option, $questions);
-
- return $updated;
- }
-
- return false;
- }
-
- /**
- * Update question.
- *
- * @param array $question Field attributes.
- */
- public function update_question(array $question): bool
- {
- $options = get_option($this->questions_option);
-
- if (is_array($options)) {
- $questions = array_values($options);
- $index = false;
- $questions_length = count($questions);
- for ($i = 0; $i < $questions_length; $i++) {
- if ((int) $questions[ $i ]['id'] === (int) $question['id']) {
- $index = $i;
- break;
- }
- }
- if ($index >= 0) {
- $questions[ $index ] = $question;
- $updated = update_option($this->questions_option, $questions);
-
- return $updated;
- }
- }
-
- return false;
- }
-
- /**
- * Delete question.
- *
- * @param mixed $id Field id.
- */
- public function delete_question(mixed $id): bool
- {
- $options = get_option($this->questions_option);
- if (is_array($options)) {
- $questions = $options;
- $questions =
- array_filter(
- $questions,
- function ($e) use ($id) {
- return (int) $e['id'] !== (int) $id;
- }
- );
- $updated = update_option($this->questions_option, $questions);
-
- return $updated;
- }
-
- return false;
- }
-}
diff --git a/src/Settings/Features.php b/src/Settings/Features.php
index b92ae99d17..da65ec7bea 100644
--- a/src/Settings/Features.php
+++ b/src/Settings/Features.php
@@ -7,7 +7,6 @@
use P4\MasterTheme\Features\Dev\BetaBlocks;
use P4\MasterTheme\Features\Dev\CoreBlockPatterns;
use P4\MasterTheme\Features\Dev\DisableDataSync;
-use P4\MasterTheme\Features\EngagingNetworks;
use P4\MasterTheme\Features\LazyYoutubePlayer;
use P4\MasterTheme\Features\RedirectRedirectPages;
use P4\MasterTheme\Features\Planet4Blocks;
@@ -95,7 +94,6 @@ public static function all_features(): array
// Perhaps an alphabetical order within a group would make most sense?
// That way controlling whether the feature is live is in one place.
return [
- EngagingNetworks::class,
CloudflareDeployPurge::class,
LazyYoutubePlayer::class,
RedirectRedirectPages::class,
diff --git a/src/View/View.php b/src/View/View.php
deleted file mode 100644
index be5c21ff7b..0000000000
--- a/src/View/View.php
+++ /dev/null
@@ -1,152 +0,0 @@
-get_template_dir($template_name, $relevant_dir);
-
- return Timber::compile([$relevant_dir . $template_name . '.twig'], $data);
- }
-
- /**
- * Uses the appropriate templating engine to render a template file.
- *
- * @param array|string $template_name The file name of the template to render.
- * @param array $data The data to pass to the template.
- * @param string $relevant_dir The path to a subdirectory where the template is located
- * (relative to $template_dir).
- * @param bool $compile A boolean to compile the template.
- */
- public function view_template(
- array|string $template_name,
- array $data,
- string $relevant_dir = '',
- bool $compile = false
- ): string|null {
- Timber::$locations = $this->get_template_dir($template_name, $relevant_dir);
-
- if ($compile) {
- return Timber::compile([$relevant_dir . $template_name . '.twig'], $data);
- }
-
- Timber::render([$relevant_dir . $template_name . '.twig'], $data);
- return null;
- }
-
- /**
- * Overrides the template file if a child theme is active and contains one.
- *
- * @param array|string $template_name The file name of the template to render.
- * @param string $relevant_dir The path to a subdirectory where the template is located
- * (relative to $template_dir or $template_override_subdir).
- * @param string $template_ext The extension of the template (php, twig, ...).
- *
- * @return string The returned output
- */
- private function get_template_dir(
- array|string $template_name,
- string $relevant_dir = 'block_templates/',
- string $template_ext = 'twig'
- ): string {
- if (is_child_theme()) {
- $override_dir = get_stylesheet_directory() . $this->template_override_subdir;
- if (file_exists($override_dir . $relevant_dir . $template_name . '.' . $template_ext)) {
- return $override_dir;
- }
- }
-
- return $this->template_dir;
- }
-
- /**
- * Render the settings page of the plugin.
- *
- * @param array $data All the data needed to render the template.
- */
- public function settings(array $data): void
- {
- $this->view_template(__FUNCTION__, $data, 'block_templates/');
- }
-
- /**
- * Uses the appropriate templating engine to render a template file.
- *
- * @param array|string $template_name The file name of the template to render.
- * @param array $data The data to pass to the template.
- * @param string $template_ext The extension of the template (php, twig, ...).
- * @param string $relevant_dir The path to a subdirectory where the template is located
- * (relative to $template_dir).
- */
- public function block(
- array|string $template_name,
- array $data,
- string $template_ext = 'twig',
- string $relevant_dir = 'block_templates/'
- ): void {
- $template_dir = $this->get_template_dir($template_name, $relevant_dir, $template_ext);
- if ('twig' === $template_ext) {
- Timber::$locations = $template_dir;
- Timber::render([$relevant_dir . $template_name . '.' . $template_ext], $data);
- } else {
- include_once $template_dir . $relevant_dir . $template_name . '.' . $template_ext;
- }
- }
-
- /**
- * Render EN Form Post.
- *
- * @param array $data All the data needed to render the template.
- */
- public function enform_post(array $data): void
- {
- $this->view_template(__FUNCTION__, $data, '/block_templates/enform/');
- }
-
- /**
- * Render the Selected Components meta box for EN Forms.
- *
- * @param array $data All the data needed to render the template.
- */
- public function en_selected_meta_box(array $data): void
- {
- $this->view_template(__FUNCTION__, $data, 'block_templates/');
- }
-}
diff --git a/templates/block_templates/block-error-message.twig b/templates/block_templates/block-error-message.twig
deleted file mode 100644
index f79ca23e3d..0000000000
--- a/templates/block_templates/block-error-message.twig
+++ /dev/null
@@ -1 +0,0 @@
-
{{ category }}: {{ message }}
';
\ No newline at end of file
diff --git a/templates/block_templates/en_selected_meta_box.twig b/templates/block_templates/en_selected_meta_box.twig
deleted file mode 100644
index 491832ba45..0000000000
--- a/templates/block_templates/en_selected_meta_box.twig
+++ /dev/null
@@ -1,56 +0,0 @@
-{% block selected_meta_box %}
-
- {{ __( 'Hover over %s icons to get more information about each field attribute.', 'planet4-engagingnetworks-backend' )|format("")|raw }}
-
- {{ __( 'Form fields will be saved only on publish/update.', 'planet4-engagingnetworks-backend' ) }}
-
-
- {# Iterate over form fields and render each field #}
- {% for key,field in form_fields %}
- {% set errorMessage = __( 'This field is required', 'planet4-engagingnetworks' ) %}
-
- {# Construct input field name based on en type #}
- {% if field.en_type == 'GEN' or field.en_type == 'OPT' %}
- {% set en_input_name = 'supporter.questions.'~field.id %}
- {% elseif field.en_type == 'Field' %}
- {% set en_input_name = 'supporter.'~field.property %}
- {% endif %}
-
- {% if ('text' == field.input_type or 'email' == field.input_type ) %}
- {% if 'email' == field.input_type %}
- {% set errorMessage = __( 'Please enter a valid e-mail address.', 'planet4-engagingnetworks' ) %}
- {% endif %}
-
-
-
-
-
-
- {% elseif 'checkbox' == field.input_type %}
- {% if field.dependency %}
- {% set dependent_field = field.dependency %}
- {% endif %}
- {% if 'GEN' == field.en_type %}
- {% set i = 0 %}
- {% for locale, question_options in field.question_options|object_to_array_theme %}
- {% if ( locale == field.selected_locale ) %}
- {% for question_option in question_options %}
- {% set question_option = question_option|object_to_array_theme %}
-