| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2025 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WpssoIntegUserCoAuthors' ) ) { |
| 14 |
|
| 15 |
class WpssoIntegUserCoAuthors { |
| 16 |
|
| 17 |
private $p; // Wpsso class object. |
| 18 |
|
| 19 |
public function __construct( &$plugin ) { |
| 20 |
|
| 21 |
$this->p =& $plugin; |
| 22 |
|
| 23 |
if ( $this->p->debug->enabled ) { |
| 24 |
|
| 25 |
$this->p->debug->mark(); |
| 26 |
} |
| 27 |
|
| 28 |
$this->p->util->add_plugin_filters( $this, array( |
| 29 |
'get_post_mod' => 2, |
| 30 |
'get_other_user_images' => 5, |
| 31 |
'get_other_user_meta' => 2, |
| 32 |
'get_author_meta' => array( |
| 33 |
'get_author_meta' => 4, |
| 34 |
'get_author_website' => 4, |
| 35 |
), |
| 36 |
'check_post_head' => 3, |
| 37 |
'description_seed' => 4, |
| 38 |
) ); |
| 39 |
|
| 40 |
$this->p->util->add_plugin_filters( $this, array( |
| 41 |
'get_user_object' => 2, |
| 42 |
), $prio = 50, $ext = 'sucom' ); // Note the 'sucom' filter prefix. |
| 43 |
|
| 44 |
add_filter( 'coauthors_guest_author_fields', array( $this, 'add_contact_methods' ), 20, 2 ); |
| 45 |
} |
| 46 |
|
| 47 |
public function filter_get_post_mod( $mod, $mod_id ) { |
| 48 |
|
| 49 |
if ( $this->p->debug->enabled ) { |
| 50 |
|
| 51 |
$this->p->debug->mark(); |
| 52 |
} |
| 53 |
|
| 54 |
$coauthors = get_coauthors( $mod_id ); |
| 55 |
|
| 56 |
if ( empty( $coauthors ) || ! is_array( $coauthors ) ) { |
| 57 |
|
| 58 |
if ( $this->p->debug->enabled ) { |
| 59 |
|
| 60 |
$this->p->debug->log( 'no coauthors found for post ID ' . $mod_id ); |
| 61 |
} |
| 62 |
|
| 63 |
return $mod; |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* Make sure the first (top) author listed is the post / page author. |
| 68 |
*/ |
| 69 |
$author = reset( $coauthors ); |
| 70 |
|
| 71 |
if ( ! empty( $author->ID ) && (int) $author->ID !== $mod[ 'post_author' ] ) { |
| 72 |
|
| 73 |
if ( $this->p->debug->enabled ) { |
| 74 |
|
| 75 |
$this->p->debug->log( 'setting author id ' . $author->ID . ' as primary author' ); |
| 76 |
} |
| 77 |
|
| 78 |
$mod[ 'post_author' ] = (int) $author->ID; |
| 79 |
} |
| 80 |
|
| 81 |
foreach ( $coauthors as $author ) { |
| 82 |
|
| 83 |
if ( (int) $author->ID === $mod[ 'post_author' ] ) { |
| 84 |
|
| 85 |
if ( $this->p->debug->enabled ) { |
| 86 |
|
| 87 |
$this->p->debug->log( 'skipping coauthor id ' . $author->ID . ' (primary author)' ); |
| 88 |
} |
| 89 |
|
| 90 |
} else { |
| 91 |
|
| 92 |
if ( $this->p->debug->enabled ) { |
| 93 |
|
| 94 |
$this->p->debug->log( 'adding coauthor id ' . $author->ID ); |
| 95 |
} |
| 96 |
|
| 97 |
$mod[ 'post_coauthors' ][] = (int) $author->ID; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $mod; |
| 102 |
} |
| 103 |
|
| 104 |
/* |
| 105 |
* Hooked to 'sucom_get_user_object'. |
| 106 |
*/ |
| 107 |
public function filter_get_user_object( $user_obj, $user_id ) { |
| 108 |
|
| 109 |
global $coauthors_plus; |
| 110 |
|
| 111 |
if ( ! is_object( $user_obj ) && $user_id ) { |
| 112 |
|
| 113 |
if ( $this->p->debug->enabled ) { |
| 114 |
|
| 115 |
$this->p->debug->log( 'getting object for coauthor id ' . $user_id ); |
| 116 |
} |
| 117 |
|
| 118 |
$user_obj = $coauthors_plus->get_coauthor_by( 'id', $user_id ); |
| 119 |
} |
| 120 |
|
| 121 |
return $user_obj; |
| 122 |
} |
| 123 |
|
| 124 |
public function filter_get_other_user_images( $mt_ret, $num, $size_names, $user_id, $md_pre ) { |
| 125 |
|
| 126 |
if ( 'guest-author' === get_post_type( $user_id ) ) { |
| 127 |
|
| 128 |
if ( $this->p->debug->enabled ) { |
| 129 |
|
| 130 |
$this->p->debug->mark( 'guest author / post ID ' . $user_id . ' images' ); // Begin timer. |
| 131 |
} |
| 132 |
|
| 133 |
$mt_ret = array_merge( $mt_ret, $this->p->media->get_post_images( $num, $size_names, $user_id, $md_pre ) ); |
| 134 |
|
| 135 |
if ( $this->p->debug->enabled ) { |
| 136 |
|
| 137 |
$this->p->debug->mark( 'guest author / post ID ' . $user_id . ' images' ); // End timer. |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $mt_ret; |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
* Coauthor guest user meta is saved as a custom post type. |
| 146 |
*/ |
| 147 |
public function filter_get_other_user_meta( $opts, $user_id ) { |
| 148 |
|
| 149 |
if ( 'guest-author' === get_post_type( $user_id ) ) { |
| 150 |
|
| 151 |
if ( $this->p->debug->enabled ) { |
| 152 |
|
| 153 |
$this->p->debug->mark( 'guest author / post ID ' . $user_id . ' meta' ); // Begin timer. |
| 154 |
} |
| 155 |
|
| 156 |
$mod = $this->p->post->get_mod( $user_id ); |
| 157 |
|
| 158 |
$opts = $mod[ 'obj' ]->get_options( $user_id, $md_key = false, $filter_opts = false ); // Returns an empty string if no meta found. |
| 159 |
|
| 160 |
if ( $this->p->debug->enabled ) { |
| 161 |
|
| 162 |
$this->p->debug->mark( 'guest author / post ID ' . $user_id . ' meta' ); // End timer. |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return $opts; |
| 167 |
} |
| 168 |
|
| 169 |
public function filter_get_author_meta( $value, $user_id, $field_id, $is_user ) { |
| 170 |
|
| 171 |
/* |
| 172 |
* Abort if user_id is a valid WordPress user. |
| 173 |
*/ |
| 174 |
if ( $is_user ) { |
| 175 |
|
| 176 |
return $value; |
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
* StdClass Object ( |
| 181 |
* [ID] => 2606 |
| 182 |
* [display_name] => Mr. John Doe |
| 183 |
* [first_name] => John |
| 184 |
* [last_name] => Doe |
| 185 |
* [user_login] => mr-john-doe |
| 186 |
* [user_email] => johndoe@someplace.com |
| 187 |
* [linked_account] => |
| 188 |
* [website] => http://guest_website.com |
| 189 |
* [aim] => |
| 190 |
* [yahooim] => |
| 191 |
* [jabber] => |
| 192 |
* [description] => Some Bio info for John Doe. |
| 193 |
* [user_nicename] => mr-john-doe |
| 194 |
* [type] => guest-author |
| 195 |
* ) |
| 196 |
*/ |
| 197 |
$user_obj = $this->filter_get_user_object( false, $user_id ); |
| 198 |
|
| 199 |
if ( isset( $user_obj->ID ) ) { |
| 200 |
|
| 201 |
if ( $this->p->debug->enabled ) { |
| 202 |
|
| 203 |
$this->p->debug->log( 'user_id ' . $user_id . ' coauthor object found' ); |
| 204 |
} |
| 205 |
|
| 206 |
} else return $value; |
| 207 |
|
| 208 |
switch ( $field_id ) { |
| 209 |
|
| 210 |
case 'fullname': |
| 211 |
|
| 212 |
return ( isset( $user_obj->first_name ) ? trim( $user_obj->first_name ) : '' ) . ' ' . |
| 213 |
( isset( $user_obj->last_name ) ? trim( $user_obj->last_name ) : '' ); |
| 214 |
|
| 215 |
case 'url': |
| 216 |
|
| 217 |
return isset( $user_obj->website ) ? trim( $user_obj->website ) : ''; |
| 218 |
|
| 219 |
default: |
| 220 |
|
| 221 |
return isset( $user_obj->$field_id ) ? trim( $user_obj->$field_id ) : ''; |
| 222 |
} |
| 223 |
|
| 224 |
return $value; |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* Don't check guest author custom post types (the permalink is not accessible). |
| 229 |
*/ |
| 230 |
public function filter_check_post_head( $enabled, $post_id, $post_obj ) { |
| 231 |
|
| 232 |
if ( $enabled && isset( $post_obj->post_type ) && $post_obj->post_type === 'guest-author' ) { |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
return $enabled; |
| 238 |
} |
| 239 |
|
| 240 |
/* |
| 241 |
* Guest author custom post types don't have content - return the description author meta instead. |
| 242 |
*/ |
| 243 |
public function filter_description_seed( $desc_text, $mod, $num_hashtags, $md_key ) { |
| 244 |
|
| 245 |
if ( $mod[ 'is_post' ] && 'guest-author' === $mod[ 'post_type' ] ) { |
| 246 |
|
| 247 |
$desc_text = $this->filter_get_author_meta( $desc_text, $mod[ 'id' ], 'description', false ); |
| 248 |
} |
| 249 |
|
| 250 |
return $desc_text; |
| 251 |
} |
| 252 |
|
| 253 |
public function add_contact_methods( $fields = array(), $groups = null ) { |
| 254 |
|
| 255 |
/* |
| 256 |
* Use the same check as the coauthors plugin. |
| 257 |
*/ |
| 258 |
if ( ! in_array( 'contact-info', $groups ) && 'all' !== $groups[0] ) { |
| 259 |
|
| 260 |
return $fields; |
| 261 |
} |
| 262 |
|
| 263 |
/* |
| 264 |
* Unset built-in contact fields and/or update their labels. |
| 265 |
*/ |
| 266 |
if ( ! empty( $this->p->cf[ 'wp' ][ 'cm_names' ] ) && is_array( $this->p->cf[ 'wp' ][ 'cm_names' ] ) ) { |
| 267 |
|
| 268 |
foreach ( $fields as $num => $cm ) { |
| 269 |
|
| 270 |
if ( ! isset( $cm[ 'key' ] ) || ! isset( $cm[ 'group' ] ) || $cm[ 'group' ] !== 'contact-info' ) { |
| 271 |
|
| 272 |
continue; |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* Adjust for wp / coauthors key differences. |
| 277 |
*/ |
| 278 |
switch ( $cm[ 'key' ] ) { |
| 279 |
|
| 280 |
case 'yahooim': |
| 281 |
|
| 282 |
$cm_opt = 'wp_cm_yim_'; |
| 283 |
|
| 284 |
break; |
| 285 |
|
| 286 |
default: |
| 287 |
|
| 288 |
$cm_opt = 'wp_cm_' . $cm[ 'key' ] . '_'; |
| 289 |
|
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
if ( isset( $this->p->options[ $cm_opt . 'enabled' ] ) ) { |
| 294 |
|
| 295 |
if ( ! empty( $this->p->options[ $cm_opt . 'enabled' ] ) ) { |
| 296 |
|
| 297 |
if ( ! empty( $this->p->options[ $cm_opt . 'label' ] ) ) { |
| 298 |
$fields[ $num ][ 'label' ] = $this->p->options[ $cm_opt . 'label' ]; |
| 299 |
} |
| 300 |
|
| 301 |
} else unset( $fields[ $num ] ); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/* |
| 307 |
* Loop through each social website option prefix. |
| 308 |
*/ |
| 309 |
foreach ( $this->p->cf[ 'opt' ][ 'cm_prefix' ] as $cm_id => $opt_pre ) { |
| 310 |
|
| 311 |
$cm_enabled_key = 'plugin_cm_' . $opt_pre . '_enabled'; |
| 312 |
$cm_name_key = 'plugin_cm_' . $opt_pre . '_name'; |
| 313 |
$cm_label_key = 'plugin_cm_' . $opt_pre . '_label'; |
| 314 |
|
| 315 |
if ( ! empty( $this->p->options[ $cm_enabled_key ] ) && ! empty( $this->p->options[ $cm_name_key ] ) ) { |
| 316 |
|
| 317 |
$cm_label_value = SucomUtilOptions::get_key_value( $cm_label_key, $this->p->options ); |
| 318 |
|
| 319 |
if ( ! empty( $cm_label_value ) ) { |
| 320 |
|
| 321 |
$fields[] = array( |
| 322 |
'key' => $this->p->options[ $cm_name_key ], |
| 323 |
'label' => $cm_label_value, |
| 324 |
'group' => 'contact-info', |
| 325 |
); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return $fields; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|