-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdf-embed.php
111 lines (102 loc) · 2.77 KB
/
pdf-embed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Plugin Name: Pdf Embed
* Plugin URI: https://francescopepe.com/
* Description: PDF embedded with official Adobe API.
* Version: 0.5.0
* Author: Tropicalista
* Author URI: https://www.francescopepe.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: pdf-embed
*
* @package Formello/PdfEmbed
*/
require __DIR__ . '/vendor/autoload.php';
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/writing-your-first-block-type/
*/
function pdf_embed_block_init() {
register_block_type_from_metadata(
__DIR__ . '/build'
);
$args = array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => '',
'show_in_rest' => true,
);
register_setting( 'embed_pdf', 'pdf_embed_api_key', $args );
}
add_action( 'init', 'pdf_embed_block_init' );
/**
* Register settings
*/
function pdf_embed_setting() {
$key = wp_json_encode(
array(
'apiKey' => get_option( 'pdf_embed_api_key', '' ),
)
);
wp_add_inline_script(
'tropicalista-pdfembed-view-script',
'const pdf_embed = ' . $key,
'before'
);
wp_add_inline_script(
'tropicalista-pdfembed-editor-script',
'const pdf_embed = ' . $key,
'before'
);
}
add_action( 'init', 'pdf_embed_setting' );
/**
* Adds pdf embed to button
*
* @param string $block_content The content.
* @param array $block The block.
*/
function pdf_embed_render( $block_content, $block ) {
if ( ! empty( $block['attrs']['embedPdf'] ) ) {
wp_enqueue_script( 'tropicalista-pdfembed-view-script' );
}
return $block_content;
}
add_filter( 'render_block', 'pdf_embed_render', 10, 2 );
/**
* Initialize the plugin tracker
*
* @return void
*/
function pdf_embed_init_tracker() {
if ( ! class_exists( 'Appsero\Client' ) ) {
require_once __DIR__ . '/appsero/src/Client.php';
}
$client = new Appsero\Client( '48a870fe-75a1-476f-a4d6-bacc22ba54f1', 'Pdf Embed', __FILE__ );
$client->insights()->init();
}
pdf_embed_init_tracker();
/**
* Fires after tracking permission allowed (optin)
*
* @param array $data The Appsero data.
*
* @return void
*/
function pdf_embed_tracker_optin( $data ) {
$data['project'] = 'pdf-embed';
$response = wp_remote_post(
'https://hook.eu1.make.com/dplrdfggemll51whv3b21yjabuk8po0b',
array(
'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ),
'body' => wp_json_encode( $data ),
'method' => 'POST',
'data_format' => 'body',
)
);
}
add_action( 'pdf-embed_tracker_optin', 'pdf_embed_tracker_optin', 10 );