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

Add basic job details to email notifications #1434

Merged
merged 12 commits into from
Apr 12, 2018
117 changes: 116 additions & 1 deletion includes/class-wp-job-manager-email-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class WP_Job_Manager_Email_Notifications {
public static function init() {
add_action( 'job_manager_send_notification', array( __CLASS__, '_schedule_notification' ), 10, 2 );
add_action( 'job_manager_email_init', array( __CLASS__, '_lazy_init' ) );
add_action( 'job_manager_email_job_details', array( __CLASS__, 'output_job_details'), 10, 4 );
add_action( 'job_manager_email_header', array( __CLASS__, 'output_header' ), 10, 3 );
add_action( 'job_manager_email_footer', array( __CLASS__, 'output_footer' ), 10, 3 );
}
Expand Down Expand Up @@ -178,6 +179,120 @@ public static function get_template_file_name( $template_name, $plain_text = fal
return implode( '/', $file_name_parts );
}

/**
* Show details about the job listing.
*
* @param WP_Post $job The job listing to show details for.
* @param WP_Job_Manager_Email $email Email object for the notification.
* @param bool $sent_to_admin True if this is being sent to an administrator.
* @param bool $plain_text True if the email is being sent as plain text.
*/
static public function output_job_details( $job, $email, $sent_to_admin, $plain_text = false ) {
$template_segment = locate_job_manager_template( self::get_template_file_name( 'email-job-details', $plain_text ) );
if ( ! file_exists( $template_segment ) ) {
return;
}

$fields = self::get_job_detail_fields( $job, $sent_to_admin, $plain_text );

include $template_segment;
}

/**
* Get the job fields to show in email templates.
*
* @param WP_Post $job
* @param bool $sent_to_admin
* @param bool $plain_text
* @return array
*/
static private function get_job_detail_fields( WP_Post $job, $sent_to_admin, $plain_text = false ) {
$fields = array();

$fields['job_title'] = array(
'label' => __( 'Job title', 'wp-job-manager' ),
'value' => $job->post_title,
);

if ( $sent_to_admin || 'publish' === $job->post_status ) {
$fields['job_title']['url'] = get_permalink( $job );
}

$job_location = get_the_job_location( $job );
if ( ! empty( $job_location ) ) {
$fields['job_location'] = array(
'label' => __( 'Location', 'wp-job-manager' ),
'value' => $job_location,
);
}

if ( get_option( 'job_manager_enable_types' ) && wp_count_terms( 'job_listing_type' ) > 0 ) {
$job_types = wpjm_get_the_job_types( $job );
if ( ! empty( $job_types ) ) {
$fields[ 'job_type' ] = array(
'label' => __( 'Job type', 'wp-job-manager' ),
'value' => implode( ', ', wp_list_pluck( $job_types, 'name' ) ),
);
}
}

if ( get_option( 'job_manager_enable_categories' ) && wp_count_terms( 'job_listing_category' ) > 0 ) {
$job_categories = wpjm_get_the_job_categories( $job );
if ( ! empty( $job_categories ) ) {
$fields[ 'job_category' ] = array(
'label' => __( 'Job category', 'wp-job-manager' ),
'value' => implode( ', ', wp_list_pluck( $job_categories, 'name' ) ),
);
}
}

$company_name = get_the_company_name( $job );
if ( ! empty( $company_name ) ) {
$fields['company_name'] = array(
'label' => __( 'Company name', 'wp-job-manager' ),
'value' => $company_name,
);
}

$company_website = get_the_company_website( $job );
if ( ! empty( $company_website ) ) {
$fields['company_website'] = array(
'label' => __( 'Company website', 'wp-job-manager' ),
'value' => $plain_text ? $company_website : sprintf( '<a href="%1$s">%1$s</a>', esc_url( $company_website, array( 'http', 'https' ) ) ),
);
}

if ( $sent_to_admin ) {
$author = get_user_by( 'ID', $job->post_author );
if ( $author instanceof WP_User ) {
$fields[ 'author' ] = array(
'label' => __( 'Posted by', 'wp-job-manager' ),
'value' => $author->user_nicename,
'url' => 'mailto:' . $author->user_email,
);
}
}

/**
* Modify the fields shown in email notifications in the details summary a job listing.
*
* @since 1.31.0
*
* @param array $fields {
* Array of fields. Each field is keyed with a unique identifier.
* {
* @type string $label Label to show next to field.
* @type string $value Value for field.
* @type string $url URL to provide with the value (optional).
* }
* }
* @param WP_Post $job Job listing.
* @param bool $sent_to_admin True if being sent in an admin notification.
* @param bool $plain_text True if being sent as plain text.
*/
return apply_filters( 'job_manager_emails_job_detail_fields', $fields, $job, $sent_to_admin, $plain_text );
}

/**
* Output email header.
*
Expand Down Expand Up @@ -322,7 +437,7 @@ private static function get_email_content( $email_notification_key, $args ) {
do_action( 'job_manager_email_header', $email_notification_key, $args, $plain_text );

if ( $plain_text ) {
echo wptexturize( $args['plain_content'] );
echo html_entity_decode( wptexturize( $args['plain_content'] ) );
} else {
echo wpautop( wptexturize( $args['rich_content'] ) );
}
Expand Down
2 changes: 1 addition & 1 deletion templates/emails/admin-notice-new-listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$job = $args['job'];
?>
<p><?php
printf( __( 'A new job listing has been submitted titled <em>%s</em>.', 'wp-job-manager' ), esc_html( $job->post_title ) );
printf( __( 'A new job listing has been submitted to <a href="%s">%s</a>.', 'wp-job-manager' ), home_url(), get_bloginfo( 'name' ) );
switch ( $job->post_status ) {
case 'publish':
printf( ' ' . __( 'It has been published and is now available to the public.', 'wp-job-manager' ) );
Expand Down
41 changes: 41 additions & 0 deletions templates/emails/email-job-details.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Email content for showing job details.
*
* This template can be overridden by copying it to yourtheme/job_manager/emails/email-job-details.php.
*
* @see https://wpjobmanager.com/document/template-overrides/
* @author Automattic
* @package WP Job Manager
* @category Template
* @version 1.31.0
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

$text_align = is_rtl() ? 'right' : 'left';

if ( ! empty( $fields ) ) : ?>
<div class="job-manager-email-job-details-container email-container">
<table border="0" cellpadding="10" cellspacing="0" width="100%" class="job-manager-email-job-details details">
<?php foreach ( $fields as $field ) : ?>
<tr>
<td class="detail-label" style="text-align:<?php echo $text_align; ?>;">
<?php echo wp_kses_post( $field['label'] ); ?>
</td>
<td class="detail-value" style="text-align:<?php echo $text_align; ?>;">
<?php
if ( ! empty( $field['url'] ) ) {
echo sprintf( '<a href="%s">%s</a>', esc_url( $field['url'] ), wp_kses_post( $field['value'] ) );
} else {
echo wp_kses_post( $field['value'] );
}
?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<?php endif; ?>
61 changes: 53 additions & 8 deletions templates/emails/email-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,69 @@
exit; // Exit if accessed directly
}

$color_bg = apply_filters( 'job_manager_email_style_background_color', '#fff');
$color_fg = apply_filters( 'job_manager_email_style_foreground_color', '#000');
$color_link = apply_filters( 'job_manager_email_style_link_color', '#036fa9');
$font_family = apply_filters( 'job_manager_email_style_font_family', '"Helvetica Neue", Helvetica, Roboto, Arial, sans-serif' );
$style_vars = array();
$style_vars['color_bg'] = '#fff';
$style_vars['color_fg'] = '#000';
$style_vars['color_light'] = '#eee';
$style_vars['color_link'] = '#036fa9';
$style_vars['font_family'] = '"Helvetica Neue", Helvetica, Roboto, Arial, sans-serif';

/**
* Change the style vars used in email generation stylesheet.
*
* @since 1.31.0
*
* @param array $style_vars Variables used in style generation.
*/
$style_vars = apply_filters( 'job_manager_email_style_vars', $style_vars );

/**
* Inject styles before the core styles.
*
* @since 1.31.0
*
* @param array $style_vars Variables used in style generation.
*/
do_action( 'job_manager_email_style_before', $style_vars );
?>

#wrapper {
background-color: <?php echo esc_attr( $color_bg ); ?>;
color: <?php echo esc_attr( $color_fg ); ?>;
background-color: <?php echo esc_attr( $style_vars['color_bg'] ); ?>;
color: <?php echo esc_attr( $style_vars['color_fg'] ); ?>;
margin: 0;
padding: 70px 0 70px 0;
-webkit-text-size-adjust: none !important;
width: 100%;
font-family: <?php echo esc_attr( $font_family ); ?>;
font-family: <?php echo esc_attr( $style_vars['font_family'] ); ?>;
}

