| 1 |
<?php |
| 2 |
require_once(dirname(__FILE__) . '/admin-ui.php'); |
| 3 |
|
| 4 |
class FeedWordPressAuthorsPage extends FeedWordPressAdminPage { |
| 5 |
var $authorlist = NULL; |
| 6 |
var $rule_count = 0; |
| 7 |
|
| 8 |
function FeedWordPressAuthorsPage ($link) { |
| 9 |
FeedWordPressAdminPage::FeedWordPressAdminPage('feedwordpressauthors', $link); |
| 10 |
$this->refresh_author_list(); |
| 11 |
$this->dispatch = 'feedwordpress_author_settings'; |
| 12 |
$this->filename = __FILE__; |
| 13 |
} |
| 14 |
|
| 15 |
function refresh_author_list () { |
| 16 |
$this->authorlist = fwp_author_list(); |
| 17 |
} |
| 18 |
|
| 19 |
/*static*/ function syndicated_authors_box ($page, $box = NULL) { |
| 20 |
$link = $page->link; |
| 21 |
$unfamiliar = array ('create' => '','default' => '','filter' => ''); |
| 22 |
|
| 23 |
if ($page->for_feed_settings()) : |
| 24 |
$key = $link->setting('unfamiliar author', NULL, 'site-default'); |
| 25 |
$unfamiliar['site-default'] = ''; |
| 26 |
else : |
| 27 |
$key = FeedWordPress::on_unfamiliar('author'); |
| 28 |
endif; |
| 29 |
$unfamiliar[$key] = ' selected="selected"'; |
| 30 |
|
| 31 |
$match_author_by_email = !('yes' == get_option("feedwordpress_do_not_match_author_by_email")); |
| 32 |
$null_emails = FeedWordPress::null_email_set(); |
| 33 |
|
| 34 |
// Hey ho, let's go... |
| 35 |
?> |
| 36 |
<table class="form-table"> |
| 37 |
<tbody> |
| 38 |
<tr> |
| 39 |
<th>New authors</th> |
| 40 |
<td><span>Authors who haven't been syndicated before</span> |
| 41 |
<select style="max-width: 27.0em" id="unfamiliar-author" name="unfamiliar_author" onchange="contextual_appearance('unfamiliar-author', 'unfamiliar-author-newuser', 'unfamiliar-author-default', 'newuser', 'inline');"> |
| 42 |
<?php if ($page->for_feed_settings()) : ?> |
| 43 |
<option value="site-default"<?php print $unfamiliar['site-default']; ?>>are handled according to the default for all feeds</option> |
| 44 |
<?php endif; ?> |
| 45 |
<option value="create"<?php print $unfamiliar['create']; ?>>will have a new author account created for them</option> |
| 46 |
<?php foreach ($page->authorlist as $author_id => $author_name) : ?> |
| 47 |
<option value="<?php echo $author_id; ?>"<?php print (isset($unfamiliar[$author_id]) ? $unfamiliar[$author_id] : ''); ?>>will have their posts attributed to <?php echo $author_name; ?></option> |
| 48 |
<?php endforeach; ?> |
| 49 |
<option value="newuser">will have their posts attributed to a new user...</option> |
| 50 |
<option value="filter"<?php print $unfamiliar['filter'] ?>>get filtered out</option> |
| 51 |
</select> |
| 52 |
|
| 53 |
<span id="unfamiliar-author-newuser">named <input type="text" name="unfamiliar_author_newuser" value="" /></span></p> |
| 54 |
</td> |
| 55 |
</tr> |
| 56 |
|
| 57 |
<?php |
| 58 |
if ($page->for_feed_settings()) : |
| 59 |
?> |
| 60 |
<tr><th>Syndicated authors</th> |
| 61 |
<td>For attributing posts by specific authors. Blank out a name to delete the rule. Fill in a new name at the bottom to create a new rule.</p> |
| 62 |
<table style="width: 100%"> |
| 63 |
<?php |
| 64 |
if (isset($link->settings['map authors'])) : |
| 65 |
?> |
| 66 |
<?php |
| 67 |
$page->rule_count=0; |
| 68 |
foreach ($link->settings['map authors'] as $author_rules) : |
| 69 |
foreach ($author_rules as $author_name => $author_action) : |
| 70 |
$page->rule_count++; |
| 71 |
?> |
| 72 |
<tr> |
| 73 |
<th style="text-align: left; width: 15.0em">Posts by <input type="text" name="author_rules_name[]" value="<?php echo htmlspecialchars($author_name); ?>" size="11" /></th> |
| 74 |
<td> |
| 75 |
<select id="author-rules-<?php echo $page->rule_count; ?>" name="author_rules_action[]" onchange="contextual_appearance('author-rules-<?php echo $page->rule_count; ?>', 'author-rules-<?php echo $page->rule_count; ?>-newuser', 'author-rules-<?php echo $page->rule_count; ?>-default', 'newuser', 'inline');"> |
| 76 |
<?php foreach ($page->authorlist as $local_author_id => $local_author_name) : ?> |
| 77 |
<option value="<?php echo $local_author_id; ?>"<?php if ($local_author_id==$author_action) : echo ' selected="selected"'; endif; ?>>are assigned to <?php echo $local_author_name; ?></option> |
| 78 |
<?php endforeach; ?> |
| 79 |
<option value="newuser">will be assigned to a new user...</option> |
| 80 |
<option value="filter"<?php if ('filter'==$author_action) : echo ' selected="selected"'; endif; ?>>get filtered out</option> |
| 81 |
</select> |
| 82 |
|
| 83 |
<span id="author-rules-<?php echo $page->rule_count; ?>-newuser">named <input type="text" name="author_rules_newuser[]" value="" /></span> |
| 84 |
</td> |
| 85 |
</tr> |
| 86 |
<?php endforeach; |
| 87 |
endforeach; |
| 88 |
endif; |
| 89 |
?> |
| 90 |
|
| 91 |
<tr> |
| 92 |
<th style="text-align: left; width: 15.0em">Posts by <input type="text" name="add_author_rule_name" size="11" /></th> |
| 93 |
<td> |
| 94 |
<select id="add-author-rule" name="add_author_rule_action" onchange="contextual_appearance('add-author-rule', 'add-author-rule-newuser', 'add-author-rule-default', 'newuser', 'inline');"> |
| 95 |
<?php foreach ($page->authorlist as $author_id => $author_name) : ?> |
| 96 |
<option value="<?php echo $author_id; ?>">are assigned to <?php echo $author_name; ?></option> |
| 97 |
<?php endforeach; ?> |
| 98 |
<option value="newuser">will be assigned to a new user...</option> |
| 99 |
<option value="filter">get filtered out</option> |
| 100 |
</select> |
| 101 |
|
| 102 |
<span id="add-author-rule-newuser">named <input type="text" name="add_author_rule_newuser" value="" /></span> |
| 103 |
</td> |
| 104 |
</tr> |
| 105 |
</table> |
| 106 |
</td> |
| 107 |
</tr> |
| 108 |
<?php endif; ?> |
| 109 |
|
| 110 |
<?php if ($page->for_default_settings()) : ?> |
| 111 |
<tr> |
| 112 |
<th scope="row">Matching Authors</th> |
| 113 |
<td><ul style="list-style: none; margin: 0; padding: 0;"> |
| 114 |
<li><div><label><input id="match-author-by-email" type="checkbox" name="match_author_by_email" value="yes" <?php if ($match_author_by_email) : ?>checked="checked" <?php endif; ?>onchange="contextual_appearance('match-author-by-email', 'unless-null-email', null, 'yes', 'block', /*checkbox=*/ true);" /> Treat syndicated authors with the same e-mail address as the same author.</label></div> |
| 115 |
<div id="unless-null-email"> |
| 116 |
<p>Unless the e-mail address is one of the following anonymous e-mail addresses:</p> |
| 117 |
<textarea name="null_emails" rows="3" style="width: 100%"> |
| 118 |
<?php print implode("\n", $null_emails); ?> |
| 119 |
</textarea> |
| 120 |
</div></li> |
| 121 |
</ul></td> |
| 122 |
</tr> |
| 123 |
<?php endif; ?> |
| 124 |
</tbody> |
| 125 |
</table> |
| 126 |
<?php |
| 127 |
} /* FeedWordPressAuthorsPage::syndicated_authors_box () */ |
| 128 |
|
| 129 |
/*static*/ function fix_authors_box ($page, $box = NULL) { |
| 130 |
?> |
| 131 |
<table class="form-table"> |
| 132 |
<tbody> |
| 133 |
<tr> |
| 134 |
<th scope="row">Fixing mis-matched authors:</th> |
| 135 |
<td><p style="margin: 0.5em 0px">Take all the posts from this feed attributed to |
| 136 |
<select name="fix_mismatch_from"> |
| 137 |
<?php foreach ($page->authorlist as $author_id => $author_name) : ?> |
| 138 |
<option value="<?php echo $author_id; ?>"><?php echo $author_name; ?></option> |
| 139 |
<?php endforeach; ?> |
| 140 |
</select> |
| 141 |
and instead |
| 142 |
<select id="fix-mismatch-to" name="fix_mismatch_to" onchange="contextual_appearance('fix-mismatch-to', 'fix-mismatch-to-newuser', null, 'newuser', 'inline');"> |
| 143 |
<?php foreach ($page->authorlist as $author_id => $author_name) : ?> |
| 144 |
<option value="<?php echo $author_id; ?>">re-assign them to <?php echo $author_name; ?></option> |
| 145 |
<?php endforeach; ?> |
| 146 |
<option value="newuser">re-assign them to a new user...</option> |
| 147 |
<option value="filter">delete them</option> |
| 148 |
</select> |
| 149 |
|
| 150 |
<span id="fix-mismatch-to-newuser">named <input type="text" name="fix_mismatch_to_newuser" value="" /></span> |
| 151 |
<input type="submit" class="button" name="fix_mismatch" value="Fix it!" /> |
| 152 |
</td> |
| 153 |
</tr> |
| 154 |
</tbody> |
| 155 |
</table> |
| 156 |
<?php |
| 157 |
} /* FeedWordPressAuthorsPage::fix_authors_box () */ |
| 158 |
} /* class FeedWordPressAuthorsPage */ |
| 159 |
|
| 160 |
function fwp_authors_page () { |
| 161 |
global $wpdb, $wp_db_version; |
| 162 |
|
| 163 |
if (FeedWordPress::needs_upgrade()) : |
| 164 |
fwp_upgrade_page(); |
| 165 |
return; |
| 166 |
endif; |
| 167 |
|
| 168 |
FeedWordPressCompatibility::validate_http_request(/*action=*/ 'feedwordpress_author_settings', /*capability=*/ 'manage_links'); |
| 169 |
|
| 170 |
$link = FeedWordPressAdminPage::submitted_link(); |
| 171 |
$authorsPage = new FeedWordPressAuthorsPage($link); |
| 172 |
|
| 173 |
$mesg = null; |
| 174 |
|
| 175 |
if (isset($GLOBALS['fwp_post']['fix_mismatch'])) : |
| 176 |
if ('newuser'==$GLOBALS['fwp_post']['fix_mismatch_to']) : |
| 177 |
$newuser_name = trim($GLOBALS['fwp_post']['fix_mismatch_to_newuser']); |
| 178 |
$fix_mismatch_to_id = fwp_insert_new_user($newuser_name); |
| 179 |
else : |
| 180 |
$fix_mismatch_to_id = $GLOBALS['fwp_post']['fix_mismatch_to']; |
| 181 |
endif; |
| 182 |
$fix_mismatch_from_id = (int) $GLOBALS['fwp_post']['fix_mismatch_from']; |
| 183 |
if (is_numeric($fix_mismatch_from_id)) : |
| 184 |
// Make a list of all the items by this author syndicated from this feed... |
| 185 |
$post_ids = $wpdb->get_col(" |
| 186 |
SELECT {$wpdb->posts}.id |
| 187 |
FROM {$wpdb->posts}, {$wpdb->postmeta} |
| 188 |
WHERE ({$wpdb->posts}.id = {$wpdb->postmeta}.post_id) |
| 189 |
AND {$wpdb->postmeta}.meta_key = 'syndication_feed_id' |
| 190 |
AND {$wpdb->postmeta}.meta_value = '{$link->id}' |
| 191 |
AND {$wpdb->posts}.post_author = '{$fix_mismatch_from_id}' |
| 192 |
"); |
| 193 |
|
| 194 |
if (count($post_ids) > 0) : |
| 195 |
// Re-assign them all to the correct author |
| 196 |
if (is_numeric($fix_mismatch_to_id)) : // re-assign to a particular user |
| 197 |
$post_set = "(".implode(",", $post_ids).")"; |
| 198 |
|
| 199 |
// Getting the revisions too, if there are any |
| 200 |
if (fwp_test_wp_version(FWP_SCHEMA_26)) : |
| 201 |
$parent_in_clause = "OR {$wpdb->posts}.post_parent IN $post_set"; |
| 202 |
else : |
| 203 |
$parent_in_clause = ''; |
| 204 |
endif; |
| 205 |
|
| 206 |
$wpdb->query(" |
| 207 |
UPDATE {$wpdb->posts} |
| 208 |
SET post_author='{$fix_mismatch_to_id}' |
| 209 |
WHERE ({$wpdb->posts}.id IN $post_set |
| 210 |
$parent_in_clause) |
| 211 |
"); |
| 212 |
$mesg = "Re-assigned ".count($post_ids)." post".((count($post_ids)==1)?'':'s')."."; |
| 213 |
|
| 214 |
// ... and kill them all |
| 215 |
elseif ($fix_mismatch_to_id=='filter') : |
| 216 |
foreach ($post_ids as $post_id) : |
| 217 |
wp_delete_post($post_id); |
| 218 |
endforeach; |
| 219 |
$mesg = "Deleted ".count($post_ids)." post".((count($post_ids)==1)?'':'s')."."; |
| 220 |
endif; |
| 221 |
else : |
| 222 |
$mesg = "Couldn't find any posts that matched your criteria."; |
| 223 |
endif; |
| 224 |
endif; |
| 225 |
$updated_link = false; |
| 226 |
elseif (isset($GLOBALS['fwp_post']['save'])) : |
| 227 |
if (is_object($link) and $link->found()) : |
| 228 |
$alter = array (); |
| 229 |
|
| 230 |
// Unfamiliar author rule |
| 231 |
if (isset($GLOBALS['fwp_post']["unfamiliar_author"])) : |
| 232 |
if ('site-default'==$GLOBALS['fwp_post']["unfamiliar_author"]) : |
| 233 |
unset($link->settings["unfamiliar author"]); |
| 234 |
elseif ('newuser'==$GLOBALS['fwp_post']["unfamiliar_author"]) : |
| 235 |
$newuser_name = trim($GLOBALS['fwp_post']["unfamiliar_author_newuser"]); |
| 236 |
$link->map_name_to_new_user(/*name=*/ NULL, $newuser_name); |
| 237 |
else : |
| 238 |
$link->settings["unfamiliar author"] = $GLOBALS['fwp_post']["unfamiliar_author"]; |
| 239 |
endif; |
| 240 |
endif; |
| 241 |
|
| 242 |
// Handle author mapping rules |
| 243 |
if (isset($GLOBALS['fwp_post']['author_rules_name']) and isset($GLOBALS['fwp_post']['author_rules_action'])) : |
| 244 |
unset($link->settings['map authors']); |
| 245 |
foreach ($GLOBALS['fwp_post']['author_rules_name'] as $key => $name) : |
| 246 |
// Normalize for case and whitespace |
| 247 |
$name = strtolower(trim($name)); |
| 248 |
$author_action = strtolower(trim($GLOBALS['fwp_post']['author_rules_action'][$key])); |
| 249 |
|
| 250 |
if (strlen($name) > 0) : |
| 251 |
if ('newuser' == $author_action) : |
| 252 |
$newuser_name = trim($GLOBALS['fwp_post']['author_rules_newuser'][$key]); |
| 253 |
$link->map_name_to_new_user($name, $newuser_name); |
| 254 |
else : |
| 255 |
$link->settings['map authors']['name'][$name] = $author_action; |
| 256 |
endif; |
| 257 |
endif; |
| 258 |
endforeach; |
| 259 |
endif; |
| 260 |
|
| 261 |
if (isset($GLOBALS['fwp_post']['add_author_rule_name']) and isset($GLOBALS['fwp_post']['add_author_rule_action'])) : |
| 262 |
$name = strtolower(trim($GLOBALS['fwp_post']['add_author_rule_name'])); |
| 263 |
$author_action = strtolower(trim($GLOBALS['fwp_post']['add_author_rule_action'])); |
| 264 |
if (strlen($name) > 0) : |
| 265 |
if ('newuser' == $author_action) : |
| 266 |
$newuser_name = trim($GLOBALS['fwp_post']['add_author_rule_newuser']); |
| 267 |
$link->map_name_to_new_user($name, $newuser_name); |
| 268 |
else : |
| 269 |
$link->settings['map authors']['name'][$name] = $author_action; |
| 270 |
endif; |
| 271 |
endif; |
| 272 |
endif; |
| 273 |
|
| 274 |
// Save settings |
| 275 |
$link->save_settings(/*reload=*/ true); |
| 276 |
$updated_link = true; |
| 277 |
|
| 278 |
// Reset, reload |
| 279 |
$link_id = $link->id; |
| 280 |
unset($link); |
| 281 |
$link = new SyndicatedLink($link_id); |
| 282 |
else : |
| 283 |
if ('newuser'==$GLOBALS['fwp_post']['unfamiliar_author']) : |
| 284 |
$newuser_name = trim($GLOBALS['fwp_post']['unfamiliar_author_newuser']); |
| 285 |
$newuser_id = fwp_insert_new_user($newuser_name); |
| 286 |
if (is_numeric($newuser_id)) : |
| 287 |
update_option('feedwordpress_unfamiliar_author', $newuser_id); |
| 288 |
else : |
| 289 |
// TODO: Add some error detection and reporting |
| 290 |
endif; |
| 291 |
else : |
| 292 |
update_option('feedwordpress_unfamiliar_author', $GLOBALS['fwp_post']['unfamiliar_author']); |
| 293 |
endif; |
| 294 |
|
| 295 |
if (isset($GLOBALS['fwp_post']['match_author_by_email']) and $GLOBALS['fwp_post']['match_author_by_email']=='yes') : |
| 296 |
update_option('feedwordpress_do_not_match_author_by_email', 'no'); |
| 297 |
else : |
| 298 |
update_option('feedwordpress_do_not_match_author_by_email', 'yes'); |
| 299 |
endif; |
| 300 |
|
| 301 |
if (isset($GLOBALS['fwp_post']['null_emails'])) : |
| 302 |
update_option('feedwordpress_null_email_set', $GLOBALS['fwp_post']['null_emails']); |
| 303 |
endif; |
| 304 |
|
| 305 |
$updated_link = true; |
| 306 |
endif; |
| 307 |
|
| 308 |
do_action('feedwordpress_admin_page_authors_save', $GLOBALS['fwp_post'], $authorsPage); |
| 309 |
$authorsPage->refresh_author_list(); |
| 310 |
else : |
| 311 |
$updated_link = false; |
| 312 |
endif; |
| 313 |
|
| 314 |
//////////////////////////////////////////////// |
| 315 |
// Prepare settings page /////////////////////// |
| 316 |
//////////////////////////////////////////////// |
| 317 |
|
| 318 |
if ($updated_link) : |
| 319 |
?> |
| 320 |
<div class="updated"><p>Syndicated author settings updated.</p></div> |
| 321 |
<?php elseif (!is_null($mesg)) : ?> |
| 322 |
<div class="updated"><p><?php print esc_html($mesg); ?></p></div> |
| 323 |
<?php endif; |
| 324 |
|
| 325 |
if (function_exists('add_meta_box')) : |
| 326 |
add_action( |
| 327 |
FeedWordPressCompatibility::bottom_script_hook(__FILE__), |
| 328 |
/*callback=*/ array($authorsPage, 'fix_toggles'), |
| 329 |
/*priority=*/ 10000 |
| 330 |
); |
| 331 |
FeedWordPressSettingsUI::ajax_nonce_fields(); |
| 332 |
endif; |
| 333 |
|
| 334 |
$authorsPage->open_sheet('Syndicated Author'); |
| 335 |
?> |
| 336 |
<div id="post-body"> |
| 337 |
<?php |
| 338 |
//////////////////////////////////////////////// |
| 339 |
// Display settings boxes ////////////////////// |
| 340 |
//////////////////////////////////////////////// |
| 341 |
|
| 342 |
$boxes_by_methods = array( |
| 343 |
'syndicated_authors_box' => __('Syndicated Authors'), |
| 344 |
'fix_authors_box' => __('Reassign Authors'), |
| 345 |
); |
| 346 |
if ($authorsPage->for_default_settings()) : |
| 347 |
unset($boxes_by_methods['fix_authors_box']); |
| 348 |
endif; |
| 349 |
|
| 350 |
foreach ($boxes_by_methods as $method => $row) : |
| 351 |
if (is_array($row)) : |
| 352 |
$id = $row['id']; |
| 353 |
$title = $row['title']; |
| 354 |
else : |
| 355 |
$id = 'feedwordpress_'.$method; |
| 356 |
$title = $row; |
| 357 |
endif; |
| 358 |
|
| 359 |
fwp_add_meta_box( |
| 360 |
/*id=*/ $id, |
| 361 |
/*title=*/ $title, |
| 362 |
/*callback=*/ array('FeedWordPressAuthorsPage', $method), |
| 363 |
/*page=*/ $authorsPage->meta_box_context(), |
| 364 |
/*context=*/ $authorsPage->meta_box_context() |
| 365 |
); |
| 366 |
endforeach; |
| 367 |
do_action('feedwordpress_admin_page_authors_meta_boxes', $authorsPage); |
| 368 |
?> |
| 369 |
<div class="metabox-holder"> |
| 370 |
<?php |
| 371 |
fwp_do_meta_boxes($authorsPage->meta_box_context(), $authorsPage->meta_box_context(), $authorsPage); |
| 372 |
?> |
| 373 |
</div> <!-- class="metabox-holder" --> |
| 374 |
</div> <!-- id="post-body" --> |
| 375 |
<?php $authorsPage->close_sheet(); ?> |
| 376 |
|
| 377 |
<script type="text/javascript"> |
| 378 |
contextual_appearance('unfamiliar-author', 'unfamiliar-author-newuser', 'unfamiliar-author-default', 'newuser', 'inline'); |
| 379 |
<?php if (is_object($link) and $link->found()) : ?> |
| 380 |
<?php for ($j=1; $j<=$authorsPage->rule_count; $j++) : ?> |
| 381 |
contextual_appearance('author-rules-<?php echo $j; ?>', 'author-rules-<?php echo $j; ?>-newuser', 'author-rules-<?php echo $j; ?>-default', 'newuser', 'inline'); |
| 382 |
<?php endfor; ?> |
| 383 |
contextual_appearance('add-author-rule', 'add-author-rule-newuser', 'add-author-rule-default', 'newuser', 'inline'); |
| 384 |
contextual_appearance('fix-mismatch-to', 'fix-mismatch-to-newuser', null, 'newuser', 'inline'); |
| 385 |
<?php else : ?> |
| 386 |
contextual_appearance('match-author-by-email', 'unless-null-email', null, 'yes', 'block', /*checkbox=*/ true); |
| 387 |
<?php endif; ?> |
| 388 |
</script> |
| 389 |
<?php |
| 390 |
} /* function fwp_authors_page () */ |
| 391 |
|
| 392 |
fwp_authors_page(); |
| 393 |
|
| 394 |
|