125 lines
4.5 KiB
PHP
125 lines
4.5 KiB
PHP
<?php
|
|
/*
|
|
* vim: set ai sw=4 smarttab expandtab: tabstop=8 softtabstop=0
|
|
*
|
|
* Plugin Name: WP PDF Helper
|
|
* Description: Helps to print pages and posts as PDF
|
|
*/
|
|
|
|
// If this file is called directly, abort.
|
|
if ( ! defined( 'WPINC' ) ) {
|
|
die;
|
|
}
|
|
|
|
function ww_load_styles(){
|
|
wp_enqueue_style('dashicons');
|
|
wp_enqueue_style( 'wpdfh-main', plugin_dir_url( __FILE__ ) . 'assets/wp-pdf-helper.css' );
|
|
}
|
|
add_action('wp_enqueue_scripts', 'ww_load_styles', 999);
|
|
|
|
add_filter('manage_media_columns', 'add_media_column', 50, 2);
|
|
function add_media_column($columns, $detached=null) {
|
|
# error_log('PHP: $columns: '.print_r($columns, true), 0);
|
|
$keys = array_keys($columns);
|
|
$columns_updated = [];
|
|
foreach ($keys as $key) {
|
|
$columns_updated[$key] = $columns[$key];
|
|
if ($key == 'parent') {
|
|
$columns_updated['wp-pdf-helper'] = 'Print';
|
|
}
|
|
}
|
|
# $actions['download'] = '<a href="' . wp_get_attachment_url($post->ID) . '">Download</a>';
|
|
return $columns_updated;
|
|
}
|
|
|
|
add_action('manage_media_custom_column', 'manage_custom_columns', 10, 2);
|
|
|
|
function manage_custom_columns ($column_key, $post_id) {
|
|
if ($column_key == 'wp-pdf-helper') {
|
|
$checked = (get_post_meta($post_id, 'wpdfh.print', true) == '') ? '' : 'checked';
|
|
echo "<input type='checkbox' class='wpdfh_print_checkbox' name='Print' data-post_id='$post_id' $checked />";
|
|
// error_log("PHP: \$column_key: $column_key, \$post_id: $post_id, \$is_printable: $is_printable.", 0);
|
|
}
|
|
}
|
|
|
|
add_action('admin_enqueue_scripts', 'wpdfh_enqueue_admin_scripts');
|
|
function wpdfh_enqueue_admin_scripts() {
|
|
wp_enqueue_style( 'wpdfh-admin', plugin_dir_url( __FILE__ ) . 'assets/wp-pdf-helper-admin.css' );
|
|
wp_register_script(
|
|
'wpdfh-ajax-admin',
|
|
plugins_url('assets/jquery.ajax.js', __FILE__),
|
|
array(),
|
|
false,
|
|
true
|
|
);
|
|
wp_enqueue_script( 'wpdfh-ajax-admin' );
|
|
wp_localize_script(
|
|
'wpdfh-ajax-admin',
|
|
'wpdfh_ajax_object',
|
|
array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
|
|
);
|
|
}
|
|
|
|
add_action( "wp_ajax_wpdfh_set_print_metadata", "wpdfh_set_print_metadata" );
|
|
function wpdfh_set_print_metadata(){
|
|
error_log("PHP: wpdfh_set_print_metadata, \$_POST:".print_r($_POST, true), 0);
|
|
$post_id = $_POST['post_id'];
|
|
$checked = $_POST['checked'];
|
|
if ($checked == 'true') {
|
|
update_post_meta($post_id, 'wpdfh.print', 'always');
|
|
} else {
|
|
delete_post_meta($post_id, 'wpdfh.print');
|
|
}
|
|
wp_send_json_success();
|
|
}
|
|
|
|
|
|
if ( ! array_key_exists('print', $_GET)) {
|
|
return;
|
|
}
|
|
|
|
add_action('wp_enqueue_scripts', 'wpdfh_enqueue_print_scripts');
|
|
function wpdfh_enqueue_print_scripts() {
|
|
wp_enqueue_style( 'wpdfh-print', plugin_dir_url( __FILE__ ) . 'assets/wp-pdf-helper-print.css' );
|
|
}
|
|
|
|
//error_log("PHP: parse_url($url, PHP_URL_QUERY): ".print_r(parse_url($url, PHP_URL_QUERY), true), 0);
|
|
//error_log('PHP: $_GET: '.print_r($_GET, true), 0);
|
|
|
|
add_filter( 'wpagpx_shortcode_parameters', 'filter_shortcode_parameters' );
|
|
|
|
function filter_shortcode_parameters($parameters) {
|
|
$gpxmap_size = array_key_exists('gpxmap-size', $_GET) ? $_GET['gpxmap-size'] : 'small';
|
|
if ( $gpxmap_size === 'small') {
|
|
$parameters['mheight'] = '200px';
|
|
$parameters['gheight'] = '150px';
|
|
} else if ( $gpxmap_size === 'large') {
|
|
$parameters['mheight'] = '600px';
|
|
$parameters['gheight'] = '300px';
|
|
}
|
|
$parameters['attachments'] = 'false';
|
|
$parameters['download'] = 'false';
|
|
error_log('PHP: $_GET: '.print_r($_GET, true), 0);
|
|
return $parameters;
|
|
}
|
|
|
|
|
|
add_filter( 'get_block_templates', 'change_block_template_content', 10, 3 );
|
|
function change_block_template_content( $block_templates, $query, $template_type ) {
|
|
// error_log('PHP: get_block_template($block_template: '.print_r($block_template, true).", \$query: ".print_r($query, true).", \$template_type: $template_type)", 0);
|
|
// error_log("PHP: get_block_template(..., \$query: ".print_r($query, true).", \$template_type: $template_type)", 0);
|
|
|
|
if ( $template_type === 'wp_template' ) {
|
|
$id = $block_templates[0]->id;
|
|
$print_id = "$id-print";
|
|
$print_template = get_block_template($print_id);
|
|
error_log('PHP: get_block_template, $print_template: '.print_r($print_template, true).", \$id: ".print_r($id, true).", \$print_id: $print_id)", 0);
|
|
if ($print_template !== null) {
|
|
$block_templates[0] = $print_template;
|
|
}
|
|
}
|
|
|
|
return $block_templates;
|
|
}
|
|
|