Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't Require EVERY Message Template Field #3966

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions admin_pages/messages/Messages_Admin_Page.core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,8 @@ protected function _edit_message_template()
// let's get EEH_MSG_Template so we can get template form fields
$template_field_structure = EEH_MSG_Template::get_fields(
$message_template_group->messenger(),
$message_template_group->message_type()
$message_template_group->message_type(),
$context
);

if (! $template_field_structure) {
Expand Down Expand Up @@ -1484,9 +1485,7 @@ protected function _edit_message_template()
. $reference_field
. '][content]['
. $extra_field . ']';
$css_class = isset($extra_array['css_class'])
? $extra_array['css_class']
: '';
$css_class = $extra_array['css_class'] ?? '';

$template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
&& in_array($extra_field, $v_fields, true)
Expand Down Expand Up @@ -1554,10 +1553,8 @@ protected function _edit_message_template()
$template_form_fields[ $field_id ] = $field_setup_array;
$template_form_fields[ $field_id ]['name'] =
'MTP_template_fields[' . $template_field . '][content]';
$message_template =
isset($message_templates[ $context ][ $template_field ])
? $message_templates[ $context ][ $template_field ]
: null;
$message_template = $message_templates[ $context ][ $template_field ]
?? null;
$template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
&& is_array($message_templates[ $context ])
&& $message_template instanceof EE_Message_Template
Expand All @@ -1571,9 +1568,7 @@ protected function _edit_message_template()


$template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
$css_class = isset($field_setup_array['css_class'])
? $field_setup_array['css_class']
: '';
$css_class = $field_setup_array['css_class'] ?? '';
$template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
&& in_array($template_field, $v_fields, true)
&& isset($validators[ $template_field ]['msg'])
Expand Down
6 changes: 3 additions & 3 deletions caffeinated/EE_Caf_Messages.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function email_messenger_template_fields($template_fields, EE_Email_messe
'input' => 'textarea',
'label' => '[QUESTION_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand All @@ -202,7 +202,7 @@ public function html_messenger_template_fields($template_fields, EE_Html_messeng
'input' => 'textarea',
'label' => '[QUESTION_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand All @@ -220,7 +220,7 @@ public function pdf_messenger_template_fields($template_fields, EE_Pdf_messenger
'input' => 'textarea',
'label' => '[QUESTION_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand Down
24 changes: 16 additions & 8 deletions core/helpers/EEH_MSG_Template.helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1226,29 +1226,37 @@ public static function message_type_has_active_templates_for_messenger(

/**
* get_fields
* This takes a given messenger and message type and returns all the template fields indexed by context (and with field type).
* This takes a given messenger and message type
* and returns all the template fields indexed by context with field type.
*
* @param string $messenger_name name of EE_messenger
* @param string $message_type_name name of EE_message_type
* @param string $current_context current context [optional]
* @return array
* @throws EE_Error
* @throws ReflectionException
*/
public static function get_fields($messenger_name, $message_type_name)
{
$template_fields = array();
public static function get_fields(
string $messenger_name,
string $message_type_name,
string $current_context = ''
): array {
$template_fields = [];
/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
$Message_Resource_Manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
$messenger = $Message_Resource_Manager->valid_messenger($messenger_name);
$message_type = $Message_Resource_Manager->valid_message_type($message_type_name);
$messenger = $Message_Resource_Manager->valid_messenger($messenger_name);
$message_type = $Message_Resource_Manager->valid_message_type($message_type_name);
if (! EEH_MSG_Template::message_type_has_active_templates_for_messenger($messenger, $message_type)) {
return array();
return [];
}

$excluded_fields_for_messenger = $message_type->excludedFieldsForMessenger($messenger_name);

// okay now let's assemble an array with the messenger template fields added to the message_type contexts.
foreach ($message_type->get_contexts() as $context => $details) {
if ($current_context && $current_context !== $context) {
continue;
}
Comment on lines +1257 to +1259
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👍

foreach ($messenger->get_template_fields() as $field => $value) {
if (in_array($field, $excluded_fields_for_messenger, true)) {
continue;
Expand All @@ -1263,7 +1271,7 @@ public static function get_fields($messenger_name, $message_type_name)
__FUNCTION__,
__LINE__
);
return array();
return [];
}
return $template_fields;
}
Expand Down
18 changes: 11 additions & 7 deletions core/libraries/messages/EE_messenger.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,16 +652,20 @@ class="button button--secondary button--tiny edit-mtpg-button"


/**
* get_template_fields
* returns $this->_template_fields,
* filtered first by child class specific filter named something like
* FHEE__EE_Email_messenger__get_template_fields,
* followed by global FHEE__EE_messenger__get_template_fields filter
*
* @access public
* @return array $this->_template_fields
* @return array
*/
public function get_template_fields()
public function get_template_fields(): array
{
$template_fields = apply_filters('FHEE__' . get_class($this) . '__get_template_fields', $this->_template_fields, $this);
$template_fields = apply_filters('FHEE__EE_messenger__get_template_fields', $template_fields, $this);
return $template_fields;
return (array) apply_filters(
'FHEE__EE_messenger__get_template_fields',
apply_filters('FHEE__' . get_class($this) . '__get_template_fields', $this->_template_fields, $this),
$this
);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ protected function _set_template_fields()
'input' => 'wp_editor',
'label' => '[EVENT_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'rows' => '15',
Expand All @@ -341,7 +341,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[ATTENDEE_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand All @@ -352,7 +352,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[TICKET_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand All @@ -363,7 +363,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[DATETIME_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand Down
10 changes: 5 additions & 5 deletions core/libraries/messages/messenger/EE_Html_messenger.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ protected function _set_template_fields()
'input' => 'wp_editor',
'label' => '[EVENT_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'rows' => '15',
Expand All @@ -305,7 +305,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[TICKET_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand Down Expand Up @@ -355,7 +355,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[DATETIME_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand All @@ -366,7 +366,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[ATTENDEE_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand Down Expand Up @@ -399,7 +399,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[PAYMENT_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand Down
8 changes: 4 additions & 4 deletions core/libraries/messages/messenger/EE_Pdf_messenger.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected function _set_template_fields()
'input' => 'wp_editor',
'label' => '[EVENT_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'rows' => '15',
Expand All @@ -183,7 +183,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[ATTENDEE_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand All @@ -194,7 +194,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[TICKET_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand All @@ -205,7 +205,7 @@ protected function _set_template_fields()
'input' => 'textarea',
'label' => '[DATETIME_LIST]',
'type' => 'string',
'required' => true,
'required' => false,
'validation' => true,
'format' => '%s',
'css_class' => 'large-text',
Expand Down