| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Pdf Embed |
| 4 |
* Plugin URI: https://www.francescopepe.com/ |
| 5 |
* Description: PDF embedded with official Adobe API. |
| 6 |
* Version: 0.6.1 |
| 7 |
* Author: Tropicalista |
| 8 |
* Author URI: https://www.francescopepe.com |
| 9 |
* License: GPL2 |
| 10 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 11 |
* Text Domain: pdf-embed |
| 12 |
* |
| 13 |
* @package Formello/PdfEmbed |
| 14 |
*/ |
| 15 |
|
| 16 |
require __DIR__ . '/vendor/autoload.php'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Registers the block using the metadata loaded from the `block.json` file. |
| 20 |
* Behind the scenes, it registers also all assets so they can be enqueued |
| 21 |
* through the block editor in the corresponding context. |
| 22 |
* |
| 23 |
* @see https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/writing-your-first-block-type/ |
| 24 |
*/ |
| 25 |
function pdf_embed_block_init() { |
| 26 |
register_block_type_from_metadata( |
| 27 |
__DIR__ . '/build' |
| 28 |
); |
| 29 |
|
| 30 |
$defaults = array( |
| 31 |
'apiKey' => '', |
| 32 |
'measurementId' => '', |
| 33 |
'embedMode' => 'FULL_WINDOW', |
| 34 |
'showZoomControl' => true, |
| 35 |
'showAnnotationTools' => true, |
| 36 |
'showFullScreen' => true, |
| 37 |
'defaultViewMode' => 'FIT_PAGE', |
| 38 |
'enableFormFilling' => false, |
| 39 |
'showDownloadPDF' => true, |
| 40 |
'showPrintPDF' => true, |
| 41 |
'exitPDFViewerType' => 'CLOSE', |
| 42 |
'showThumbnails' => true, |
| 43 |
'showBookmarks' => true, |
| 44 |
'enableLinearization' => false, |
| 45 |
'enableAnnotationAPIs' => false, |
| 46 |
'includePDFAnnotations' => false, |
| 47 |
'enableSearchAPIs' => true, |
| 48 |
'showDisabledSaveButton' => true, |
| 49 |
'focusOnRendering' => true, |
| 50 |
'showFullScreenViewButton' => true, |
| 51 |
'dockPageControls' => true, |
| 52 |
'enableTextSelection' => false, |
| 53 |
); |
| 54 |
|
| 55 |
$strings = array( 'apiKey', 'measurementId', 'embedMode', 'defaultViewMode', 'exitPDFViewerType' ); |
| 56 |
|
| 57 |
$properties = array(); |
| 58 |
|
| 59 |
foreach ( $defaults as $key => $value ) { |
| 60 |
if ( in_array( $key, $strings, true ) ) { |
| 61 |
$properties[ $key ] = array( |
| 62 |
'type' => 'string', |
| 63 |
); |
| 64 |
} else { |
| 65 |
$properties[ $key ] = array( |
| 66 |
'type' => 'boolean', |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$args = array( |
| 72 |
'type' => 'object', |
| 73 |
'default' => $defaults, |
| 74 |
'show_in_rest' => array( |
| 75 |
'schema' => array( |
| 76 |
'type' => 'object', |
| 77 |
'properties' => $properties, |
| 78 |
'additionalProperties' => false, |
| 79 |
), |
| 80 |
), |
| 81 |
); |
| 82 |
register_setting( 'pdf_embed', 'pdf_embed', $args ); |
| 83 |
} |
| 84 |
add_action( 'init', 'pdf_embed_block_init' ); |
| 85 |
|
| 86 |
/** |
| 87 |
* Add menu item |
| 88 |
*/ |
| 89 |
function pdf_embed_admin_menu() { |
| 90 |
$dashboard_hook = add_options_page( |
| 91 |
__( 'PDF Embed Settings', 'pdf-embed' ), |
| 92 |
__( 'PDF Embed', 'pdf-embed' ), |
| 93 |
'manage_options', |
| 94 |
'pdf-embed', |
| 95 |
'pdf_embed_admin_page' |
| 96 |
); |
| 97 |
add_action( 'load-' . $dashboard_hook, 'pdf_embed_enqueue_admin_js' ); |
| 98 |
} |
| 99 |
add_action( 'admin_menu', 'pdf_embed_admin_menu' ); |
| 100 |
|
| 101 |
/** |
| 102 |
* Enqueue admin JavaScript |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
function pdf_embed_enqueue_admin_js() { |
| 107 |
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/admin-script.asset.php'; |
| 108 |
|
| 109 |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 110 |
|
| 111 |
wp_enqueue_script( |
| 112 |
'pdf-embed-script', |
| 113 |
plugins_url( 'build/admin-script.js', __FILE__ ), |
| 114 |
$asset_file['dependencies'], |
| 115 |
$asset_file['version'], |
| 116 |
true |
| 117 |
); |
| 118 |
|
| 119 |
wp_enqueue_style( |
| 120 |
'pdf-embed-style', |
| 121 |
plugins_url( 'build/style-admin-script.css', __FILE__ ), |
| 122 |
array( 'wp-components', 'wp-reset-editor-styles' ), |
| 123 |
$asset_file['version'] |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Admin page HTML |
| 129 |
*/ |
| 130 |
function pdf_embed_admin_page() { |
| 131 |
?> |
| 132 |
<div id="pdf-embed"></div> |
| 133 |
<?php |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Register settings |
| 138 |
*/ |
| 139 |
function pdf_embed_setting() { |
| 140 |
$options = get_option( 'pdf_embed', false ); |
| 141 |
|
| 142 |
wp_add_inline_script( |
| 143 |
'tropicalista-pdfembed-view-script', |
| 144 |
'const pdf_embed = ' . wp_json_encode( $options ), |
| 145 |
'before' |
| 146 |
); |
| 147 |
wp_add_inline_script( |
| 148 |
'tropicalista-pdfembed-editor-script', |
| 149 |
'const pdf_embed = ' . wp_json_encode( $options ), |
| 150 |
'before' |
| 151 |
); |
| 152 |
} |
| 153 |
add_action( 'init', 'pdf_embed_setting' ); |
| 154 |
|
| 155 |
/** |
| 156 |
* Adds pdf embed to button |
| 157 |
* |
| 158 |
* @param string $block_content The content. |
| 159 |
* @param array $block The block. |
| 160 |
*/ |
| 161 |
function pdf_embed_render( $block_content, $block ) { |
| 162 |
if ( ! empty( $block['attrs']['embedPdf'] ) ) { |
| 163 |
wp_enqueue_script( 'tropicalista-pdfembed-view-script' ); |
| 164 |
} |
| 165 |
return $block_content; |
| 166 |
} |
| 167 |
add_filter( 'render_block', 'pdf_embed_render', 10, 2 ); |
| 168 |
|
| 169 |
/** |
| 170 |
* Add settings link on plugin page |
| 171 |
* |
| 172 |
* @param array $links The links. |
| 173 |
* @param string $plugin_file_name The plugin file name. |
| 174 |
* @return array |
| 175 |
*/ |
| 176 |
function pdf_embed_settings_link( $links, $plugin_file_name ) { |
| 177 |
if ( strpos( $plugin_file_name, basename( __FILE__ ) ) ) { |
| 178 |
array_unshift( |
| 179 |
$links, |
| 180 |
sprintf( |
| 181 |
'<a href="%s">%s</a>', |
| 182 |
add_query_arg( |
| 183 |
array( |
| 184 |
'page' => 'pdf-embed', |
| 185 |
), |
| 186 |
'admin.php' |
| 187 |
), |
| 188 |
esc_html__( 'Settings' ) |
| 189 |
) |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
return $links; |
| 194 |
} |
| 195 |
add_filter( 'plugin_action_links', 'pdf_embed_settings_link', 25, 2 ); |
| 196 |
|
| 197 |
/** |
| 198 |
* Initialize the plugin tracker |
| 199 |
* |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
function pdf_embed_init_tracker() { |
| 203 |
|
| 204 |
if ( ! class_exists( 'Appsero\Client' ) ) { |
| 205 |
require_once __DIR__ . '/appsero/src/Client.php'; |
| 206 |
} |
| 207 |
|
| 208 |
$client = new Appsero\Client( '48a870fe-75a1-476f-a4d6-bacc22ba54f1', 'Pdf Embed', __FILE__ ); |
| 209 |
|
| 210 |
$client->insights()->init(); |
| 211 |
} |
| 212 |
pdf_embed_init_tracker(); |
| 213 |
|
| 214 |
/** |
| 215 |
* Fires after tracking permission allowed (optin) |
| 216 |
* |
| 217 |
* @param array $data The Appsero data. |
| 218 |
* |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
function pdf_embed_tracker_optin( $data ) { |
| 222 |
$data['project'] = 'pdf-embed'; |
| 223 |
$response = wp_remote_post( |
| 224 |
'https://hook.eu1.make.com/dplrdfggemll51whv3b21yjabuk8po0b', |
| 225 |
array( |
| 226 |
'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
| 227 |
'body' => wp_json_encode( $data ), |
| 228 |
'method' => 'POST', |
| 229 |
'data_format' => 'body', |
| 230 |
) |
| 231 |
); |
| 232 |
} |
| 233 |
add_action( 'pdf-embed_tracker_optin', 'pdf_embed_tracker_optin', 10 ); |
| 234 |
|
| 235 |
/** |
| 236 |
* This function runs when WordPress completes its upgrade process |
| 237 |
* It iterates through each plugin updated to see if ours is included |
| 238 |
* @param $upgrader_object Array |
| 239 |
* @param $options Array |
| 240 |
*/ |
| 241 |
function pdf_embed_upgrade_completed( $upgrader_object, $options ) { |
| 242 |
// The path to our plugin's main file |
| 243 |
$our_plugin = plugin_basename( __FILE__ ); |
| 244 |
// If an update has taken place and the updated type is plugins and the plugins element exists |
| 245 |
if ( 'update' === $options['action'] && 'plugin' === $options['type'] && isset( $options['plugins'] ) ) { |
| 246 |
// Iterate through the plugins being updated and check if ours is there |
| 247 |
foreach ( $options['plugins'] as $plugin ) { |
| 248 |
if ( $plugin === $our_plugin ) { |
| 249 |
// Remove old setting. |
| 250 |
delete_option( 'pdf_embed_api_key' ); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
add_action( 'upgrader_process_complete', 'pdf_embed_upgrade_completed', 10, 2 ); |
| 256 |
|