Changeset 43101 for branches/4.9/src/wp-admin/includes/misc.php
- Timestamp:
- 05/02/2018 02:58:12 AM (5 years ago)
- Location:
- branches/4.9
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.9
-
branches/4.9/src/wp-admin/includes/misc.php
r42831 r43101 1214 1214 } 1215 1215 } 1216 1217 /** 1218 * WP_Privacy_Policy_Content class. 1219 * TODO: move this to a new file. 1220 * 1221 * @since 4.9.6 1222 */ 1223 final class WP_Privacy_Policy_Content { 1224 1225 private static $policy_content = array(); 1226 1227 /** 1228 * Constructor 1229 * 1230 * @since 4.9.6 1231 */ 1232 private function __construct() {} 1233 1234 /** 1235 * Add privacy information to the postbox shown when editing the privacy policy. 1236 * 1237 * Intended for use from `wp_add_privacy_policy_content()`. 1238 * 1239 * $since 5.0.0 1240 * 1241 * @param string $plugin_name The plugin'as name. Will be shown in the privacy policy metabox. 1242 * @param string $policy_text The content that should appear in the site's privacy policy. 1243 */ 1244 public static function add( $plugin_name, $policy_text ) { 1245 if ( empty( $plugin_name ) || empty( $policy_text ) ) { 1246 return; 1247 } 1248 1249 $data = array( 1250 'plugin_name' => $plugin_name, 1251 'policy_text' => $policy_text, 1252 ); 1253 1254 if ( ! in_array( $data, self::$policy_content, true ) ) { 1255 self::$policy_content[] = $data; 1256 } 1257 } 1258 1259 /** 1260 * Quick check if any privacy info has changed. 1261 * 1262 * @since 4.9.6 1263 */ 1264 public static function text_change_check() { 1265 1266 $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 1267 1268 // The site doesn't have a privacy policy. 1269 if ( empty( $policy_page_id ) ) { 1270 return; 1271 } 1272 1273 if ( ! current_user_can( 'edit_post', $policy_page_id ) ) { 1274 return; 1275 } 1276 1277 // Also run when the option doesn't exist yet. 1278 if ( get_option( '_wp_privacy_text_change_check' ) === 'no-check' ) { 1279 return; 1280 } 1281 1282 $old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' ); 1283 $new = self::$policy_content; 1284 1285 // Remove the extra values added to the meta. 1286 foreach ( $old as $key => $data ) { 1287 $old[ $key ] = array( 1288 'plugin_name' => $data['plugin_name'], 1289 'policy_text' => $data['policy_text'], 1290 ); 1291 } 1292 1293 // The == operator (equal, not identical) was used intentionally. 1294 // See http://php.net/manual/en/language.operators.array.php 1295 if ( $new != $old ) { 1296 // A plugin was activated or deactivated, or some policy text has changed. 1297 // Show a notice on all screens in wp-admin. 1298 add_action( 'admin_notices', array( 'WP_Privacy_Policy_Content', 'policy_text_changed_notice' ) ); 1299 } else { 1300 // Stop checking. 1301 update_option( '_wp_privacy_text_change_check', 'no-check' ); 1302 } 1303 } 1304 1305 /** 1306 * Output an admin notice when some privacy info has changed. 1307 * 1308 * @since 4.9.6 1309 */ 1310 public static function policy_text_changed_notice() { 1311 global $post; 1312 $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 1313 1314 ?> 1315 <div class="policy-text-updated notice notice-warning is-dismissible"> 1316 <p><?php 1317 1318 _e( 'The suggested privacy policy text has changed.' ); 1319 1320 if ( empty( $post ) || $post->ID != $policy_page_id ) { 1321 ?> 1322 <a href="<?php echo get_edit_post_link( $policy_page_id ); ?>"><?php _e( 'Edit the privacy policy.' ); ?></a> 1323 <?php 1324 } 1325 1326 ?></p> 1327 </div> 1328 <?php 1329 } 1330 1331 /** 1332 * Stop checking for changed privacy info when the policy page is updated. 1333 * 1334 * @since 4.9.6 1335 * @access private 1336 */ 1337 public static function _policy_page_updated( $post_id ) { 1338 $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 1339 1340 if ( ! $policy_page_id || $policy_page_id !== (int) $post_id ) { 1341 return; 1342 } 1343 1344 // The policy page was updated. 1345 // Stop checking for text changes. 1346 update_option( '_wp_privacy_text_change_check', 'no-check' ); 1347 1348 // Remove updated|removed status. 1349 $old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' ); 1350 $done = array(); 1351 $update_cache = false; 1352 1353 foreach ( $old as $old_key => $old_data ) { 1354 if ( ! empty( $old_data['removed'] ) ) { 1355 // Remove the old policy text. 1356 $update_cache = true; 1357 continue; 1358 } 1359 1360 if ( ! empty( $old_data['updated'] ) ) { 1361 // 'updated' is now 'added'. 1362 $done[] = array( 1363 'plugin_name' => $old_data['plugin_name'], 1364 'policy_text' => $old_data['policy_text'], 1365 'added' => $old_data['updated'], 1366 ); 1367 $update_cache = true; 1368 } else { 1369 $done[] = $old_data; 1370 } 1371 } 1372 1373 if ( $update_cache ) { 1374 delete_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' ); 1375 // Update the cache. 1376 foreach ( $done as $data ) { 1377 add_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content', $data ); 1378 } 1379 } 1380 } 1381 1382 /** 1383 * Check for updated, added or removed privacy policy information from plugins. 1384 * 1385 * Caches the current info in post_meta of the policy page. 1386 * 1387 * @since 4.9.6 1388 * 1389 * @return array The privacy policy text/informtion added by core and plugins. 1390 */ 1391 public static function get_suggested_policy_text() { 1392 $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 1393 $new = self::$policy_content; 1394 $old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' ); 1395 $checked = array(); 1396 $time = time(); 1397 $update_cache = false; 1398 1399 // Check for no-changes and updates. 1400 foreach ( $new as $new_key => $new_data ) { 1401 foreach ( $old as $old_key => $old_data ) { 1402 $found = false; 1403 1404 if ( $new_data['policy_text'] === $old_data['policy_text'] ) { 1405 // Use the new plugin name in case it was changed, translated, etc. 1406 if ( $old_data['plugin_name'] !== $new_data['plugin_name'] ) { 1407 $old_data['plugin_name'] = $new_data['plugin_name']; 1408 $update_cache = true; 1409 } 1410 1411 // A plugin was re-activated. 1412 if ( ! empty( $old_data['removed'] ) ) { 1413 unset( $old_data['removed'] ); 1414 $old_data['added'] = $time; 1415 $update_cache = true; 1416 } 1417 1418 $checked[] = $old_data; 1419 $found = true; 1420 } elseif ( $new_data['plugin_name'] === $old_data['plugin_name'] ) { 1421 // The info for the policy was updated. 1422 $checked[] = array( 1423 'plugin_name' => $new_data['plugin_name'], 1424 'policy_text' => $new_data['policy_text'], 1425 'updated' => $time, 1426 ); 1427 $found = $update_cache = true; 1428 } 1429 1430 if ( $found ) { 1431 unset( $new[ $new_key ], $old[ $old_key ] ); 1432 continue 2; 1433 } 1434 } 1435 } 1436 1437 if ( ! empty( $new ) ) { 1438 // A plugin was activated. 1439 foreach ( $new as $new_data ) { 1440 if ( ! empty( $new_data['plugin_name'] ) && ! empty( $new_data['policy_text'] ) ) { 1441 $new_data['added'] = $time; 1442 array_unshift( $checked, $new_data ); 1443 } 1444 } 1445 $update_cache = true; 1446 } 1447 1448 if ( ! empty( $old ) ) { 1449 // A plugin was deactivated. 1450 foreach ( $old as $old_data ) { 1451 if ( ! empty( $old_data['plugin_name'] ) && ! empty( $old_data['policy_text'] ) ) { 1452 $data = array( 1453 'plugin_name' => $old_data['plugin_name'], 1454 'policy_text' => $old_data['policy_text'], 1455 'removed' => $time, 1456 ); 1457 array_unshift( $checked, $data ); 1458 } 1459 } 1460 $update_cache = true; 1461 } 1462 1463 if ( $update_cache ) { 1464 delete_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' ); 1465 // Update the cache. 1466 foreach ( $checked as $data ) { 1467 add_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content', $data ); 1468 } 1469 } 1470 1471 // Stop checking for changes after the postbox has been loaded. 1472 // TODO make this user removable? 1473 if ( get_option( '_wp_privacy_text_change_check' ) !== 'no-check' ) { 1474 update_option( '_wp_privacy_text_change_check', 'no-check' ); 1475 } 1476 1477 return $checked; 1478 } 1479 1480 /** 1481 * Output the postbox when editing the privacy policy page 1482 * 1483 * @since 4.9.6 1484 * 1485 * @param $post WP_Post The currently edited post. 1486 */ 1487 public static function privacy_policy_postbox( $post ) { 1488 if ( ! ( $post instanceof WP_Post ) ) { 1489 return; 1490 } 1491 1492 $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 1493 1494 if ( ! $policy_page_id || $policy_page_id != $post->ID ) { 1495 return; 1496 } 1497 1498 $content_array = self::get_suggested_policy_text(); 1499 1500 $content = ''; 1501 $date_format = get_option( 'date_format' ); 1502 $copy = __( 'Copy' ); 1503 1504 foreach ( $content_array as $section ) { 1505 $class = $meta = ''; 1506 1507 if ( ! empty( $section['removed'] ) ) { 1508 $class = ' text-removed'; 1509 $date = date_i18n( $date_format, $section['removed'] ); 1510 $meta = sprintf( __( 'Policy text removed %s.' ), $date ); 1511 } elseif ( ! empty( $section['updated'] ) ) { 1512 $class = ' text-updated'; 1513 $date = date_i18n( $date_format, $section['updated'] ); 1514 $meta = sprintf( __( 'Policy text last updated %s.' ), $date ); 1515 } elseif ( ! empty( $section['added'] ) ) { 1516 $class = ' text-added'; 1517 $date = date_i18n( $date_format, $section['added'] ); 1518 $meta = sprintf( __( 'Policy text added %s.' ), $date ); 1519 } 1520 1521 $content .= '<div class="privacy-text-section' . $class . '">'; 1522 $content .= '<h3>' . $section['plugin_name'] . '</h3>'; 1523 $content .= '<button type="button" class="privacy-text-copy-button button">'; 1524 $content .= $copy; 1525 $content .= '<span class="screen-reader-text">' . sprintf( __( 'Copy suggested policy text from %s.' ), $section['plugin_name'] ) . '</span>'; 1526 $content .= '</button>'; 1527 1528 if ( ! empty( $meta ) ) { 1529 $content .= '<span class="privacy-text-meta">' . $meta . '</span>'; 1530 } 1531 1532 $content .= '<div class="policy-text">' . $section['policy_text'] . '</div>'; 1533 $content .= "</div>\n"; 1534 } 1535 1536 ?> 1537 <div id="privacy-text-box" class="privacy-text-box postbox <?php echo postbox_classes( 'privacy-text-box', 'page' ); ?>"> 1538 <button type="button" class="handlediv" aria-expanded="true"> 1539 <span class="screen-reader-text"><?php _e( 'Toggle panel: Suggested privacy policy text' ); ?></span> 1540 <span class="toggle-indicator" aria-hidden="true"></span> 1541 </button> 1542 <div class="privacy-text-box-head hndle"> 1543 <h2><?php _e( 'This suggested privacy policy text comes from plugins you have installed.' ); ?></h2> 1544 <p> 1545 <?php _e( 'We suggest reviewing this text then copying and pasting it into your privacy policy page.' ); ?> 1546 <?php _e( 'Please remember you are responsible for the policies you choose to adopt, so review the content and make any necessary edits.' ); ?> 1547 </p> 1548 </div> 1549 1550 <div class="privacy-text-box-body"> 1551 <?php echo $content; ?> 1552 </div> 1553 </div> 1554 <?php 1555 } 1556 1557 /** 1558 * Return the default suggested privacy policy content. 1559 * 1560 * @since 4.9.6 1561 * 1562 * @return string The defauld policy content. 1563 */ 1564 public static function get_default_content() { 1565 $content = '<p>' . __( 'Lorem ipsum dolor sit amet consectetuer id elit enim neque est. Sodales tincidunt Nulla leo penatibus Vestibulum adipiscing est cursus Nam Vestibulum. Orci Vivamus mollis eget pretium dictumst Donec Integer auctor sociis rutrum. Mauris felis Donec neque cursus tellus odio adipiscing netus elit Donec. Vestibulum Cras ligula vitae pretium Curabitur eros Nam Lorem eros non. Sed id mauris justo tristique orci neque eleifend lacus lorem.' ) . "</p>\n"; 1566 $content .= '<p>' . __( 'Sed consequat Nullam et vel platea semper id mauris Nam eget. Sem neque a amet eu ipsum id dignissim neque eu pulvinar. Mauris nulla egestas et laoreet penatibus ipsum lobortis convallis congue libero. Tortor nibh pellentesque tellus odio Morbi cursus eros tincidunt tincidunt sociis. Egestas at In Donec mi dignissim Nam rutrum felis metus Maecenas. Sed tellus consectetuer.' ) . "</p>\n"; 1567 $content .= '<p>' . __( 'Justo orci pulvinar mauris tincidunt sed Pellentesque dis sapien tempor ligula. Dolor laoreet fames eros accumsan Integer feugiat nec augue Phasellus rutrum. Id Sed facilisi elit mus nulla at dapibus ut enim sociis. Fringilla ridiculus dui justo eu Maecenas ipsum ut aliquet magna non. Id magna adipiscing Vestibulum Curabitur vel pretium ac justo platea neque. Maecenas Donec Quisque urna interdum.' ) . "</p>\n"; 1568 $content .= '<p>' . __( 'Tellus sagittis leo adipiscing ante facilisis Aliquam tellus at at elit. Ut dignissim tempus eu Fusce Vestibulum at eros ante dis tempus. Sed libero orci at id ut pretium metus adipiscing justo malesuada. In tempus vitae commodo libero In neque sagittis turpis In In. Eleifend elit dis ac eros urna auctor semper quis odio pretium. Ut Aenean cursus.' ) . "</p>\n"; 1569 1570 /** 1571 * Filters the default content suggested for inclusion in a privacy policy. 1572 * 1573 * @since 4.9.6 1574 * 1575 * @param $content string The defauld policy content. 1576 */ 1577 return apply_filters( 'wp_get_default_privcy_policy_content', $content ); 1578 } 1579 1580 /** 1581 * Add the suggested privacy policy text to the policy postbox. 1582 * 1583 * @since 4.9.6 1584 */ 1585 public static function add_suggested_content() { 1586 $content = self::get_default_content(); 1587 wp_add_privacy_policy_content( 'WordPress', $content ); 1588 } 1589 }
Note: See TracChangeset
for help on using the changeset viewer.