a {
color: <?php echo esc_attr( $color_link ); ?>;
color: <?php echo esc_attr( $style_vars['color_link'] ); ?>;
font-weight: normal;
text-decoration: underline;
}

.email-container {
margin-bottom: 10px;
}

td.detail-label,
td.detail-value {
vertical-align: middle;
border: 1px solid <?php echo esc_attr( $style_vars['color_light'] ); ?>;
}

td.detail-label {
word-wrap: break-word;
width: 40%;
}

<?php
/**
* Inject styles after the core styles.
*
* @since 1.31.0
*
* @param array $style_vars Variables used in style generation.
*/
do_action( 'job_manager_email_style_after', $style_vars );
5 changes: 2 additions & 3 deletions templates/emails/plain/admin-notice-new-listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
* @var WP_Post $job
*/
$job = $args['job'];
?>
<?php
printf( __( 'A new job listing has been submitted titled "%s".', 'wp-job-manager' ), esc_html( $job->post_title ) );

printf( __( 'A new job listing has been submitted to %s (%s).', 'wp-job-manager' ), get_bloginfo( 'name' ), home_url() );
switch ( $job->post_status ) {
case 'publish':
printf( ' ' . __( 'It has been published and is now available to the public.', 'wp-job-manager' ) );
Expand Down
28 changes: 28 additions & 0 deletions templates/emails/plain/email-job-details.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Email content for showing job details.
*
* This template can be overridden by copying it to yourtheme/job_manager/emails/plain/email-job-details.php.
*
* @see https://wpjobmanager.com/document/template-overrides/
* @author Automattic
* @package WP Job Manager
* @category Template
* @version 1.31.0
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

echo "\n\n";

if ( ! empty( $fields ) ) {
foreach ( $fields as $field ) {
echo strip_tags( $field[ 'label' ] ) .': '. strip_tags( $field[ 'value' ] );
if ( ! empty( $field['url'] ) ) {
echo ' (' . esc_url( $field['url'] ) . ')';
}
echo "\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public function setUp() {
defined( 'PHPUNIT_WPJM_TESTSUITE' ) || define( 'PHPUNIT_WPJM_TESTSUITE', true );
parent::setUp();
reset_phpmailer_instance();
update_option( 'job_manager_enable_categories', 1 );
update_option( 'job_manager_enable_types', 1 );
add_theme_support( 'job-manager-templates' );
unregister_post_type( 'job_listing' );
$post_type_instance = WP_Job_Manager_Post_Types::instance();
$post_type_instance->register_post_types();
WP_Job_Manager_Email_Notifications::_clear_deferred_notifications();
}

Expand Down Expand Up @@ -188,6 +194,28 @@ public function test_get_template_file_name_rich() {
$this->assertEquals( "emails/{$template_name}.php", WP_Job_Manager_Email_Notifications::get_template_file_name( $template_name, false ) );
}

/**
* @covers WP_Job_Manager_Email_Notifications::output_job_details()
* @covers WP_Job_Manager_Email_Notifications::get_job_detail_fields()
*/
public function test_output_job_details() {
add_filter( 'job_manager_email_notifications', array( $this, 'inject_email_config_valid_email' ) );
$emails = WP_Job_Manager_Email_Notifications::get_email_notifications( false );
remove_filter( 'job_manager_email_notifications', array( $this, 'inject_email_config_valid_email' ) );
$email = $emails['valid-email'];
$job = $this->get_valid_job();

ob_start();
WP_Job_Manager_Email_Notifications::output_job_details( $job, $email, true, true );
$content = ob_get_clean();
$this->assertContains( 'Job title: ' . $job->post_title, $content );
$this->assertContains( 'Location: ' . $job->_job_location, $content );
$this->assertContains( 'Job type: Full Time', $content );
$this->assertContains( 'Job category: Weird', $content );
$this->assertContains( 'Company name: ' . $job->_company_name, $content );
$this->assertContains( 'Company website: ' . $job->_company_website, $content );
}

/**
* @covers WP_Job_Manager_Email_Notifications::output_header()
*/
Expand Down Expand Up @@ -239,6 +267,25 @@ public function inject_email_config_valid_email( $emails ) {
return $emails;
}

protected function get_valid_job() {
$full_time_term = wp_create_term( 'Full Time', 'job_listing_type' );
$weird_cat_term = wp_create_term( 'Weird', 'job_listing_category' );
$job_args = array(
'post_title' => 'Job Post-' . md5( microtime( true ) ),
'post_content' => 'Job Description-' . md5( microtime( true ) ),
'meta_input' => array(
'_job_location' => 'Job Location-' . md5( microtime( true ) ),
'_company_name' => 'Company-' . md5( microtime( true ) ),
'_company_website' => 'http://' . md5( microtime( true ) ) .'.com',
),
'tax_input' => array(
'job_listing_type' => $full_time_term['term_id'],
'job_listing_category' => $weird_cat_term['term_id'],
),
);
return get_post( $this->factory->job_listing->create( $job_args ) );
}

/**
* @param array $core_email_class
*/
Expand Down
Loading