PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/shrewd-resume.php

https://bitbucket.org/manvscode/shrewd-resume
PHP | 334 lines | 228 code | 51 blank | 55 comment | 14 complexity | 752e808cb21dae48d07c9acd06754824 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Shrewd Resume
  4. Plugin URI: http://www.manvscode.com/
  5. Description: Manage your resume from Wordpress and generate versions in PDF and Microsoft Word.
  6. Version: 0.1
  7. Author: Joe Marrero
  8. Author URI: http://www.manvscode.com
  9. License: GPL2
  10. Copyright 2012 Joe Marrero (email: manvscode@gmail.com)
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License, version 2, as
  13. published by the Free Software Foundation.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. require_once dirname(__FILE__) . '/utility.php';
  23. define("DOMPDF_TEMP_DIR", DOCROOT.'/tmp');
  24. //define("DOMPDF_CHROOT", DOMPDF_DIR);
  25. //define("DOMPDF_UNICODE_ENABLED", false);
  26. //define("TTF2AFM", "C:/Program Files (x86)/GnuWin32/bin/ttf2pt1.exe");
  27. define( 'DOMPDF_PDF_BACKEND', 'PDFLib' );
  28. define( 'DOMPDF_DEFAULT_MEDIA_TYPE', "screen");
  29. define( 'DOMPDF_DEFAULT_PAPER_SIZE', "letter");
  30. define( 'DOMPDF_DEFAULT_FONT', "Verdana");
  31. define( 'DOMPDF_DPI', 95 );
  32. define( 'DOMPDF_ENABLE_PHP', TRUE);
  33. define( 'DOMPDF_ENABLE_JAVASCRIPT', TRUE);
  34. define( 'DOMPDF_ENABLE_REMOTE', TRUE );
  35. //define("DEBUGPNG", true);
  36. //define("DEBUGKEEPTEMP", true);
  37. //define("DEBUGCSS", true);
  38. //define("DEBUG_LAYOUT", true);
  39. //define("DEBUG_LAYOUT_LINES", false);
  40. //define("DEBUG_LAYOUT_BLOCKS", false);
  41. //define("DEBUG_LAYOUT_INLINE", false);
  42. //define("DEBUG_LAYOUT_PADDINGBOX", false);
  43. // Load DOMPDF configuration, this will prepare DOMPDF
  44. require_once dirname(__FILE__) . '/lib/dompdf-latest/dompdf_config.inc.php';
  45. add_action( 'init', array('ShrewdResume', 'createInternalUrl') );
  46. add_filter( 'query_vars', array('ShrewdResume', 'insertQueryVariables') );
  47. add_action( 'parse_request', array('ShrewdResume', 'handleRequests') );
  48. add_action( 'admin_menu', array('ShrewdResumeOptions', 'registerAdmin') );
  49. add_filter( 'the_content', array('ShrewdResume', 'replaceVariables'));
  50. add_action( 'widgets_init', array('ShrewdResume', 'registerWidgets') );
  51. class ShrewdDownloadResume_Widget extends WP_Widget
  52. {
  53. public function __construct()
  54. {
  55. parent::__construct(
  56. 'shrewddownloadresume_widget', // Base ID
  57. 'Shrewd Download Resume', // Name
  58. array( 'description' => __( 'Download Resume Widget', 'text_domain' ), ) // Args
  59. );
  60. }
  61. public function form( $instance )
  62. {
  63. if( isset( $instance[ 'title' ] ) )
  64. {
  65. $title = $instance[ 'title' ];
  66. }
  67. else
  68. {
  69. $title = __( 'Download Resume', 'text_domain' );
  70. }
  71. if( isset( $instance[ 'display_on_page' ] ) )
  72. {
  73. $display_on_page = $instance[ 'display_on_page' ];
  74. }
  75. else
  76. {
  77. $display_on_page = 'home';
  78. }
  79. ?>
  80. <p>
  81. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  82. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  83. </p>
  84. <p>
  85. <label for="<?php echo $this->get_field_id( 'display_on_page' ); ?>"><?php _e( 'Display Only On:' ); ?></label>
  86. <?php $pages = get_pages( array(
  87. 'sort_order' => 'ASC',
  88. 'sort_column' => 'post_title',
  89. 'hierarchical' => 1,
  90. 'post_type' => 'page',
  91. 'post_status' => 'publish') ); ?>
  92. <select class="widefat" id="<?php echo $this->get_field_id( 'display_on_page' ); ?>" name="<?php echo $this->get_field_name( 'display_on_page' ); ?>">
  93. <?php foreach( $pages as $page ): ?>
  94. <?php if( $page->post_name == $display_on_page ): ?>
  95. <option value="<?php echo $page->post_name; ?>" selected="selected"> <?php echo $page->post_title; ?> </option>
  96. <?php else: ?>
  97. <option value="<?php echo $page->post_name; ?>"> <?php echo $page->post_title; ?> </option>
  98. <?php endif; ?>
  99. <?php endforeach; ?>
  100. </select>
  101. </p>
  102. <?php
  103. }
  104. public function update( $new_instance, $old_instance )
  105. {
  106. $instance = array();
  107. $instance['title'] = strip_tags( $new_instance['title'] );
  108. $instance['display_on_page'] = strip_tags( $new_instance['display_on_page'] );
  109. return $instance;
  110. }
  111. public function widget( $args, $instance )
  112. {
  113. //extract( $args );
  114. $data = $args;
  115. $data[ 'title' ] = apply_filters( 'widget_title', $instance['title'] );
  116. $data[ 'display_on_page'] = $instance['display_on_page'];
  117. shrewd_views::load( 'download-resume', $data );
  118. }
  119. }
  120. class ShrewdResume
  121. {
  122. static function createInternalUrl()
  123. {
  124. $txtEnabled = shrewd_option::get( 'shrewd-resume-enable-txt', FALSE );
  125. $docEnabled = shrewd_option::get( 'shrewd-resume-enable-doc', FALSE );
  126. $pdfEnabled = shrewd_option::get( 'shrewd-resume-enable-pdf', FALSE );
  127. $typeRegEx = 'web';
  128. if( $txtEnabled ) $typeRegEx .= '|txt';
  129. if( $docEnabled ) $typeRegEx .= '|doc';
  130. if( $pdfEnabled ) $typeRegEx .= '|pdf';
  131. //add_rewrite_rule( 'resume/?$', 'index.php?shrewd_resume=1', 'top' );
  132. add_rewrite_rule( "resume/($typeRegEx)/?$", 'index.php?shrewd_resume=1&resume_type=$matches[1]', 'top' );
  133. //exit( shrewd_option::get( 'shrewd-resume-body' ) );
  134. }
  135. static function registerWidgets( )
  136. {
  137. return register_widget( 'ShrewdDownloadResume_Widget' );
  138. }
  139. static function insertQueryVariables( $query_vars )
  140. {
  141. $query_vars[] = 'shrewd_resume';
  142. $query_vars[] = 'resume_type';
  143. return $query_vars;
  144. }
  145. function handleRequests( &$wp )
  146. {
  147. if( array_key_exists( 'shrewd_resume', $wp->query_vars ) )
  148. {
  149. $type = shrewd_array::get( $wp->query_vars, 'resume_type', 'web' );
  150. $data = array(
  151. 'resume_css' => shrewd_option::get( 'shrewd-resume-css', '' ),
  152. 'resume_name' => shrewd_option::get( 'shrewd-resume-name', '' ),
  153. 'resume_author' => shrewd_option::get( 'shrewd-resume-author', '' ),
  154. 'resume_header' => shrewd_option::get( 'shrewd-resume-header', '' ),
  155. 'resume_body' => shrewd_option::get( 'shrewd-resume-body', '' ),
  156. 'resume_footer' => shrewd_option::get( 'shrewd-resume-footer', '' ),
  157. );
  158. foreach( $data as $key => &$value )
  159. {
  160. $value = ShrewdResume::replaceVariables( $value );
  161. }
  162. switch( $type )
  163. {
  164. case 'pdf':
  165. header( 'Content-Type: application/pdf' );
  166. // Render the HTML normally
  167. $html = shrewd_views::load( 'resume', $data, TRUE );
  168. // Turn off strict errors, DOMPDF is stupid like that
  169. $ER = error_reporting(~E_STRICT);
  170. // Render the HTML to a PDF
  171. $pdf = new DOMPDF;
  172. $pdf->load_html($html);
  173. $pdf->render();
  174. // Restore error reporting settings
  175. error_reporting($ER);
  176. echo $pdf->output();
  177. break;
  178. case 'txt':
  179. echo "Not implemented.";
  180. break;
  181. case 'doc':
  182. echo "Not implemented.";
  183. break;
  184. case 'web': // fall-through
  185. default:
  186. shrewd_views::load( 'resume', $data );
  187. break;
  188. }
  189. exit();
  190. }
  191. }
  192. /**
  193. * A callback that will parse out things in the form {{source:username}} and
  194. * replace it with the project list. Ex, {{github:katzgrau}}
  195. * @param string $content
  196. * @return string The updated content
  197. */
  198. static function replaceVariables( $content )
  199. {
  200. $header = '';
  201. $footer = '';
  202. if( shrewd_option::get('shrewd-resume-enable-header', FALSE) == TRUE )
  203. {
  204. $header = shrewd_option::get('shrewd-resume-header', '');
  205. }
  206. if( shrewd_option::get('shrewd-resume-enable-footer', FALSE) == TRUE )
  207. {
  208. $footer = shrewd_option::get('shrewd-resume-footer', '');
  209. }
  210. $content = str_replace( '{{RESUME_NAME}}', shrewd_option::get('shrewd-resume-name', ''), $content );
  211. $content = str_replace( '{{RESUME_HEADER}}', $header, $content );
  212. $content = str_replace( '{{RESUME_BODY}}', shrewd_option::get('shrewd-resume-body', ''), $content );
  213. $content = str_replace( '{{RESUME_FOOTER}}', $footer, $content );
  214. $content = str_replace( '{{RESUME_WEB_URL}}', home_url().'/resume/web', $content );
  215. $content = str_replace( '{{RESUME_PDF_URL}}', home_url().'/resume/pdf', $content );
  216. $content = str_replace( '{{RESUME_DOC_URL}}', home_url().'/resume/doc', $content );
  217. $content = str_replace( '{{RESUME_TXT_URL}}', home_url().'/resume/txt', $content );
  218. return $content;
  219. }
  220. }
  221. class ShrewdResumeOptions
  222. {
  223. /**
  224. * Register the admin settings page
  225. */
  226. static function registerAdmin( )
  227. {
  228. $icon = plugins_url( 'shrewd-resume/images/resume.png' );
  229. add_menu_page( 'Shrewd Resume', 'Resume', 'edit_pages', 'shrewd-resume', array(__CLASS__, 'editResume'), $icon );
  230. add_submenu_page( 'shrewd-resume', 'Settings', 'Settings', 'edit_pages', 'shrewd-resume-settings', array(__CLASS__, 'editSettings') );
  231. }
  232. /**
  233. * The function used by WP to print the admin settings page
  234. */
  235. static function editResume()
  236. {
  237. $submit = shrewd_array::get($_POST, 'submit_btn');
  238. $updated = FALSE;
  239. if($submit)
  240. {
  241. shrewd_option::set( 'shrewd-resume-name', shrewd_array::get($_POST, 'resume_name'));
  242. shrewd_option::set( 'shrewd-resume-header', shrewd_array::get($_POST, 'resume_header'));
  243. shrewd_option::set( 'shrewd-resume-body', shrewd_array::get($_POST, 'resume_body'));
  244. shrewd_option::set( 'shrewd-resume-footer', shrewd_array::get($_POST, 'resume_footer'));
  245. $updated = TRUE;
  246. }
  247. $data = array (
  248. 'resume_name' => shrewd_option::get( 'shrewd-resume-name', 'John Doe' ),
  249. 'resume_header' => shrewd_option::get( 'shrewd-resume-header', '' ),
  250. 'resume_body' => shrewd_option::get( 'shrewd-resume-body', '' ),
  251. 'resume_footer' => shrewd_option::get( 'shrewd-resume-footer', '' ),
  252. 'is_updated' => $updated
  253. );
  254. shrewd_views::load( 'edit-resume', $data );
  255. }
  256. static function editSettings()
  257. {
  258. $submit = shrewd_array::get($_POST, 'submit_btn', FALSE);
  259. $updated = FALSE;
  260. if($submit)
  261. {
  262. shrewd_option::set( 'shrewd-resume-enable-header', shrewd_array::get($_POST, 'enable_header', 0) );
  263. shrewd_option::set( 'shrewd-resume-enable-footer', shrewd_array::get($_POST, 'enable_footer', 0) );
  264. shrewd_option::set( 'shrewd-resume-enable-pdf', shrewd_array::get($_POST, 'enable_pdf', 0) );
  265. shrewd_option::set( 'shrewd-resume-enable-doc', shrewd_array::get($_POST, 'enable_doc', 0) );
  266. shrewd_option::set( 'shrewd-resume-css', shrewd_array::get($_POST, 'resume_css', '') );
  267. $updated = TRUE;
  268. }
  269. $data = array (
  270. 'enable_header' => shrewd_option::get( 'shrewd-resume-enable-header', TRUE ),
  271. 'enable_footer' => shrewd_option::get( 'shrewd-resume-enable-footer', TRUE ),
  272. 'enable_pdf' => shrewd_option::get( 'shrewd-resume-enable-pdf', TRUE ),
  273. 'enable_doc' => shrewd_option::get( 'shrewd-resume-enable-doc', TRUE ),
  274. 'resume_css' => shrewd_option::get( 'shrewd-resume-css', '' ),
  275. 'is_updated' => $updated
  276. );
  277. shrewd_views::load( 'settings', $data );
  278. }
  279. }