Last active
May 3, 2017 02:57
-
-
Save krugazul/6c48076a67e3b7efe467 to your computer and use it in GitHub Desktop.
Additional filters for Woothemes Sensei Certificates to allow custom fields to be included on your PDF.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* NB You will need to add the following lines into the actual plugin. | |
* line 114,122,130,138 and 146 - are the filters which you want to include in you sensei-certificates plugin. | |
* line 158 - this filter allows you to change the type of field, textarea or normal input. | |
*/ | |
class WooThemes_Sensei_Certificates { | |
/** | |
* Add text to the certificate | |
* | |
* @access public | |
* @since 1.0.0 | |
* @return void | |
*/ | |
public function certificate_text( $pdf_certificate, $fpdf ) { | |
global $woothemes_sensei; | |
$show_border = apply_filters( 'woothemes_sensei_certificates_show_border', 0 ); | |
$start_position = 200; | |
// Find certificate based on hash | |
$args = array( | |
'post_type' => 'certificate', | |
'meta_key' => 'certificate_hash', | |
'meta_value' => $pdf_certificate->hash | |
); | |
$query = new WP_Query( $args ); | |
$certificate_id = 0; | |
if ( $query->have_posts() ) { | |
$query->the_post(); | |
$certificate_id = $query->posts[0]->ID; | |
} // End If Statement | |
wp_reset_query(); | |
if ( 0 < intval( $certificate_id ) ) { | |
// Get Student Data | |
$user_id = get_post_meta( $certificate_id, 'learner_id', true ); | |
$student = get_userdata( $user_id ); | |
$student_name = $student->display_name; | |
$fname = $student->first_name; | |
$lname = $student->last_name; | |
if ( '' != $fname && '' != $lname ) { | |
$student_name = $fname . ' ' . $lname; | |
} | |
// Get Course Data | |
$course_id = get_post_meta( $certificate_id, 'course_id', true ); | |
$course = $woothemes_sensei->post_types->course->course_query( -1, 'usercourses', $course_id ); | |
$course = $course[0]; | |
$course_end = WooThemes_Sensei_Utils::sensei_check_for_activity( array( 'post_id' => intval( $course_id ), 'user_id' => intval( $user_id ), 'type' => 'sensei_course_status' ), true ); | |
$course_end_date = $course_end->comment_date; | |
// Get the certificate template | |
$certificate_template_id = get_post_meta( $course_id, '_course_certificate_template', true ); | |
$certificate_template_custom_fields = get_post_custom( $certificate_template_id ); | |
// Define the data we're going to load: Key => Default value | |
$load_data = array( | |
'certificate_font_style' => array(), | |
'certificate_font_color' => array(), | |
'certificate_font_size' => array(), | |
'certificate_font_family' => array(), | |
'image_ids' => array(), | |
'certificate_template_fields' => array(), | |
); | |
// Load the data from the custom fields | |
foreach ( $load_data as $key => $default ) { | |
// set value from db (unserialized if needed) or use default | |
$this->$key = ( isset( $certificate_template_custom_fields[ '_' . $key ][0] ) && '' !== $certificate_template_custom_fields[ '_' . $key ][0] ) ? ( is_array( $default ) ? maybe_unserialize( $certificate_template_custom_fields[ '_' . $key ][0] ) : $certificate_template_custom_fields[ '_' . $key ][0] ) : $default; | |
} // End For Loop | |
// Set default fonts | |
if ( isset( $this->certificate_font_color ) && '' != $this->certificate_font_color ) { $pdf_certificate->certificate_pdf_data['font_color'] = $this->certificate_font_color; } | |
if ( isset( $this->certificate_font_size ) && '' != $this->certificate_font_size ) { $pdf_certificate->certificate_pdf_data['font_size'] = $this->certificate_font_size; } | |
if ( isset( $this->certificate_font_family ) && '' != $this->certificate_font_family ) { $pdf_certificate->certificate_pdf_data['font_family'] = $this->certificate_font_family; } | |
if ( isset( $this->certificate_font_style ) && '' != $this->certificate_font_style ) { $pdf_certificate->certificate_pdf_data['font_style'] = $this->certificate_font_style; } | |
// Set default fonts | |
setlocale(LC_TIME, get_locale() ); | |
if( false !== strpos( get_locale(), 'en' ) ) { | |
$date_format = apply_filters( 'sensei_certificate_date_format', 'jS F Y' ); | |
$date = date( $date_format, strtotime( $course_end_date ) ); | |
} else { | |
$date_format = apply_filters( 'sensei_certificate_date_format', '%Y %B %e' ); | |
$date = strftime ( $date_format, strtotime( $course_end_date ) ); | |
} | |
$certificate_heading = __( 'Certificate of Completion', 'sensei-certificates' ); // Certificate of Completion | |
if ( isset( $this->certificate_template_fields['certificate_heading']['text'] ) && '' != $this->certificate_template_fields['certificate_heading']['text'] ) { | |
$certificate_heading = $this->certificate_template_fields['certificate_heading']['text']; | |
$certificate_heading = str_replace( array( '{{learner}}', '{{course_title}}', '{{completion_date}}', '{{course_place}}' ), array( $student_name, $course->post_title, $date, get_bloginfo( 'name' ) ) , $certificate_heading ); | |
$certificate_heading = apply_filters('woothemes_sensei_certificates_certificate_heading_text',$certificate_heading,$course_id,$student); | |
} // End If Statement | |
$certificate_message = __( 'This is to certify that', 'sensei-certificates' ) . " \r\n\r\n" . $student_name . " \r\n\r\n" . __( 'has completed the course', 'sensei-certificates' ); // This is to certify that {{learner}} has completed the course | |
if ( isset( $this->certificate_template_fields['certificate_message']['text'] ) && '' != $this->certificate_template_fields['certificate_message']['text'] ) { | |
$certificate_message = $this->certificate_template_fields['certificate_message']['text']; | |
$certificate_message = str_replace( array( '{{learner}}', '{{course_title}}', '{{completion_date}}', '{{course_place}}' ), array( $student_name, $course->post_title, $date, get_bloginfo( 'name' ) ) , $certificate_message ); | |
$certificate_message = apply_filters('woothemes_sensei_certificates_certificate_message_text',$certificate_message,$course_id,$student); | |
} // End If Statement | |
$certificate_course = $course->post_title; // {{course_title}} | |
if ( isset( $this->certificate_template_fields['certificate_course']['text'] ) && '' != $this->certificate_template_fields['certificate_course']['text'] ) { | |
$certificate_course = $this->certificate_template_fields['certificate_course']['text']; | |
$certificate_course = str_replace( array( '{{learner}}', '{{course_title}}', '{{completion_date}}', '{{course_place}}' ), array( $student_name, $course->post_title, $date, get_bloginfo( 'name' ) ) , $certificate_course ); | |
$certificate_course = apply_filters('woothemes_sensei_certificates_certificate_course_text',$certificate_course,$course_id,$student); | |
} // End If Statement | |
$certificate_completion = $date; // {{completion_date}} | |
if ( isset( $this->certificate_template_fields['certificate_completion']['text'] ) && '' != $this->certificate_template_fields['certificate_completion']['text'] ) { | |
$certificate_completion = $this->certificate_template_fields['certificate_completion']['text']; | |
$certificate_completion = str_replace( array( '{{learner}}', '{{course_title}}', '{{completion_date}}', '{{course_place}}' ), array( $student_name, $course->post_title, $date, get_bloginfo( 'name' ) ) , $certificate_completion ); | |
$certificate_completion = apply_filters('woothemes_sensei_certificates_certificate_completion_text',$certificate_completion,$course_id,$student); | |
} // End If Statement | |
$certificate_place = sprintf( __( 'At %s', 'sensei-certificates' ), get_bloginfo( 'name' ) ); // At {{course_place}} | |
if ( isset( $this->certificate_template_fields['certificate_place']['text'] ) && '' != $this->certificate_template_fields['certificate_place']['text'] ) { | |
$certificate_place = $this->certificate_template_fields['certificate_place']['text']; | |
$certificate_place = str_replace( array( '{{learner}}', '{{course_title}}', '{{completion_date}}', '{{course_place}}' ), array( $student_name, $course->post_title, $date, get_bloginfo( 'name' ) ) , $certificate_place ); | |
$certificate_place = apply_filters('woothemes_sensei_certificates_certificate_place_text',$certificate_place,$course_id,$student); | |
} // End If Statement | |
$output_fields = array( 'certificate_heading' => 'text_field', | |
'certificate_message' => 'textarea_field', | |
'certificate_course' => 'text_field', | |
'certificate_completion' => 'text_field', | |
'certificate_place' => 'text_field', | |
); | |
$output_fields = apply_filters('woothemes_sensei_certificates_ouput_fields',$output_fields); | |
foreach ( $output_fields as $meta_key => $function_name ) { | |
// Check if the field has a set position | |
if ( isset( $this->certificate_template_fields[$meta_key]['position']['x1'] ) ) { | |
$font_settings = $this->get_certificate_font_settings( $meta_key ); | |
call_user_func_array(array($pdf_certificate, $function_name), array( $fpdf, $$meta_key, $show_border, array( $this->certificate_template_fields[$meta_key]['position']['x1'], $this->certificate_template_fields[$meta_key]['position']['y1'], $this->certificate_template_fields[$meta_key]['position']['width'], $this->certificate_template_fields[$meta_key]['position']['height'] ), $font_settings )); | |
} // End If Statement | |
} // End For Loop | |
} else { | |
wp_die( __( 'The certificate you are searching for does not exist.', 'sensei-certificates' ), __( 'Certificate Error', 'sensei-certificates' ) ); | |
} // End If Statement | |
} // End certificate_text() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This will change the "Heading" field to output like a text area, accross multiple lines. | |
*/ | |
function certificate_output_fields( $output_fields ) { | |
$output_fields['certificate_heading'] = 'textarea_field'; | |
return $output_fields; | |
} | |
add_filter('woothemes_sensei_certificates_ouput_fields','certificate_output_fields'); | |
/* | |
* This will change the "Heading" field to output like a text area, accross multiple lines. | |
*/ | |
function certificate_course_text( $course_text, $course_id , $student ) { | |
$approval = get_post_meta($course_id,'approval',true); | |
if(false !== $approval){ | |
$course_text = str_replace('{{approval}}',$approval,$course_text); | |
} | |
return $course_text; | |
} | |
add_filter('woothemes_sensei_certificates_certificate_course_text','certificate_course_text',3,10); | |
add_filter('woothemes_sensei_certificates_certificate_message_text','certificate_course_text',3,10); | |
add_filter('woothemes_sensei_certificates_certificate_heading_text','certificate_course_text',3,10); | |
add_filter('woothemes_sensei_certificates_certificate_place_text','certificate_course_text',3,10); | |
add_filter('woothemes_sensei_certificates_certificate_completion_text','certificate_course_text',3,10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment