-
Notifications
You must be signed in to change notification settings - Fork 4
/
wp-graphql-redirection.php
executable file
·132 lines (108 loc) · 4.28 KB
/
wp-graphql-redirection.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
<?php // phpcs:ignore
/**
* Plugin Name: Add WPGraphQL Redirection
* Plugin URI: https://github.com/ashhitch/wp-graphql-redirection
* Description: A WPGraphQL Extension that adds WPGraphQL support support for Redirection plugin
* Author: Ash Hitchcock
* Author URI: https://www.ashleyhitchcock.com
* Text Domain: wp-graphql-redirection
* Domain Path: /languages
* Version: 0.0.3
*
* @package WP_Graphql_REDIRECTION
*/
if (!defined('ABSPATH')) {
exit();
}
use WPGraphQL\AppContext;
add_action('admin_init', function () {
$core_dependencies = [
'WPGraphQL plugin' => class_exists('WPGraphQL'),
];
$missing_dependencies = array_keys(
array_diff($core_dependencies, array_filter($core_dependencies))
);
$display_admin_notice = static function () use ($missing_dependencies) {
?>
<div class="notice notice-error">
<p><?php esc_html_e(
'The WPGraphQL Redirection plugin can\'t be loaded because these dependencies are missing:',
'wp-graphql-redirection'
); ?>
</p>
<ul>
<?php foreach ($missing_dependencies as $missing_dependency) : ?>
<li><?php echo esc_html($missing_dependency); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php
};
if (!empty($missing_dependencies)) {
add_action('network_admin_notices', $display_admin_notice);
add_action('admin_notices', $display_admin_notice);
return;
}
});
add_action('graphql_init', function () {
add_action('graphql_register_types', function () {
register_graphql_object_type('RedirectionRedirect', [
'description' => __(
'The redirect',
'wp-graphql-redirection'
),
'fields' => [
'origin' => ['type' => 'String'],
'target' => ['type' => 'String'],
'type' => ['type' => 'String'],
'code' => ['type' => 'Int'],
'groupId' => ['type' => 'Int'],
'groupName' => ['type' => 'String'],
'matchType' => ['type' => 'String'],
],
]);
register_graphql_object_type('RedirectionRedirects', [
'description' => __('The Redirects', 'wp-graphql-redirection'),
'fields' => [
'redirects' => ['type' => ['list_of' => 'RedirectionRedirect']],
],
]);
register_graphql_field(
'RootQuery',
'redirection',
[
'type' => 'RedirectionRedirects',
'description' => __(
'The redirects',
'wp-graphql-redirection'
),
'resolve' => function (
$post,
array $args,
AppContext $context
) {
global $wpdb;
$sql = $wpdb->prepare(
"SELECT {$wpdb->prefix}redirection_items.*, {$wpdb->prefix}redirection_groups.name FROM {$wpdb->prefix}redirection_items
INNER JOIN {$wpdb->prefix}redirection_groups ON {$wpdb->prefix}redirection_groups.id={$wpdb->prefix}redirection_items.group_id
ORDER BY {$wpdb->prefix}redirection_items.position"
);
$rows = $wpdb->get_results($sql);
$items = [];
foreach ((array) $rows as $row) {
$items[] = [
'origin' => $row->url,
'target' => $row->action_data,
'code' => absint($row->action_code),
'type' => $row->action_type,
'groupId' => $row->group_id,
'groupName' => $row->name,
'matchType' => $row->match_type,
];
}
return ['redirects' => !empty($items) ? $items : null];
},
]
);
});
});