-
Notifications
You must be signed in to change notification settings - Fork 1
/
inherit-featured-image.php
172 lines (123 loc) · 4.95 KB
/
inherit-featured-image.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
<?php
/*
* Plugin Name: JSM Inherit Parent Featured Image
* Text Domain: inherit-featured-image
* Domain Path: /languages
* Plugin URI: https://surniaulula.com/extend/plugins/inherit-featured-image/
* Assets URI: https://jsmoriss.github.io/inherit-featured-image/assets/
* Author: JS Morisset
* Author URI: https://surniaulula.com/
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl.txt
* Description: Inherit the featured image from the Post, Page, or Custom Post Type parent, grand-parent, etc.
* Requires PHP: 7.4.33
* Requires At Least: 5.9
* Tested Up To: 6.7.1
* Version: 2.1.1
*
* Version Numbering: {major}.{minor}.{bugfix}[-{stage}.{level}]
*
* {major} Major structural code changes and/or incompatible API changes (ie. breaking changes).
* {minor} New functionality was added or improved in a backwards-compatible manner.
* {bugfix} Backwards-compatible bug fixes or small improvements.
* {stage}.{level} Pre-production release: dev < a (alpha) < b (beta) < rc (release candidate).
*
* Copyright 2013-2024 Jean-Sebastien Morisset (https://surniaulula.com/)
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'These aren\'t the droids you\'re looking for.' );
}
if ( ! class_exists( 'InheritFeaturedImage' ) ) {
class InheritFeaturedImage {
private static $instance = null; // InheritFeaturedImage class object.
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init_textdomain' ) );
add_filter( 'get_post_metadata', array( __CLASS__, 'get_post_metadata' ), 10000, 4 );
add_filter( 'update_post_metadata', array( __CLASS__, 'update_post_metadata' ), 10000, 5 );
}
public static function &get_instance() {
if ( null === self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
public function init_textdomain() {
load_plugin_textdomain( 'inherit-featured-image', false, 'inherit-featured-image/languages/' );
}
public static function get_post_metadata( $check, $obj_id, $meta_key, $single ) {
if ( '_thumbnail_id' !== $meta_key ) {
return $check; // Null by default.
}
$metadata = self::get_meta_cache( $obj_id, 'post' );
/*
* If the meta key already has a value, then no need to check the parents.
*/
if ( ! empty( $metadata[ $meta_key ][ 0 ] ) ) {
return $check; // Null by default.
}
/*
* Start with the parent and work our way up - return the first value found.
*/
$ancestor_ids = get_post_ancestors( $post_id );
if ( is_array( $ancestor_ids ) ) { // Just in case.
foreach ( $ancestor_ids as $parent_id ) {
$metadata = self::get_meta_cache( $parent_id, 'post' );
if ( ! empty( $metadata[ $meta_key ][ 0 ] ) ) { // Parent has a meta key value.
if ( $single ) {
return maybe_unserialize( $metadata[ $meta_key ][ 0 ] );
}
return array_map( 'maybe_unserialize', $metadata[ $meta_key ] );
}
}
}
return $check; // Null by default.
}
public static function update_post_metadata( $check, $obj_id, $meta_key, $meta_value, $prev_value ) {
if ( '_thumbnail_id' !== $meta_key ) {
return $check; // Null by default.
}
if ( '' === $prev_value ) { // No existing previous value.
$ancestor_ids = get_post_ancestors( $post_id );
if ( is_array( $ancestor_ids ) ) { // Just in case.
foreach ( $ancestor_ids as $parent_id ) {
$metadata = self::get_meta_cache( $parent_id, 'post' );
if ( ! empty( $metadata[ $meta_key ][ 0 ] ) ) { // Parent has a meta key value.
$parent_value = maybe_unserialize( $metadata[ $meta_key ][ 0 ] );
if ( $meta_value == $parent_value ) { // Allow integer to numeric string comparison.
return false; // Do not save the meta key value.
}
}
}
}
}
return $check; // Null by default.
}
private static function get_meta_cache( $obj_id, $meta_type ) {
/*
* Retrieves the cache contents from the cache by key and group.
*
* WordPress stores data using a post, term, or user ID, along with a group string.
*
* Example: wp_cache_get( 1, 'user_meta' );
*
* Returns (bool|mixed) false on failure to retrieve contents or the cache contents on success.
*
* $found (bool) Whether the key was found in the cache (passed by reference) - disambiguates a return of false.
*/
$found = null;
$metadata = wp_cache_get( $obj_id, $meta_type . '_meta', $force = false, $found );
if ( $found ) return $metadata;
/*
* Updates the metadata cache for the specified objects.
*
* $meta_type (string) Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
*
* Returns (array|false) metadata cache for the specified objects, or false on failure.
*/
$metadata = update_meta_cache( $meta_type, array( $obj_id ) );
return $metadata[ $obj_id ];
}
}
InheritFeaturedImage::get_instance();
}