├── templates ├── pdf.php ├── pdf-save.php ├── pdf-public.php ├── pdf-render.php └── _notes │ └── dwsync.xml ├── js ├── _notes │ └── dwsync.xml └── editor.js ├── css ├── _notes │ └── dwsync.xml └── admin-style.css ├── inc ├── _notes │ └── dwsync.xml └── inc-the-editor.php ├── _notes └── dwsync.xml ├── classes ├── _notes │ └── dwsync.xml ├── class-cahnrs-pdf-save.php └── class-cahnrs-pdf.php └── cahnrs-plugin-pdf-generator.php /templates/pdf.php: -------------------------------------------------------------------------------- 1 | generate_pdf( $post ); 8 | 9 | $pdf->stream( $response['file'] ); 10 | 11 | -------------------------------------------------------------------------------- /templates/pdf-save.php: -------------------------------------------------------------------------------- 1 | save_pdf( $post ); -------------------------------------------------------------------------------- /js/_notes/dwsync.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /css/_notes/dwsync.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /inc/_notes/dwsync.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /_notes/dwsync.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /templates/pdf-public.php: -------------------------------------------------------------------------------- 1 | ID , $pdf->meta_key , true ); 8 | 9 | $pdf_url = apply_filters( 'cahnrswp_pdf_public_url' , $pdf_url , $post ); 10 | 11 | if ( $pdf_url ){ 12 | 13 | $pdf->stream( $pdf_url , $post->post_name . '.pdf' ); 14 | 15 | } // end if -------------------------------------------------------------------------------- /classes/_notes/dwsync.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /templates/pdf-render.php: -------------------------------------------------------------------------------- 1 | connect('localhost', 11211); 7 | 8 | $memcache_obj->flush(); 9 | 10 | } // end if 11 | 12 | $pdf = new CAHNRS_PDF(); 13 | 14 | $post_id = sanitize_text_field( $_GET['pdf-id'] ); 15 | 16 | $post = get_post( $post_id ); 17 | 18 | ;?> 19 | 20 | 21 | 22 | 23 | 24 | <?php echo apply_filters( 'cahnrswp_pdf_title' , $post->post_title , $post );?> 25 | 26 | 27 | get_pdf_content( $post );?> 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /inc/inc-the-editor.php: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | Preview PDF 5 |
6 |
7 |
8 | Update PDF 9 |
10 |
11 | 12 |
13 |
14 | 17 |
-------------------------------------------------------------------------------- /templates/_notes/dwsync.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /cahnrs-plugin-pdf-generator.php: -------------------------------------------------------------------------------- 1 | init(); 22 | } // end if 23 | 24 | return self::$instance; 25 | 26 | } // end get_instance 27 | 28 | 29 | private function init(){ 30 | 31 | require_once 'classes/class-cahnrs-pdf.php'; 32 | 33 | $pdf = new CAHNRS_PDF(); 34 | 35 | $pdf->init(); 36 | 37 | } // end init 38 | 39 | } // end CAHNRS_PDF_Generator 40 | 41 | $cahnrs_pdf_generator = CAHNRS_PDF_Generator::get_instance(); 42 | -------------------------------------------------------------------------------- /js/editor.js: -------------------------------------------------------------------------------- 1 | var CAHNRS_PDF = { 2 | 3 | wrap: jQuery('#cwp-pdf-editor'), 4 | 5 | init:function(){ 6 | 7 | CAHNRS_PDF.wrap.on('click' , '.cwp-pdf-update a' , function(){ 8 | event.preventDefault(); 9 | CAHNRS_PDF.update_pdf( jQuery( this ) ); 10 | }); 11 | 12 | }, 13 | 14 | update_pdf:function( ic ){ 15 | 16 | CAHNRS_PDF.show_window(); 17 | 18 | jQuery.get( 19 | ic.attr('href'), 20 | function( response ){ 21 | 22 | console.log( response ); 23 | 24 | CAHNRS_PDF.set_response( response ); 25 | 26 | }, 27 | 'json' 28 | ); 29 | 30 | }, 31 | 32 | set_response: function( response ){ 33 | 34 | if ( response.file ){ 35 | 36 | var txt = 'Success! PDF updated'; 37 | 38 | jQuery('.cwp-pdf-url input').val( response.file ); 39 | 40 | } else { 41 | 42 | var txt = 'Well snap, something went wrong'; 43 | 44 | } // 45 | 46 | CAHNRS_PDF.update_modal( txt ); 47 | 48 | }, 49 | 50 | update_modal:function( txt ){ 51 | 52 | var modal = jQuery('#cwp-pdf-modal'); 53 | 54 | modal.find('div').html( txt ); 55 | 56 | var timer = setTimeout( function(){ CAHNRS_PDF.hide_window(); } , 1500 ); 57 | 58 | }, 59 | 60 | show_window:function(){ 61 | 62 | var msg = 'Generating PDF. Please Wait...'; 63 | 64 | var bg = '
'; 65 | 66 | var modal = '
' + msg + '
'; 67 | 68 | jQuery('body').append( bg + modal ); 69 | 70 | }, 71 | 72 | hide_window:function(){ 73 | 74 | var bg = jQuery('#cwp-pdf-bg'); 75 | 76 | var modal = jQuery('#cwp-pdf-modal'); 77 | 78 | modal.fadeOut('fast', function(){ jQuery( this ).remove()} ); 79 | 80 | bg.fadeOut('fast', function(){ jQuery( this ).remove()} ); 81 | 82 | }, 83 | 84 | } 85 | 86 | CAHNRS_PDF.init(); -------------------------------------------------------------------------------- /css/admin-style.css: -------------------------------------------------------------------------------- 1 | #cwp-pdf-editor { 2 | border: 1px solid #ddd; 3 | background: #f5f5f5; 4 | border-radius: 4px; 5 | margin: 1rem 0; 6 | padding: 12px 16px; 7 | box-sizing: border-box; 8 | } 9 | 10 | #cwp-pdf-editor:after { 11 | content: ''; 12 | display: block; 13 | clear: both; 14 | } 15 | 16 | #cwp-pdf-editor .cwp-pdf-editor-field, #cwp-pdf-editor .cwp-share-link { 17 | padding: 6px 0; 18 | } 19 | 20 | #cwp-pdf-editor .cwp-pdf-editor-field.cwp-pdf-preview { 21 | float: right; 22 | width: 92px; 23 | } 24 | 25 | #cwp-pdf-editor .cwp-pdf-editor-field a { 26 | display: block; 27 | height: 40px; 28 | line-height: 40px; 29 | text-align: center; 30 | background: #0085ba; 31 | border-color: #0073aa #006799 #006799; 32 | -webkit-box-shadow: 0 1px 0 #006799; 33 | box-shadow: 0 1px 0 #006799; 34 | color: #fff; 35 | text-decoration: none; 36 | text-shadow: 0 -1px 1px #006799,1px 0 1px #006799,0 1px 1px #006799,-1px 0 1px #006799; 37 | border-radius: 4px; 38 | } 39 | 40 | #cwp-pdf-editor .cwp-pdf-update-url { 41 | display: block; 42 | margin-right: 100px; 43 | } 44 | 45 | #cwp-pdf-editor .cwp-pdf-editor-field.cwp-pdf-url{ 46 | margin-right: 100px; 47 | } 48 | 49 | #cwp-pdf-editor .cwp-pdf-editor-field.cwp-pdf-url input { 50 | height: 40px; 51 | outline: none; 52 | border: 1px solid #0085ba; 53 | border-radius: 3px; 54 | line-height: 40px; 55 | text-indent: 8px; 56 | width: 100%; 57 | } 58 | 59 | #cwp-pdf-editor .cwp-pdf-editor-field.cwp-pdf-update { 60 | float: right; 61 | width: 92px; 62 | } 63 | 64 | #cwp-pdf-bg { 65 | position: fixed; 66 | width: 100%; 67 | height: 100%; 68 | top: 0; 69 | left: 0; 70 | background-color: rgba( 0,0,0,0.9 ); 71 | z-index: 10000; 72 | } 73 | 74 | #cwp-pdf-modal { 75 | position: fixed; 76 | top: 10%; 77 | width: 100%; 78 | left: 0; 79 | z-index: 10001; 80 | } 81 | 82 | #cwp-pdf-modal div { 83 | width: 200px; 84 | background-color: #fff; 85 | border: 1px solid #0085ba; 86 | color: #0085ba; 87 | font-size: 18px; 88 | line-height: 26px; 89 | padding: 16px; 90 | box-sizing: border-box; 91 | border-radius: 10px; 92 | margin: 0 auto; 93 | position: relative; 94 | } 95 | 96 | -------------------------------------------------------------------------------- /classes/class-cahnrs-pdf-save.php: -------------------------------------------------------------------------------- 1 | check_perms() ){ 8 | 9 | $response = $this->generate_pdf( $post ); 10 | 11 | if ( ! empty( $response['file'] ) && ! empty( $response['file_name'] ) ){ 12 | 13 | $copy_path = $this->copy_pdf( $response['file'] , $response['file_name'] ); 14 | 15 | $this->update_post_meta( $post , $copy_path ); 16 | 17 | $this->do_response( $copy_path , $response['file_name'] ); 18 | 19 | } else { 20 | 21 | $this->do_response( false , 'na' ); 22 | 23 | }// end if 24 | 25 | } else { 26 | 27 | $this->do_response( false , 'na' ); 28 | 29 | } // end if 30 | 31 | } // end save_pdf 32 | 33 | 34 | private function copy_pdf( $pdf_path , $filename ){ 35 | 36 | if ( $this->check_file( $pdf_path ) && $this->check_perms() ){ 37 | 38 | $uploaddir = wp_upload_dir( 'pdfs'); 39 | 40 | $uploadfile = $uploaddir['path'] . $filename; 41 | 42 | $contents= file_get_contents( $pdf_path ); 43 | 44 | $savefile = fopen( $uploadfile , 'w' ); 45 | 46 | fwrite($savefile, $contents); 47 | 48 | fclose($savefile); 49 | 50 | return $uploaddir['url'] . $filename; 51 | 52 | } // end if 53 | 54 | return false; 55 | 56 | } // end copy_pdf 57 | 58 | 59 | private function update_post_meta( $post , $file_url ){ 60 | 61 | if ( $file_url && $this->check_perms() ) { 62 | 63 | update_post_meta( $post->ID , $this->meta_key , $file_url ); 64 | 65 | } // end if 66 | 67 | } // end update_post_meta 68 | 69 | 70 | private function check_perms(){ 71 | 72 | if ( is_user_logged_in() && current_user_can( 'edit_posts' ) ){ 73 | 74 | return true; 75 | 76 | } // end if 77 | 78 | return false; 79 | 80 | } // end check_perms 81 | 82 | 83 | private function check_file( $path ){ 84 | 85 | $regex = '/^http:\/\/134\.121\.225\.236/'; 86 | 87 | if ( preg_match( $regex , $path ) ){ 88 | 89 | return true; 90 | 91 | } // end if 92 | 93 | return false; 94 | 95 | } // end path 96 | 97 | 98 | protected function do_response( $path , $filename ){ 99 | 100 | $json = array( 101 | 'file' => $path, 102 | 'file_name' => $filename, 103 | ); 104 | 105 | echo json_encode( $json ); 106 | 107 | } 108 | 109 | } // end CAHNRS_PDF_Save -------------------------------------------------------------------------------- /classes/class-cahnrs-pdf.php: -------------------------------------------------------------------------------- 1 | post_content , $post ); 60 | 61 | return $pdf_content; 62 | 63 | } // end get_pdf_content 64 | 65 | 66 | public function generate_pdf( $post ){ 67 | 68 | $build_url = $this->build_pdf_url( $post ); 69 | 70 | $request_url = $this->build_request_url( $build_url , $post ); 71 | 72 | $json = file_get_contents( $request_url ); 73 | 74 | $pdf = json_decode( $json , true ); 75 | 76 | return $pdf; 77 | 78 | } // end generate_pdf 79 | 80 | 81 | protected function build_pdf_url( $post ){ 82 | 83 | $url = get_site_url(); 84 | 85 | if ( strpos( $url , '?' ) !== false ){ 86 | 87 | $url .= '&'; 88 | 89 | } else { 90 | 91 | $url .= '?'; 92 | 93 | } // end if 94 | 95 | $url .= 'pdf-id=' . $post->ID; 96 | 97 | return $url; 98 | 99 | } // end build_pdf_url 100 | 101 | 102 | protected function build_request_url( $build_url , $post ){ 103 | 104 | $url = $this->request_url . 'access-key=' . $this->access_key; 105 | 106 | $url .= '&folder=' . $this->build_site_url(); 107 | 108 | $url .= '&filename=' . $post->post_name; 109 | 110 | $url .= '&src=' . urlencode( $build_url ); 111 | 112 | return $url; 113 | 114 | } 115 | 116 | protected function build_site_url(){ 117 | 118 | $url = get_site_url(); 119 | 120 | $url = str_replace( array( 'http://','https://','www' ) , '' , $url ); 121 | 122 | $url = str_replace( array('.','/') , '-' , $url ); 123 | 124 | return $url; 125 | 126 | } // end build_site_url 127 | 128 | 129 | public function stream( $file_path , $filename = false ){ 130 | 131 | if ( ! $filename ) $filename = $this->filename; 132 | 133 | 134 | if ( isset( $file_path ) ){ 135 | 136 | $output = file_get_contents( $file_path ); 137 | 138 | header("Content-type: application/pdf"); 139 | header('Content-disposition: inline;filename=' . $filename ); 140 | 141 | echo $output; 142 | 143 | } // end if 144 | 145 | } // end stream 146 | 147 | 148 | public function the_editor( $post ){ 149 | 150 | $pdf_url = apply_filters( 'cahnrswp_pdf_public_url' , get_post_meta( $post->ID , $this->meta_key , true ) , $post ); 151 | 152 | $post_link = get_post_permalink( $post->ID ); 153 | 154 | include dirname( dirname( __FILE__ ) ) . '/inc/inc-the-editor.php'; 155 | 156 | } // end the_eidtor 157 | 158 | 159 | /** 160 | * Add admin scripts 161 | */ 162 | public function add_admin_scripts( $hook ){ 163 | 164 | if ( $hook == 'post-new.php' || $hook == 'post.php' ) { 165 | 166 | wp_enqueue_style( 167 | 'cwp_pdf_admin_style' , 168 | plugin_dir_url( dirname( __FILE__ ) ) . 'css/admin-style.css', 169 | array() , 170 | CAHNRS_PDF_Generator::$version 171 | ); 172 | 173 | wp_enqueue_script( 174 | 'cwp_pdf_admin_js', 175 | plugin_dir_url( dirname( __FILE__ ) ) . 'js/editor.js', 176 | array('jquery-ui-draggable','jquery-ui-droppable','jquery-ui-sortable'), 177 | CAHNRS_PDF_Generator::$version, 178 | true 179 | ); 180 | 181 | } // end if 182 | 183 | } // end add_admin_scripts 184 | 185 | 186 | public function save_pdf( $post_id ){ 187 | 188 | if ( ! empty( $_POST[ $this->meta_key ] ) && $this->check_save_permissions( $post_id ) ){ 189 | 190 | update_post_meta( $post_id , $this->meta_key , sanitize_text_field( $_POST[ $this->meta_key ] ) ); 191 | 192 | } // end if 193 | 194 | } // end save_pdf 195 | 196 | 197 | /** 198 | * Check user permissions 199 | * @param int $post_id Post ID 200 | * @return bool TRUE if has permissions otherwise FALSE 201 | */ 202 | protected function check_save_permissions( $post_id ){ 203 | 204 | // If this is an autosave, our form has not been submitted, so we don't want to do anything. 205 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 206 | 207 | return false; 208 | 209 | } // end if 210 | 211 | // Check the user's permissions. 212 | if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) { 213 | 214 | if ( current_user_can( 'edit_page', $post_id ) ) { 215 | 216 | return true; 217 | 218 | } // end if 219 | 220 | } else { 221 | 222 | if ( current_user_can( 'edit_post', $post_id ) ) { 223 | 224 | return true; 225 | 226 | } // end if 227 | 228 | } // end if 229 | 230 | return false; 231 | 232 | }// end check_permissions 233 | 234 | 235 | } --------------------------------------------------------------------------------