From ce8296bfc1eb94820342e602adc1e60c039e366e Mon Sep 17 00:00:00 2001 From: Rutvik Savsani Date: Thu, 15 Jan 2026 20:08:30 +0530 Subject: [PATCH] chore: initialize and add attachment filters for month and year. --- src/js/media/models/attachments.js | 56 ++++++++++++++++++++++++++++++ src/js/media/models/query.js | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/js/media/models/attachments.js b/src/js/media/models/attachments.js index 23510bd949f4c..35a854b160379 100644 --- a/src/js/media/models/attachments.js +++ b/src/js/media/models/attachments.js @@ -594,6 +594,62 @@ var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen } return status === attachment.get('status'); + }, + /** + * @static + * @param {wp.media.model.Attachment} attachment + * + * @this wp.media.model.Attachments + * + * @return {boolean} + */ + year: function( attachment ) { + var year = this.props.get('year'), + date; + + if ( _.isUndefined( year ) || false === year ) { + return true; + } + + // If uploading, we don't have the date yet, so allow it through. + if ( attachment.get('uploading') ) { + return true; + } + + date = attachment.get('date') || attachment.get('modified'); + if ( date ) { + return year === new Date( date ).getFullYear(); + } + + return false; + }, + /** + * @static + * @param {wp.media.model.Attachment} attachment + * + * @this wp.media.model.Attachments + * + * @return {boolean} + */ + monthnum: function( attachment ) { + var monthnum = this.props.get('monthnum'), + date; + + if ( _.isUndefined( monthnum ) || false === monthnum ) { + return true; + } + + // If uploading, we don't have the date yet, so allow it through. + if ( attachment.get('uploading') ) { + return true; + } + + date = attachment.get('date') || attachment.get('modified'); + if ( date ) { + return monthnum === ( new Date( date ).getMonth() + 1 ); + } + + return false; } } }); diff --git a/src/js/media/models/query.js b/src/js/media/models/query.js index b3f62018f5cd4..f238c4fa5fe2f 100644 --- a/src/js/media/models/query.js +++ b/src/js/media/models/query.js @@ -77,7 +77,7 @@ Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{ * are no filters for other properties, so observing will result in * false positives in those queries. */ - allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent', 'author' ]; + allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent', 'author', 'year', 'monthnum' ]; if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() ) { this.observe( wp.Uploader.queue ); }