PluginProbe
WP Coder – Insert & Manage Code Snippets / 3.1
WP Coder – Insert & Manage Code Snippets v3.1
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / classes / Publisher / ShortcodeFinder.php

ShortcodeFinder.php in WP Coder – Insert & Manage Code Snippets 3.1, at classes/Publisher/ShortcodeFinder.php

37 lines 874 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPCoder\Publisher;
4
5 class ShortcodeFinder {
6
7 private $shortcode_name;
8
9 public function __construct($shortcode_name) {
10 $this->shortcode_name = $shortcode_name;
11 }
12
13 public function getShortcodeId($post_id) {
14 $post = get_post($post_id);
15
16 if (!$post) {
17 return false; // Return false if the post does not exist
18 }
19
20 // Use regex to find the shortcode
21 $pattern = get_shortcode_regex([ $this->shortcode_name ]);
22 if (preg_match('/' . $pattern . '/s', $post->post_content, $matches) && isset($matches[3])) {
23 // Extract attributes string
24 $attributes_string = $matches[3];
25
26 // Parse attributes
27 $attributes = shortcode_parse_atts($attributes_string);
28
29 if (isset($attributes['id'])) {
30 return $attributes['id']; // Return the ID attribute of the shortcode
31 }
32 }
33
34 return false; // Return false if no matching shortcode found
35 }
36 }
37