<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12692] sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory: Photo Directory, Flagged: Add 'flagged' post status for photos.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/12692">12692</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/12692","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>coffee2code</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-07-03 18:54:47 +0000 (Mon, 03 Jul 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Photo Directory, Flagged: Add 'flagged' post status for photos.

- Only accessible to Admins and Photo Admins.
- Registers 'flagged' post status.
- Adds 'Flagged' admin submenu link under 'Photos'.
- Adds post state indicator, if applicable.
- Allows post status to be assigned via quick edit and bulk edit.
- Adds count of user's flagged photos in 'Author' column.
- Prevents unauthorized direct access to flagged photos and their listing. 

See <a href="http://meta.trac.wordpress.org/ticket/7119">#7119</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsphotodirectoryphotodirectoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/photo-directory.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsphotodirectoryincflaggedphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/flagged.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsphotodirectoryincflaggedphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/flagged.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/flagged.php                          (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/flagged.php    2023-07-03 18:54:47 UTC (rev 12692)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,342 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Post handling customizations.
+ *
+ * @package WordPressdotorg\Photo_Directory
+ */
+
+namespace WordPressdotorg\Photo_Directory;
+
+class Flagged {
+
+       /**
+        * Initializer.
+        */
+       public static function init() {
+               // Register post statuses.
+               add_action( 'init',                                  [ __CLASS__, 'register_post_statuses' ] );
+
+               // Restrict access to the listing of flagged photos.
+               add_action( 'current_screen',                        [ __CLASS__, 'restrict_photo_listing' ] );
+
+               // Restrict access to edit a flagged post.
+               add_action( 'current_screen',                        [ __CLASS__, 'restrict_photo_editing' ] );
+
+               // Add 'Flagged' admin menu link under 'Photos'.
+               add_action( 'admin_menu',                            [ __CLASS__, 'modify_admin_menu_links' ] );
+
+               // Add post state indicator.
+               add_filter( 'display_post_states',                   [ __CLASS__, 'display_post_states' ], 10, 2 );
+
+               // Add count of user's flagged photos in author column.
+               //     Note: Priority after Admin::add_published_photos_count_to_author()
+               add_filter( 'the_author',                            [ __CLASS__, 'add_flagged_photos_count_to_author' ], 11 );
+
+               // Add support to the post edit page.
+               add_action( 'admin_footer-post.php',                 [ __CLASS__, 'output_js_for_post_edit_support' ] );
+
+               // Add support to quick edit and bulk edit.
+               add_filter( 'quick_edit_dropdown_pages_args',        [ __CLASS__, 'add_custom_status_to_quick_edit' ] );
+               add_action( 'admin_footer',                          [ __CLASS__, 'add_flagged_quick_edit_javascript' ] );
+
+               // Allow Photo metabox to be shown for flagged photos.
+               add_filter( 'wporg_photos_post_statuses_with_photo', [ __CLASS__, 'amend_with_post_status' ] );
+       }
+
+       /**
+        * Returns the flagged post status.
+        *
+        * @return string
+        */
+       public static function get_post_status() {
+               return 'flagged';
+       }
+
+       /**
+        * Returns the capability required to manage flagged photos.
+        *
+        * @return string
+        */
+       public static function get_capability() {
+               return 'manage_flagged_photos';
+       }
+
+       /**
+        * Registers post statuses.
+        */
+       public static function register_post_statuses() {
+               $is_user_allowed = current_user_can( self::get_capability() );
+
+               register_post_status( 'flagged', [
+                       'label'                     => _x( 'Flagged', 'photo status', 'wporg-photos' ),
+                       'label_count'               => _n_noop( 'Flagged <span class="count">(%s)</span>', 'Flagged <span class="count">(%s)</span>', 'wporg-photos' ),
+                       'protected'                 => true,
+                       'public'                    => false,
+                       'show_in_admin_all_list'    => $is_user_allowed,
+                       'show_in_admin_status_list' => $is_user_allowed,
+               ] );
+       }
+
+       /**
+        * Returns the number of total flagged photos.
+        *
+        * @return int The number of total flagged photos.
+        */
+       public static function count() {
+               global $wpdb;
+
+               return (int) $wpdb->get_var( $wpdb->prepare(
+                       "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status = %s",
+                       Registrations::get_post_type(),
+                       self::get_post_status()
+               ) );
+       }
+
+       /**
+        * Amends an array with the flagged post status.
+        *
+        * @param string[] $post_statuses Array of post statuses.
+        * @return string[]
+        */
+       public static function amend_with_post_status( $post_statuses ) {
+               $post_statuses[] = self::get_post_status();
+               return $post_statuses;
+       }
+
+       /**
+        * Restrict access to the listing of flagged photos.
+        *
+        * @param WP_Screen $current_screen Current WP_Screen object.
+        */
+       public static function restrict_photo_listing( $screen ) {
+               // Bail if not in admin.
+               if ( ! is_admin() ) {
+                       return;
+               }
+
+               // Bail if not listing photos.
+               if ( 'edit-' . Registrations::get_post_type() !== $screen->id ) {
+                       return;
+               }
+
+               // Bail if post status is not explicitly requested.
+               if ( empty( $_GET['post_status'] ) ) {
+                       return;
+               }
+
+               // Bail if post status isn't 'flagged'.
+               if ( self::get_post_status() !== $_GET['post_status'] ) {
+                       return;
+               }
+
+               // Bail if user has capability.
+               if ( current_user_can( self::get_capability() ) ) {
+                       return;
+               }
+
+               // Fall back to as if 'All' view was requested.
+               $_GET['post_status'] = 'all';
+       }
+
+       /**
+        * Restricts access to edit a flagged post.
+        *
+        * @param WP_Screen $current_screen Current WP_Screen object.
+        */
+       public static function restrict_photo_editing( $screen ) {
+               $post_id = $_GET['post'] ?? null;
+               if ( $post_id && $screen && Registrations::get_post_type() === $screen->id ) {
+                       $post = get_post( $post_id );
+                       if (
+                               $post
+                       &&
+                               // Post is a photo.
+                               Registrations::get_post_type() === get_post_type( $post )
+                       &&
+                               // Photo is flagged.
+                               self::get_post_status() === get_post_status( $post )
+                       &&
+                               // User can't manage flagged photos.
+                               ! current_user_can( self::get_capability() )
+                       ) {
+                               wp_die( __( 'Sorry, you are not allowed to edit this post.', 'wporg-photos' ) );
+                       }
+               }
+       }
+
+       /**
+        * Adds 'Flagged' to admin menu for photos.
+        */
+       public static function modify_admin_menu_links() {
+               $path = 'edit.php?post_type=' . Registrations::get_post_type();
+
+               $flagged_count = self::count();
+               $count_indicator = $flagged_count
+                       ? "<span class=\"update-plugins count-{$flagged_count}\"><span class=\"plugin-count\">{$flagged_count}</span></span>"
+                       : '';

+               // Add 'Flagged' link if user can read flagged photos.
+               add_submenu_page(
+                       $path,
+                       __( 'Flagged Photos', 'wporg-photos' ),
+                       /* translators: %s: Markup for the count indicator. */
+                       sprintf( __( 'Flagged %s', 'wporg-photos' ), $count_indicator ),
+                       self::get_capability(),
+                       esc_url( add_query_arg( [ 'post_status' => self::get_post_status() ], $path ) ),
+                       '',
+                       2
+               );
+       }
+
+       /**
+        * Adds 'Flagged' post state indicator when appropriate.
+        */
+       public static function display_post_states( $post_states, $post ) {
+               if ( Registrations::get_post_type() === get_post_type( $post ) && self::get_post_status() === get_post_status( $post ) ) {
+                       $post_states[] = __( 'Flagged', 'wporg-photos' );
+               }
+               return $post_states;
+       }
+
+       /**
+        * Appends the count of the published photos to author names in photo post
+        * listings.
+        *
+        * @param string $display_name The author's display name.
+        * @return string
+        */
+       public static function add_flagged_photos_count_to_author( $display_name ) {
+               global $authordata;
+
+               if ( ! is_admin() || ! Admin::should_include_photo_column() ) {
+                       return $display_name;
+               }
+
+               // Close link to contributor's listing of photos.
+               $display_name .= '</a>';
+
+               // Show number of flagged photos.
+               $flagged_count = User::count_flagged_photos( $authordata->ID );
+               $flagged_link = '';
+               if ( $flagged_count ) {
+                       if ( current_user_can( self::get_capability() ) ) {
+                               $flagged_link = add_query_arg( [
+                                       'post_type'   => Registrations::get_post_type(),
+                                       'post_status' => self::get_post_status(),
+                                       'author'      => $authordata->ID,
+                               ], 'edit.php' );
+                       }
+                       $display_name .= '<div class="user-flagged-count">'
+                               . sprintf(
+                                       /* translators: %s: Count of user's flagged photos possibly linked to listing of their flagged photos. */
+                                       _n( 'Flagged: <strong>%s</strong>', 'Flagged: <strong>%s</strong>', $flagged_count, 'wporg-photos' ),
+                                       $flagged_link ? sprintf( '<a href="%s">%d</a>', $flagged_link, $flagged_count ) : $flagged_count
+                               )
+                               . "</div>\n";
+               }
+
+               // Prevent unbalanced tag.
+               $display_name .= '<a>';
+
+               return $display_name;
+       }
+
+       /**
+        * Outputs JS to the post edit page of photos to support flagged photos.
+        */
+       public static function output_js_for_post_edit_support() {
+               $post = get_post();
+               $complete = '';
+               $label = '';
+               if ( Registrations::get_post_type() === get_post_type( $post ) ) {
+                       $post_status = self::get_post_status();
+                       $is_flagged = get_post_status( $post ) === $post_status;
+                       $selected = $is_flagged ? 'selected' : '';
+                       $label_text = __( 'Flagged', 'wporg-photos' );
+                       $label = $is_flagged ? "<span id=\"post-status-display\">{$label_text}</span>" : '';
+       
+                       echo "
+                       <script>
+                       document.addEventListener('DOMContentLoaded', function() {
+                               const select = document.querySelector('select#post_status');
+                               const option = document.createElement('option');
+                               option.value = '{$post_status}';
+                               option.innerHTML = '{$label_text}';
+                               option.selected = '{$selected}' === 'selected';
+                               select.appendChild(option);
+       
+                               const label = document.querySelector('.misc-pub-section label');
+                               label.innerHTML += '{$label}';
+
+                               if (option.selected) {
+                                       document.getElementById('post-status-display').innerHTML = '{$label_text}';
+                               }
+                       });
+                       </script>
+                       ";
+               }
+       }
+
+       /**
+        * Adds custom post statuses to the Quick Edit panel.
+        *
+        * @param array $args
+        * @return array
+        */
+       public static function add_custom_status_to_quick_edit( $args ) {
+               $args['include'] = array_merge( $args['include'], [ self::get_post_status() ] );
+               return $args;
+       }
+
+       /**
+        * Outputs the JavaScript to allow assignment of flagged status via Quick Edit and Bulk Edit.
+        */
+       public static function add_flagged_quick_edit_javascript() {
+               $current_screen = get_current_screen();
+               $post_type = Registrations::get_post_type();
+
+               if ( ( "edit-{$post_type}" !== $current_screen->id ) || ( $post_type !== $current_screen->post_type ) ) {
+                       return;
+               }
+               ?>
+               <script>
+                       document.addEventListener('DOMContentLoaded', function () {
+                               // Function to add the new status
+                               function addNewStatus(target) {
+                                       const select = target.querySelector('select[name="_status"]');
+                                       const post_status = '<?php echo self::get_post_status(); ?>';
+
+                                       if (select) {
+                                               const optionExists = Array.from(select.options).some(opt => opt.value === post_status);
+                                               if (!optionExists) {
+                                                       const newOption = new Option( "<?php _e( 'Flagged', 'wporg-photos' ); ?>", post_status);
+                                                       select.add(newOption);
+                                               }
+                                       }
+                               }
+
+                               // Function to check if a new node is the Quick Edit or Bulk Edit form
+                               function checkNewNode(target) {
+                                       if (target.classList.contains('inline-edit-<?php echo Registrations::get_post_type(); ?>') || target.id === 'bulk-edit') {
+                                               addNewStatus(target);
+                                       }
+                               }
+
+                               // MutationObserver setup
+                               const observer = new MutationObserver((mutations) => {
+                                       mutations.forEach((mutation) => {
+                                               if (mutation.addedNodes) {
+                                                       mutation.addedNodes.forEach(checkNewNode);
+                                               }
+                                       });
+                               });
+
+                               observer.observe(document.getElementById('the-list'), { childList: true, subtree: true });
+                       }, false);
+               </script>
+               <?php
+       }
+
+}
+
+add_action( 'plugins_loaded', [ __NAMESPACE__ . '\Flagged', 'init' ] );
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/flagged.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsphotodirectoryphotodirectoryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/photo-directory.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/photo-directory.php      2023-07-03 18:31:59 UTC (rev 12691)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/photo-directory.php        2023-07-03 18:54:47 UTC (rev 12692)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,6 +17,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/inc/admin.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/inc/badges.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require_once __DIR__ . '/inc/flagged.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/inc/head.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/inc/moderation.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/inc/photo.php';
</span></span></pre>
</div>
</div>

</body>
</html>