PluginProbe
Parse.ly / 3.24.3
Parse.ly v3.24.3
3.24.3 3.24.2 3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 All 107 releases
wp-parsely / src / content-helper / editor-sidebar / smart-linking / class-smart-linking.php

class-smart-linking.php in Parse.ly 3.24.3, at src/content-helper/editor-sidebar/smart-linking/class-smart-linking.php

208 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class for Smart Linking feature in the editor sidebar
4 *
5 * @package Parsely
6 * @since 3.16.0
7 */
8
9 declare( strict_types = 1 );
10
11 namespace Parsely\Content_Helper\Editor_Sidebar;
12
13 use Parsely\Content_Helper\Content_Helper_Feature;
14 use Parsely\Content_Helper\Editor_Sidebar;
15 use Parsely\Permissions;
16 use WP_Query;
17
18 /**
19 * Class for Smart Linking feature in the editor sidebar.
20 *
21 * @since 3.16.0
22 */
23 class Smart_Linking extends Editor_Sidebar_Feature {
24 /**
25 * Returns the feature's name.
26 *
27 * @since 3.17.0
28 *
29 * @return string
30 */
31 public static function get_feature_name(): string {
32 return 'smart_linking';
33 }
34
35 /**
36 * Allowed blocks for the Smart Linking feature.
37 *
38 * @since 3.16.2
39 *
40 * @var array $BLOCKS_ALLOWED
41 */
42 private const BLOCKS_ALLOWED = array(
43 'core/paragraph',
44 );
45
46 /**
47 * Constructor.
48 *
49 * @since 3.16.0
50 *
51 * @param Editor_Sidebar $editor_sidebar Instance of Editor_Sidebar class.
52 */
53 public function __construct( Editor_Sidebar $editor_sidebar ) {
54 parent::__construct( $editor_sidebar );
55
56 add_action( 'delete_post', array( $this, 'purge_smart_links' ) );
57 add_action( 'admin_enqueue_scripts', array( $this, 'add_inline_script' ) );
58 }
59
60 /**
61 * Runs the feature's initialization process.
62 *
63 * Registers the custom post type and taxonomies for the Smart Links.
64 *
65 * @since 3.16.0
66 */
67 public function run(): void {
68 // Register private custom post type for the Smart Links.
69 $this->register_post_type();
70
71 // Register the taxonomies for the Smart Links.
72 $this->register_taxonomy( 'smart_link_source', __( 'Smart Link Source', 'wp-parsely' ) );
73 $this->register_taxonomy( 'smart_link_destination', __( 'Smart Link Destination', 'wp-parsely' ) );
74 $this->register_taxonomy( 'smart_link_status', __( 'Smart Link Status', 'wp-parsely' ) );
75 }
76
77 /**
78 * Registers the custom post type for the Smart Links.
79 *
80 * @since 3.16.0
81 */
82 private function register_post_type(): void {
83 register_post_type(
84 'parsely_smart_link',
85 array(
86 'labels' => array(
87 'name' => __( 'Smart Links', 'wp-parsely' ),
88 'singular_name' => __( 'Smart Link', 'wp-parsely' ),
89 ),
90 'supports' => array( 'title', 'custom-fields' ),
91 'taxonomies' => array( 'smart_link_source', 'smart_link_destination', 'smart_link_status' ),
92 'hierarchical' => false,
93 'public' => false,
94 'show_ui' => false,
95 'can_export' => true,
96 'has_archive' => false,
97 'rewrite' => false,
98 'capability_type' => 'post',
99 'show_in_rest' => false,
100 )
101 );
102 }
103
104 /**
105 * Registers a taxonomy for the Smart Links.
106 *
107 * @since 3.16.0
108 *
109 * @param string $taxonomy The taxonomy name.
110 * @param string $singular_name The singular name of the taxonomy.
111 */
112 private function register_taxonomy( string $taxonomy, string $singular_name ): void {
113 register_taxonomy(
114 $taxonomy,
115 'parsely_smart_link',
116 array(
117 'labels' => array(
118 'name' => $singular_name,
119 'singular_name' => $singular_name,
120 ),
121 'public' => false,
122 'show_ui' => false,
123 'show_tagcloud' => false,
124 'show_admin_column' => false,
125 'show_in_rest' => false,
126 'hierarchical' => false,
127 'rewrite' => false,
128 'capabilities' => array(
129 'manage_terms' => 'edit_posts',
130 'edit_terms' => 'edit_posts',
131 'delete_terms' => 'edit_posts',
132 'assign_terms' => 'edit_posts',
133 ),
134 )
135 );
136 register_taxonomy_for_object_type( $taxonomy, 'parsely_smart_link' );
137 }
138
139 /**
140 * Adds inline script with Smart Linking specific data.
141 *
142 * @since 3.16.2
143 */
144 public function add_inline_script(): void {
145 /**
146 * Filters the allowed blocks for the Smart Linking feature.
147 *
148 * @since 3.16.2
149 *
150 * @param array $allowed_blocks The allowed blocks for the Smart Linking feature.
151 */
152 $allowed_blocks = apply_filters( 'wp_parsely_smart_linking_allowed_blocks', self::BLOCKS_ALLOWED );
153 wp_add_inline_script(
154 Editor_Sidebar::get_script_id(),
155 'window.wpParselySmartLinkingAllowedBlocks = ' . wp_json_encode( $allowed_blocks ) . ';',
156 'before'
157 );
158 }
159
160 /**
161 * Purges all outbound smart links associated with a post when the post is deleted.
162 *
163 * @since 3.16.0
164 *
165 * @param int $post_id The ID of the post being deleted.
166 */
167 public function purge_smart_links( int $post_id ): void {
168 $post = get_post( $post_id );
169 if ( null === $post ) {
170 return;
171 }
172
173 $smart_links_query = new WP_Query(
174 array(
175 'post_type' => 'parsely_smart_link',
176 'post_status' => 'any',
177 'posts_per_page' => - 1,
178 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
179 'tax_query' => array(
180 array(
181 'taxonomy' => 'smart_link_source',
182 'field' => 'slug',
183 'terms' => $post_id,
184 ),
185 ),
186 )
187 );
188
189 while ( $smart_links_query->have_posts() ) {
190 $smart_links_query->the_post();
191 $smart_link_post_id = get_the_ID();
192
193 if ( ! is_int( $smart_link_post_id ) ) {
194 continue;
195 }
196
197 wp_delete_post( $smart_link_post_id, true );
198 }
199
200 wp_reset_postdata();
201
202 // Remove any source and destination terms that have the post ID.
203 wp_delete_term( $post_id, 'smart_link_source' );
204 wp_delete_term( $post_id, 'smart_link_destination' );
205 wp_delete_term( $post_id, 'smart_link_status' );
206 }
207 }
208