From e2d24ae0a707e0fa0ce6d7119c9df13a353bb120 Mon Sep 17 00:00:00 2001 From: Florian Krauthan Date: Fri, 18 Jun 2021 11:18:46 -0700 Subject: [PATCH] * Removed php 4 support * Migrated the custom shell cron job to a wp-cron system * Added an option in the admin section to enable cron job --- .idea/php.xml | 2 +- .idea/sqldialects.xml | 7 ++++ cronCreatePDFs.php | 54 +++--------------------------- php4.inc.php | 19 ----------- readme.md | 6 +++- readme.txt | 5 ++- wp-mpdf.php | 16 +++++++-- wp-mpdf_admin.php | 78 +++++++++++++++++++++++++++---------------- wp-mpdf_cron.php | 60 +++++++++++++++++++++++++++++++++ 9 files changed, 144 insertions(+), 103 deletions(-) create mode 100644 .idea/sqldialects.xml delete mode 100644 php4.inc.php create mode 100644 wp-mpdf_cron.php diff --git a/.idea/php.xml b/.idea/php.xml index cd67c36..07f31c0 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..8d36da7 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/cronCreatePDFs.php b/cronCreatePDFs.php index 2aea71e..953be09 100644 --- a/cronCreatePDFs.php +++ b/cronCreatePDFs.php @@ -10,53 +10,7 @@ * You should have received a copy of the GNU General Public License along with wp-mpdf. If not, see . */ - -//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 ); diff --git a/php4.inc.php b/php4.inc.php deleted file mode 100644 index a759f5a..0000000 --- a/php4.inc.php +++ /dev/null @@ -1,19 +0,0 @@ -. - */ - -if ( ! function_exists( 'file_put_contents' ) ) { - function file_put_contents( $file, $var ) { - $fp = fopen( $file, 'w' ); - if ( $fp ) { - fputs( $fp, $var ); - fclose( $fp ); - } - } -} diff --git a/readme.md b/readme.md index a3e14f0..9a1a635 100644 --- a/readme.md +++ b/readme.md @@ -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. @@ -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) diff --git a/readme.txt b/readme.txt index 29764d4..e49c7be 100644 --- a/readme.txt +++ b/readme.txt @@ -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. @@ -17,6 +17,9 @@ Note: You can find the development repository at Github: Options'; - $themes = mpdf_admin_find_themes(); - $CODEPAGES_ARRAY = [ + $themes = mpdf_admin_find_themes(); + $CODEPAGES_ARRAY = [ 'utf-8', 'win-1251', 'win-1252', @@ -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__ ) ) ) { @@ -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 { @@ -223,6 +232,19 @@ function mpdf_admin_options() { } echo '/>'; + //Enable cron job + echo 'Enable pdf generating cron job: '; + if ( ! wp_next_scheduled( 'mpdf_generate_pdfs_hook' ) ) { + echo ''; + } else { + echo ''; + } + echo ''; + //Cron generating User echo 'User for generating per Cron: '; @@ -260,7 +282,7 @@ function mpdf_admin_listposts() { continue; } - echo ''; + echo ''; } echo ''; echo ''; @@ -270,7 +292,7 @@ function mpdf_admin_listposts() { continue; } - echo ''; + echo ''; } echo ''; echo ''; @@ -280,7 +302,7 @@ function mpdf_admin_listposts() { continue; } - echo ''; + echo ''; } echo ''; echo ''; @@ -290,7 +312,7 @@ function mpdf_admin_listposts() { continue; } - echo ''; + echo ''; } echo ''; echo ''; @@ -387,17 +409,17 @@ function mpdf_admin_allowedprintedpages() { $data = $wpdb->get_results( $sql, OBJECT ); for ( $i = 0; $i < count( $data ); $i ++ ) { echo ''; - echo '' . esc_html($data[ $i ]->post_type) . ''; + echo '' . esc_html( $data[ $i ]->post_type ) . ''; echo '   '; if ( $data[ $i ]->post_type == 'post' ) { $post = get_post( $data[ $i ]->post_id ); - echo '' . esc_html($post->post_title) . ''; + echo '' . esc_html( $post->post_title ) . ''; } else { $page = get_page( $data[ $i ]->post_id ); - echo '' . esc_html($page->post_title) . ''; + echo '' . esc_html( $page->post_title ) . ''; } echo '   '; - echo 'Delete'; + echo 'Delete'; echo ''; } echo ''; @@ -467,19 +489,19 @@ function mpdf_admin_pdfname() { $data = $wpdb->get_results( $sql, OBJECT ); for ( $i = 0; $i < count( $data ); $i ++ ) { echo ''; - echo '' . esc_html($data[ $i ]->post_type) . ''; + echo '' . esc_html( $data[ $i ]->post_type ) . ''; echo '   '; if ( $data[ $i ]->post_type == 'post' ) { $post = get_post( $data[ $i ]->post_id ); - echo '' . esc_html($post->post_title) . ''; + echo '' . esc_html( $post->post_title ) . ''; } else { $page = get_page( $data[ $i ]->post_id ); - echo '' . esc_html($page->post_title) . ''; + echo '' . esc_html( $page->post_title ) . ''; } echo ' -> '; - echo '' . esc_html($data[ $i ]->pdfname) . ''; + echo '' . esc_html( $data[ $i ]->pdfname ) . ''; echo '   '; - echo 'Delete'; + echo 'Delete'; echo ''; } echo ''; @@ -516,17 +538,17 @@ function mpdf_admin_stats() { echo ''; echo '' . ( $i + 1 ) . '. (' . $data[ $i ]->downloads . ')'; echo '   '; - echo '' . esc_html($data[ $i ]->post_type) . ''; + echo '' . esc_html( $data[ $i ]->post_type ) . ''; echo '   '; if ( $data[ $i ]->post_type == 'post' ) { $post = get_post( $data[ $i ]->post_id ); - echo '' . esc_html($post->post_title) . ''; + echo '' . esc_html( $post->post_title ) . ''; } else { $page = get_page( $data[ $i ]->post_id ); - echo '' . esc_html($page->post_title) . ''; + echo '' . esc_html( $page->post_title ) . ''; } echo '   '; - echo 'Clear'; + echo 'Clear'; echo ''; } echo ''; @@ -592,17 +614,17 @@ function mpdf_admin_loginneededpages() { $data = $wpdb->get_results( $sql, OBJECT ); for ( $i = 0; $i < count( $data ); $i ++ ) { echo ''; - echo '' . esc_html($data[ $i ]->post_type) . ''; + echo '' . esc_html( $data[ $i ]->post_type ) . ''; echo '   '; if ( $data[ $i ]->post_type == 'post' ) { $post = get_post( $data[ $i ]->post_id ); - echo '' . esc_html($post->post_title) . ''; + echo '' . esc_html( $post->post_title ) . ''; } else { $page = get_page( $data[ $i ]->post_id ); - echo '' . esc_html($page->post_title) . ''; + echo '' . esc_html( $page->post_title ) . ''; } echo '   '; - echo 'Delete'; + echo 'Delete'; echo ''; } echo ''; @@ -651,9 +673,9 @@ function mpdf_admin_cache() { if ( strtolower( substr( $file, strlen( $file ) - 5 ) ) == 'cache' ) { $pdffilename = substr( $file, 0, strlen( $file ) - 6 ); echo ''; - echo '' . esc_html(file_get_contents( plugin_dir_path( __FILE__ ) . 'cache/' . $file )) . ''; - echo '' . esc_html($pdffilename) . ''; - echo 'Delete'; + echo '' . esc_html( file_get_contents( plugin_dir_path( __FILE__ ) . 'cache/' . $file ) ) . ''; + echo '' . esc_html( $pdffilename ) . ''; + echo 'Delete'; echo ''; } } diff --git a/wp-mpdf_cron.php b/wp-mpdf_cron.php new file mode 100644 index 0000000..b53f3f4 --- /dev/null +++ b/wp-mpdf_cron.php @@ -0,0 +1,60 @@ +. + */ + +function mpdf_cron_generate_pdfs() { + global $wpdb; + + //Check if Caching is enabled or not + if ( get_option( 'mpdf_caching' ) != true ) { + echo "No caching enabled\n"; + + return; + } + + $oldUser = wp_get_current_user(); + try { + //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"; + } finally { + wp_set_current_user( $oldUser->ID ); + } +}