| 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 |
|