Skip to content

Commit

Permalink
* Removed php 4 support
Browse files Browse the repository at this point in the history
* Migrated the custom shell cron job to a wp-cron system
* Added an option in the admin section to enable cron job
  • Loading branch information
fkrauthan committed Jun 18, 2021
1 parent c9a1827 commit e2d24ae
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 4 additions & 50 deletions cronCreatePDFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,7 @@
* You should have received a copy of the GNU General Public License along with wp-mpdf. If not, see <http://www.gnu.org/licenses/>.
*/


//Call this script from a cron job to create/update the pdf cache
require_once( dirname( __FILE__ ) . '/../../../wp-config.php' );
require_once( dirname( __FILE__ ) . '/wp-mpdf.php' );


//Disable the Timeout
set_time_limit( 0 );


//Check if Caching is enabled or not
if ( get_option( 'mpdf_caching' ) != true ) {
echo "No caching enabled\n";
exit( - 1 );
}


//Do login if is whished
if ( get_option( 'mpdf_cron_user' ) != '' ) {
$userId = get_option( 'mpdf_cron_user' );
if ( get_option( 'mpdf_cron_user' ) == 'auto' ) {
$aUsersID = $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->users . ' LIMIT 1' ) );
foreach ( $aUsersID as $iUserID ) {
$userId = $iUserID;
}
}

wp_set_current_user( $userId );
}


//Cache the posts
$_GET['output'] = 'pdf';
echo "Start cache creating\n";

$posts = get_posts( 'numberposts=-1&order=ASC&orderby=title' );
foreach ( $posts as $post ) {
if ( $post->post_title == '' ) {
echo "Skip post creating: No Title (" . $post->ID . ")\n";
continue;
}

echo "Create cache for post (" . $post->ID . ")\n";


query_posts( 'p=' . $post->ID );
mpdf_exec( 'false' );
}

echo "Caching finished\n";
echo "This cron script is no longer supported as per wordpress guidelines!\n";
echo "It now uses the internal wp-cron system and if enabled in the admin section.\n";
echo "Please use official plugins to change the schedule if required\n";
exit( - 1 );
19 changes: 0 additions & 19 deletions php4.inc.php

This file was deleted.

6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**Tags:** print, printer, wp-mpdf, pdf, mpdf
**Requires at least:** 2.9
**Tested up to:** 5.7.2
**Stable tag:** 3.5.2
**Stable tag:** 3.6

Print Wordpress posts as PDF. Optional with Geshi highlighting.

Expand All @@ -17,6 +17,10 @@ Print Wordpress posts as PDF. Optional with Geshi highlighting. It also has supp

## Changelog ##

