-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
212 lines (169 loc) · 5.23 KB
/
plugin.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* Plugin Name: Silverback Privacy Policies
* Plugin URI: https://github.com/silverbackstudio/wp-privacy-policy
* Description: Send Wordpress emails through Email Services API with templates
* Author: Silverback Studio
* Version: 1.0.0
* Author URI: http://www.silverbackstudio.it/
* Text Domain: svbk-privacy
* @package Silverback Privacy Policies Plugin
* @version 1.1
*/
use Svbk\WP\Privacy as WP_Privacy;
defined( 'ABSPATH' ) || exit;
define( 'SVBK_POLICY_BLOCKS_DIR', plugin_dir_path( __FILE__ ) . 'src/blocks/' );
/**
* If used as standalone plugin trigger autoloading
*/
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
require __DIR__ . '/src/providers/ProviderInterface.php';
require __DIR__ . '/src/providers/File.php';
/**
* Load all translations for our plugin from the MO file.
*/
add_action( 'init', 'svbk_policy_load_textdomain' );
function svbk_policy_load_textdomain() {
load_plugin_textdomain( 'svbk-wp-policy', false, basename( __DIR__ ) . '/languages' );
}
/**
* Registers all blocks
*/
function svbk_policy_register_block() {
register_block_type( 'svbk/privacy-policy', include( SVBK_POLICY_BLOCKS_DIR . 'privacy-policy/index.php') );
register_block_type( 'svbk/cookie-policy', include( SVBK_POLICY_BLOCKS_DIR . 'cookie-policy/index.php') );
}
add_action( 'init', 'svbk_policy_register_block');
/**
* Registers all block assets so that they can be enqueued through Gutenberg in
* the corresponding context.
*
* Passes translations to JavaScript.
*/
function svbk_policy_blocks_editor_assets() {
// automatically load dependencies and version
$asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php');
wp_register_script(
'svbk-policy-blocks',
plugins_url( 'build/index.js', __FILE__ ),
array_merge($asset_file['dependencies'], array( 'wp-blocks', 'wp-components', 'wp-editor') ),
$asset_file['version']
);
wp_enqueue_style(
'svbk-policy-blocks-editor',
plugins_url( 'build/editor.css', __FILE__ ),
$asset_file['version']
);
if ( function_exists( 'wp_set_script_translations' ) ) {
/**
* May be extended to wp_set_script_translations( 'my-handle', 'my-domain',
* plugin_dir_path( MY_PLUGIN ) . 'languages' ) ). For details see
* https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/
*/
wp_set_script_translations( 'svbk-policy-blocks', 'svbk-wp-policy', plugin_dir_path( __FILE__ ) . 'languages' );
}
}
add_action( 'enqueue_block_editor_assets', 'svbk_policy_blocks_editor_assets' );
/**
* Registers frontend scripts and assets
*/
function svbk_policy_scripts() {
wp_enqueue_style(
'svbk-policy-blocks',
plugins_url( 'build/style.css', __FILE__ )
);
}
add_action( 'wp_enqueue_scripts', 'svbk_policy_scripts' );
/**
* Set default Privacy Policy page content
*
* @param string $privacy_content The WP generated policy content
* @return string
*/
function svbk_policy_default_privacy_content($privacy_content){
$provider = svbk_policy_get_provider();
if ( $provider ) {
return
'<!-- wp:heading -->' .
'<h2> ' . __('Privacy Policy', 'svbk-wp-policy') . ' </h2>' .
'<!-- /wp:heading -->' .
'<!-- wp:svbk/privacy-policy /-->';
}
return $provider_content;
}
add_filter( 'wp_add_privacy_policy_content', 'svbk_policy_default_privacy_content' );
/**
* Retrieves the policy content
*
* @param string $name The policy name (identifier)
* @param array $attributes The attributes used to configure the policy
* @return void
*/
function svbk_policy_content($name, $attributes){
$provider = svbk_policy_get_provider();
if ( empty( $attributes['language'] ) ) {
$attributes['language'] = get_locale();
}
$attributes = apply_filters( 'svbk_policy_attributes', $attributes, $name );
$provider_content = $provider->getPolicyContent($name, $attributes);
$search = array_map( 'svbk_policy_attribute_placeholder', array_keys($attributes));
$replace = array_values($attributes);
$content = str_replace( $search, $replace, $provider_content );
return apply_filters( 'svbk_policy_content', $content, $name, $attributes);
}
/**
* Wraps attribute in it's placeholder
*
* @param string $name Attribute name
* @return void
*/
function svbk_policy_attribute_placeholder($name){
return '{{' . $name . '}}';
}
/**
* Return the currently configured provider
*
* @return void
*/
function svbk_policy_get_provider(){
$provider = new \Svbk\WP\Privacy\Providers\File;
return apply_filters('svbk_policy_provider', $provider );
}
/**
* Shortcode
*
* @param array $atts Shortcode attributes
* @return string
*/
function svbk_policy_shortcode( $atts ) {
$a = shortcode_atts( array(
'name' => 'privacy-policy',
'email' => '',
'address' => '',
'company' => '',
'phone' => '',
), $atts );
return svbk_policy_content($a['name'], $a);
}
add_shortcode( 'svbk-policy', 'svbk_policy_shortcode' );
/**
* Block Editor category filter
*
* @param array $categories
* @param int $post
* @return array
*/
function svbk_policy_block_category( $categories, $post ) {
return array_merge(
$categories,
array(
array(
'slug' => 'policy',
'title' => __( 'Policy', 'svbk-wp-policy' ),
),
)
);
}
add_filter( 'block_categories', 'svbk_policy_block_category', 10, 2);