-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft-network-sourcelinks.php
372 lines (282 loc) · 9.43 KB
/
ft-network-sourcelinks.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* Main Plugin File for the 'ft-network-sourcelinks'
* and all related stuff
*
* @package ft-network-sourcelinks
* @version version
* @author Carsten Bach
*/
declare(strict_types=1);
namespace Figuren_Theater\Network\Sources;
use Figuren_Theater\inc\EventManager;
use Figuren_Theater\Network\Blocks;
use Figuren_Theater\Network\Options;
use Figuren_Theater\Network\Post_Types;
use Figuren_Theater\Network\Taxonomies;
/**
* Plugin Name: f.t | NETWORK Sources Management
* Description: Manage external Links as 'other' personal profiles or external sources. Handles syncing content from thoose sites, (NOT YET: using RSS-Bridge, friends,) and the old native WordPress Link-Manager a little modified.
* Plugin URI: https://figuren.theater
* Author: Carsten Bach
* Author URI: https://carsten-bach.de
* Text Domain: ft-network-sourcelinks
* Domain Path: /languages
* Version: 0.9.0
*
* @package Ft_Network_Sourcelinks
*/
const TRANSIENT_KEY = 'ft_ns_urls';
/**
* This class handles all the major use-cases for external URLs in WordPress
*
* 1. Importing your social-media content
* 2. Show your social-networks in your privacy-statement
* 3. Prepare the Social-Link(s) Blocks with defaults from your social-networks
* 4. Setting your URLs as Option of third-party plugins, like for yoasts 'wpseo_social' option
* 5. Have all links in place for your 'Impressum'
*
* and all of the dependencies of thoose workflows, like
* - a custom post_type to handle the URLs
*/
class Management implements EventManager\SubscriberInterface
{
/**
* [$urls description]
*
* @var array
*/
protected static $urls = [];
public $plugin_dir_path = '';
function __construct()
{
$this->plugin_dir_path = \plugin_dir_path( __FILE__ );
}
/**
* Returns an array of hooks that this subscriber wants to register with
* the WordPress plugin API.
*
* @return array
*/
public static function get_subscribed_events() : Array
{
return array(
'plugins_loaded' => 'enable', //
// load language files
// 'init' => ['i18n', 0 ],
// IS THIS NEEDED SOMEWHERE ???
// https://github.com/WordPress/gutenberg/issues/36785
// 'render_block_data' => 'custom_query_block_attributes',
// @todo #32 Re-ENABLE - TEMP DISABLED; until sync works, read on down the code
// 'after_setup_theme' => 'enable__on_setup_theme', // working
//
// 'admin_menu' => 'enable__on_admin', //
);
}
public function enable() : void
{
\load_plugin_textdomain(
'ft-network-sourcelinks',
false,
dirname( \plugin_basename( __FILE__ ) ) . '/languages'
// $this->plugin_dir_path . '/languages'
);
// 1. Register our post_type 'ft_link'
\Figuren_Theater\API::get('PT')->add(
Post_Types\Post_Type__ft_link::NAME,
Post_Types\Post_Type__ft_link::get_instance()
);
// 1.1 Register our shadow-taxonomy for the 'ft_link' post_type
$Taxonomy__ft_link_shadow = new Taxonomies\Taxonomy__ft_link_shadow;
\Figuren_Theater\API::get('TAX')->add(
$Taxonomy__ft_link_shadow::NAME,
$Taxonomy__ft_link_shadow
);
// 2. Re-Use old and existing 'link_category'
\Figuren_Theater\FT::site()->EventManager->add_subscriber( new Taxonomies\Taxonomy__link_category );
// 3. Importing your social-media content
// \Figuren_Theater\FT::site()->EventManager->add_subscriber( new ... );
// 4. Show your social-networks in your privacy-statement
// (disabled in favor of Blocks, loaded next)
// \Figuren_Theater\FT::site()->EventManager->add_subscriber( new LinksListsShortcode() );
// 5. Prepare the Social-Link(s) Blocks with defaults from your social-networks
// 5.1 Load some new Blocks
\Figuren_Theater\FT::site()->EventManager->add_subscriber( new Blocks\Register_Blocks );
// 5.2 Load some Block Patterns
\Figuren_Theater\FT::site()->EventManager->add_subscriber( new Blocks\Patterns );
// 6. Setting your URLs as Options of third-party plugins, like for yoasts 'wpseo_social' option
// \Figuren_Theater\FT::site()->EventManager->add_subscriber( new OptionsBridge( $this->get_urls() ) );
\Figuren_Theater\FT::site()->EventManager->add_subscriber( new Options\Preset__wpseo_social );
}
/* // IS THIS NEEDED SOMEWHERE ???
public function custom_query_block_attributes( $parsed_block ) {
if ( 'core/query' === $parsed_block['blockName'] ) {
// If the block has a `taxQuery` attribute, then find the corresponding cat ID and set the `categoryIds` attribute.
// TODO: support multiple?
if (
isset( $parsed_block[ 'attrs' ][ 'query' ][ 'search' ] )
&&
strpos( $parsed_block[ 'attrs' ][ 'query' ][ 'search' ], 'link_category:' )
) {
// die(var_dump($parsed_block));
#if ( is_string( $parsed_block[ 'attrs' ][ 'query' ][ 'taxQuery' ]['link_category'][0] ) )
$_link_cat = array_flip( explode(':', $parsed_block[ 'attrs' ][ 'query' ][ 'search' ] ) );
$tax_term = get_term_by(
'slug',
$_link_cat[0],
'link_category',
);
if ( $tax_term ) {
$parsed_block[ 'attrs' ][ 'query' ][ 'taxQuery' ]['link_category'] = [ $tax_term->term_id ];
}
$parsed_block[ 'attrs' ][ 'query' ][ 'search' ] = '';
}
}
return $parsed_block;
}*/
public function i18n()
{
\load_plugin_textdomain(
'ft-network-sourcelinks',
false,
dirname( \plugin_basename( __FILE__ ) ) . '/languages'
// $this->plugin_dir_path . '/languages'
);
/*
\wp_set_script_translations(
'figurentheater-figurentheater-production-duration-editor-script',
'ft-network-sourcelinks',
\plugin_dir_path( __FILE__ ) . 'languages'
);
\wp_set_script_translations(
'figurentheater-figurentheater-production-premiere-editor-script',
'ft-network-sourcelinks',
\plugin_dir_path( __FILE__ ) . 'languages'
);
*/
}
/**
* @todo #32
* TEMPORARILY DISABLED
* but needed when sync/importing starts
* so we can assign different post_formats based on the
* source_links post_format
*
*
* Note that you must call 'add_theme_support()' before the init hook gets called!
*
* A good hook to use is the after_setup_theme hook.
public function enable__on_setup_theme() : void
{
$post_formats = \get_post_format_slugs();
unset( $post_formats['standard'] );
\add_theme_support( 'post-formats', $post_formats );
}
*/
/*
public function enable__on_admin() : void
{
$this->debug();
}*/
public static function query_urls() : Array {
$ft_query = \Figuren_Theater\FT_Query::init();
//
return $ft_query->find_many_by_type(
Post_Types\Post_Type__ft_link::NAME,
'publish',
[
# 'cache_results' => false,
# 'update_post_meta_cache' => false,
# 'update_post_term_cache' => false,
# 'suppress_filters' => true,
## second try
'cache_results' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
// 'suppress_filters' => true,
'no_found_rows' => true, // Useful when pagination is not needed.
'posts_per_page' => 25,
]
);
}
/**
* Get stored labels from the database.
*
* Uses transients to cache simplified WP_Query results.
*
* @return Array An array of Label objects.
*/
public static function get_stored_urls() : array {
// Check if the value is already stored.
$stored_urls = \get_transient( TRANSIENT_KEY );
if ( empty( $stored_urls ) ) {
// Retrieve posts from DB.
$stored_urls = static::query_urls();
// Store for long,
// because this will beflushed with every new (and updated) 'wp_block' post.
\set_transient(
TRANSIENT_KEY,
$stored_urls,
\WEEK_IN_SECONDS
);
}
return $stored_urls;
}
public static function get_urls() : Array
{
// minimal caching
if( isset( self::$urls ) && !empty( self::$urls ) )
return self::$urls;
return self::$urls = static::get_stored_urls();
}
public static function init()
{
static $instance;
if ( NULL === $instance ) {
$instance = new self;
}
return $instance;
}
protected function debug()
{
# \do_action( 'qm/debug', $this->get_urls() );
# \do_action( 'qm/info', '{fn}: {value}', [
# 'fn' => "get_taxonomy( 'link_category' )",
# 'value' => var_export( \get_taxonomy( 'link_category' ), true ),
# ] );
#
# \do_action( 'qm/info', '{fn}: {value}', [
# 'fn' => "\get_post_type( 'link' )",
# 'value' => var_export( \get_post_type( 'link' ), true ),
# ] );
}
}
// instantiate the loader
$loader = new \Figuren_Theater\Psr4AutoloaderClass;
// register the autoloader
$loader->register();
// register the base directories for the namespace prefix
$loader->addNamespace( 'Figuren_Theater', dirname( __FILE__ ) . '/inc', true );
// $management = new Management;
$management = Management::init();
// // 7.4. Register the Manager to our site
// if ( ! is_a( \Figuren_Theater\FT::site()->EventManager, 'EventManager' ))
// return;
// if ( ! method_exists( \Figuren_Theater\FT::site()->EventManager, 'add_subscriber'))
// return;
\Figuren_Theater\FT::site()->EventManager->add_subscriber( $management );
// runs once, on activation
//
// after reading a loooong thread at
// https://core.trac.wordpress.org/ticket/14170
// I know now, that we should follow a new path
// because of multisite vs. register_activation_hook
//
// let us now do this from within the taxonomy,
// when visiting the links edit.php
/*
\register_activation_hook( __FILE__, function(){
// create 'link_category' taxonomy terms
// create first 'ft_link' using site_url()
// add_option('default_link_category')
} );*/