### 3.6 ###
* Removed manual cron job and used wp-cron instead for cache population (as per wordpress guidelines)
* Removed PHP 4 support (don't think anyone is running that anymore)

### 3.5.2 ###
* Added some small security improvements for the admin page
* Fixed a PHP notice when "allow to print all pages" is disabled (thanks to grandeljay)
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: https://fkrauthan.de
Tags: print, printer, wp-mpdf, pdf, mpdf
Requires at least: 2.9
Tested up to: 5.7.2
Stable tag: 3.5.2
Stable tag: 3.6

Print Wordpress posts as PDF. Optional with Geshi highlighting.

Expand All @@ -17,6 +17,9 @@ Note: You can find the development repository at Github: <https://github.com/fkr

== Changelog ==

= 3.6 =
* Removed manual cron job and used wp-cron instead for cache population (as per wordpress guidelines)
* Removed PHP 4 support (don't think anyone is running that anymore)
= 3.5.2 =
* Added some small security improvements for the admin page
* Fixed a PHP notice when "allow to print all pages" is disabled (thanks to grandeljay)
Expand Down
16 changes: 13 additions & 3 deletions wp-mpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: wp-mpdf
Plugin URI: https://fkrauthan.de/projects/php/wp-mpdf.html
Description: Print a wordpress page as PDF with optional Geshi Parsing.
Version: 3.5.2
Version: 3.6
Author: Florian 'fkrauthan' Krauthan
Author URI: http://fkrauthan.de
Expand All @@ -23,7 +23,6 @@

define( 'WP_MPDF_ALLOWED_POSTS_DB', 'wp_mpdf_allowed' );
define( 'WP_MPDF_POSTS_DB', 'wp_mpdf_posts' );
require_once( dirname( __FILE__ ) . '/php4.inc.php' );

function mpdf_install() {
global $wpdb;
Expand Down Expand Up @@ -79,6 +78,15 @@ function mpdf_install() {
}
}

function mpdf_deactivate () {
wp_clear_scheduled_hook( 'mpdf_generate_pdfs_hook' );
}

function mpdf_cron_generate_pdfs_exec() {
require_once( dirname( __FILE__ ) . '/wp-mpdf_cron.php' );
mpdf_cron_generate_pdfs();
}

function mpdf_filename( $filename ) {
return str_replace( '.' . mpdf_extension( $filename ), '', $filename );
}
Expand Down Expand Up @@ -487,7 +495,7 @@ function mpdf_exec( $outputToBrowser = '' ) {
}

function mpdf_admin() {
require_once( 'wp-mpdf_admin.php' );
require_once( dirname( __FILE__ ) . '/wp-mpdf_admin.php' );
mpdf_admin_display();
}

Expand Down Expand Up @@ -632,6 +640,7 @@ function mpdf_admin_deletepost( $post_id ) {


//Register Filter
add_action( 'mpdf_generate_pdfs_hook', 'mpdf_cron_generate_pdfs_exec' );
add_action( 'delete_post', 'mpdf_admin_deletepost' );

add_action( 'template_redirect', 'mpdf_exec', 98 );
Expand All @@ -641,3 +650,4 @@ function mpdf_admin_deletepost( $post_id ) {
add_action( 'save_post', 'mpdf_admin_savepost' );

register_activation_hook( __FILE__, 'mpdf_install' );
register_deactivation_hook( __FILE__, 'mpdf_deactivate' );
78 changes: 50 additions & 28 deletions wp-mpdf_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ function mpdf_admin_find_users() {
function mpdf_admin_options() {
echo '<h2>Options</h2>';

$themes = mpdf_admin_find_themes();
$CODEPAGES_ARRAY = [
$themes = mpdf_admin_find_themes();
$CODEPAGES_ARRAY = [
'utf-8',
'win-1251',
'win-1252',
Expand All @@ -87,7 +87,8 @@ function mpdf_admin_options() {
'uhc',
'shift_jis',
];
$aUsersID = mpdf_admin_find_users();
$aUsersID = mpdf_admin_find_users();
$allowedSchedules = wp_get_schedules();

if ( isset( $_POST['save_options'] ) ) {
if ( ! isset( $_POST['wp_mpdf_noncename'] ) || ! wp_verify_nonce( $_POST['wp_mpdf_noncename'], plugin_basename( __FILE__ ) ) ) {
Expand All @@ -109,6 +110,14 @@ function mpdf_admin_options() {
update_option( 'mpdf_stats', isset( $_POST['stats'] ) );
update_option( 'mpdf_debug', isset( $_POST['debug'] ) );

if ( ! isset( $_POST['enable_cron'] ) ) {
wp_clear_scheduled_hook( 'mpdf_generate_pdfs_hook' );
} else {
if ( array_key_exists( $_POST['enable_cron'], $allowedSchedules ) ) {
wp_schedule_event( time(), $_POST['enable_cron'], 'mpdf_generate_pdfs_hook' );
}
}

if ( isset( $_POST['allow_all'] ) ) {
update_option( 'mpdf_allow_all', true );
} else {
Expand Down Expand Up @@ -223,6 +232,19 @@ function mpdf_admin_options() {
}
echo '/></td></tr>';

//Enable cron job
echo '<tr><td>Enable pdf generating cron job: </td><td>';
if ( ! wp_next_scheduled( 'mpdf_generate_pdfs_hook' ) ) {
echo '<select name="enable_cron"><option value="" selected="selected">Off</option>';
foreach ( $allowedSchedules as $schedule => $scheduleInfo ) {
echo '<option value="' . $schedule . '">' . $scheduleInfo['display'] . '</option>';
}
echo '</select>';
} else {
echo '<input type="checkbox" name="enable_cron" checked=checked />';
}
echo '</td></tr>';

//Cron generating User
echo '<tr><td>User for generating per Cron: </td><td><select name="cron_user">';
echo '<option value="" ';
Expand All @@ -242,7 +264,7 @@ function mpdf_admin_options() {
if ( $iUserID == get_option( 'mpdf_cron_user' ) ) {
echo 'selected="selected"';
}
echo '>' . esc_html($user->user_nicename) . '</option>';
echo '>' . esc_html( $user->user_nicename ) . '</option>';
}
echo '</select></td></tr>';

Expand All @@ -260,7 +282,7 @@ function mpdf_admin_listposts() {
continue;
}

echo '<option value="' . esc_attr($post->ID) . '">' . esc_html($post->post_title) . '</option>';
echo '<option value="' . esc_attr( $post->ID ) . '">' . esc_html( $post->post_title ) . '</option>';
}
echo '</optgroup>';
echo '<optgroup label="Future">';
Expand All @@ -270,7 +292,7 @@ function mpdf_admin_listposts() {
continue;
}

echo '<option value="' . esc_attr($post->ID) . '">' . esc_html($post->post_title) . '</option>';
echo '<option value="' . esc_attr( $post->ID ) . '">' . esc_html( $post->post_title ) . '</option>';
}
echo '</optgroup>';
echo '<optgroup label="Private">';
Expand All @@ -280,7 +302,7 @@ function mpdf_admin_listposts() {
continue;
}

echo '<option value="' . esc_attr($post->ID) . '">' . esc_html($post->post_title) . '</option>';
echo '<option value="' . esc_attr( $post->ID ) . '">' . esc_html( $post->post_title ) . '</option>';
}
echo '</optgroup>';
echo '<optgroup label="Publish">';
Expand All @@ -290,7 +312,7 @@ function mpdf_admin_listposts() {
continue;
}

echo '<option value="' . esc_attr($post->ID) . '">' . esc_html($post->post_title) . '</option>';
echo '<option value="' . esc_attr( $post->ID ) . '">' . esc_html( $post->post_title ) . '</option>';
}
echo '</optgroup>';
echo '</select>';
Expand Down Expand Up @@ -387,17 +409,17 @@ function mpdf_admin_allowedprintedpages() {
$data = $wpdb->get_results( $sql, OBJECT );
for ( $i = 0; $i < count( $data ); $i ++ ) {
echo '<tr>';
echo '<td>' . esc_html($data[ $i ]->post_type) . '</td>';
echo '<td>' . esc_html( $data[ $i ]->post_type ) . '</td>';
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
if ( $data[ $i ]->post_type == 'post' ) {
$post = get_post( $data[ $i ]->post_id );
echo '<td>' . esc_html($post->post_title) . '</td>';
echo '<td>' . esc_html( $post->post_title ) . '</td>';
} else {
$page = get_page( $data[ $i ]->post_id );
echo '<td>' . esc_html($page->post_title) . '</td>';
echo '<td>' . esc_html( $page->post_title ) . '</td>';
}
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
echo '<td><a href="?page=' . $_GET['page'] . '&amp;delallowedprintedpage=' . esc_attr($data[ $i ]->id) . '&amp;' . $nonceURL . '">Delete</a></td>';
echo '<td><a href="?page=' . $_GET['page'] . '&amp;delallowedprintedpage=' . esc_attr( $data[ $i ]->id ) . '&amp;' . $nonceURL . '">Delete</a></td>';
echo '</tr>';
}
echo '</table>';
Expand Down Expand Up @@ -467,19 +489,19 @@ function mpdf_admin_pdfname() {
$data = $wpdb->get_results( $sql, OBJECT );
for ( $i = 0; $i < count( $data ); $i ++ ) {
echo '<tr>';
echo '<td>' . esc_html($data[ $i ]->post_type) . '</td>';
echo '<td>' . esc_html( $data[ $i ]->post_type ) . '</td>';
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
if ( $data[ $i ]->post_type == 'post' ) {
$post = get_post( $data[ $i ]->post_id );
echo '<td>' . esc_html($post->post_title) . '</td>';
echo '<td>' . esc_html( $post->post_title ) . '</td>';
} else {
$page = get_page( $data[ $i ]->post_id );
echo '<td>' . esc_html($page->post_title) . '</td>';
echo '<td>' . esc_html( $page->post_title ) . '</td>';
}
echo '<td> -> </td>';
echo '<td>' . esc_html($data[ $i ]->pdfname) . '</td>';
echo '<td>' . esc_html( $data[ $i ]->pdfname ) . '</td>';
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
echo '<td><a href="?page=' . $_GET['page'] . '&amp;delcustomname=' . esc_attr($data[ $i ]->id) . '&' . $nonceURL . '">Delete</a></td>';
echo '<td><a href="?page=' . $_GET['page'] . '&amp;delcustomname=' . esc_attr( $data[ $i ]->id ) . '&' . $nonceURL . '">Delete</a></td>';
echo '</tr>';
}
echo '</table>';
Expand Down Expand Up @@ -516,17 +538,17 @@ function mpdf_admin_stats() {
echo '<tr>';
echo '<td>' . ( $i + 1 ) . '.&nbsp;(' . $data[ $i ]->downloads . ')</td>';
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
echo '<td>' . esc_html($data[ $i ]->post_type) . '</td>';
echo '<td>' . esc_html( $data[ $i ]->post_type ) . '</td>';
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
if ( $data[ $i ]->post_type == 'post' ) {
$post = get_post( $data[ $i ]->post_id );
echo '<td>' . esc_html($post->post_title) . '</td>';
echo '<td>' . esc_html( $post->post_title ) . '</td>';
} else {
$page = get_page( $data[ $i ]->post_id );
echo '<td>' . esc_html($page->post_title) . '</td>';
echo '<td>' . esc_html( $page->post_title ) . '</td>';
}
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
echo '<td><a href="?page=' . $_GET['page'] . '&amp;resetstat=' . esc_attr($data[ $i ]->id) . '&amp;' . $nonceURL . '">Clear</a></td>';
echo '<td><a href="?page=' . $_GET['page'] . '&amp;resetstat=' . esc_attr( $data[ $i ]->id ) . '&amp;' . $nonceURL . '">Clear</a></td>';
echo '</tr>';
}
echo '</table>';
Expand Down Expand Up @@ -592,17 +614,17 @@ function mpdf_admin_loginneededpages() {
$data = $wpdb->get_results( $sql, OBJECT );
for ( $i = 0; $i < count( $data ); $i ++ ) {
echo '<tr>';
echo '<td>' . esc_html($data[ $i ]->post_type) . '</td>';
echo '<td>' . esc_html( $data[ $i ]->post_type ) . '</td>';
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
if ( $data[ $i ]->post_type == 'post' ) {
$post = get_post( $data[ $i ]->post_id );
echo '<td>' . esc_html($post->post_title) . '</td>';
echo '<td>' . esc_html( $post->post_title ) . '</td>';
} else {
$page = get_page( $data[ $i ]->post_id );
echo '<td>' . esc_html($page->post_title) . '</td>';
echo '<td>' . esc_html( $page->post_title ) . '</td>';
}
echo '<td>&nbsp;&nbsp;&nbsp;</td>';
echo '<td><a href="?page=' . $_GET['page'] . '&amp;delloginneededpages=' . esc_attr($data[ $i ]->id) . '&amp;' . $nonceURL . '">Delete</a></td>';
echo '<td><a href="?page=' . $_GET['page'] . '&amp;delloginneededpages=' . esc_attr( $data[ $i ]->id ) . '&amp;' . $nonceURL . '">Delete</a></td>';
echo '</tr>';
}
echo '</table>';
Expand Down Expand Up @@ -651,9 +673,9 @@ function mpdf_admin_cache() {
if ( strtolower( substr( $file, strlen( $file ) - 5 ) ) == 'cache' ) {
$pdffilename = substr( $file, 0, strlen( $file ) - 6 );
echo '<tr>';
echo '<td style="padding: 5px;">' . esc_html(file_get_contents( plugin_dir_path( __FILE__ ) . 'cache/' . $file )) . '</td>';
echo '<td style="padding: 5px;"><a href="' . esc_url(plugin_dir_url( __FILE__ ) . 'cache/' . $pdffilename) . '">' . esc_html($pdffilename) . '</a></td>';
echo '<td style="padding: 5px;"><a href="?page=' . $_GET['page'] . '&amp;delfile=' . esc_attr($pdffilename) . '&amp;' . $nonceURL . '">Delete</a></td>';
echo '<td style="padding: 5px;">' . esc_html( file_get_contents( plugin_dir_path( __FILE__ ) . 'cache/' . $file ) ) . '</td>';
echo '<td style="padding: 5px;"><a href="' . esc_url( plugin_dir_url( __FILE__ ) . 'cache/' . $pdffilename ) . '">' . esc_html( $pdffilename ) . '</a></td>';
echo '<td style="padding: 5px;"><a href="?page=' . $_GET['page'] . '&amp;delfile=' . esc_attr( $pdffilename ) . '&amp;' . $nonceURL . '">Delete</a></td>';
echo '</tr>';
}
}
Expand Down
Loading

0 comments on commit e2d24ae

Please sign in to comment.