(function ($) {
  if (!window.inlineEditPost || !inlineEditPost.edit) return;

  var coreEdit = inlineEditPost.edit;

  inlineEditPost.edit = function (id) {
    // Let core render the inline row
    coreEdit.apply(this, arguments);

    // Obtain numeric post ID (works for object or number)
    var postId = (typeof id === 'object') ? parseInt(this.getId(id), 10) : parseInt(id, 10);
    if (!postId) return;

    // Read per-row marker (use attr to avoid stale jQuery data cache)
    var $row    = $('#post-' + postId);
    var $marker = $row.find('.masspost-selected-sites').first();
    var payload = $marker.length ? String($marker.attr('data-selected') || '') : '';
    var selected = payload.split(',').filter(Boolean); // e.g. ['1','3']

    // Wait until the Quick Edit row AND our scroll wrapper exist, then tick boxes
var tries = 0;
(function fillWhenReady () {
  var $qe = $('#edit-' + postId); // this post's inline edit row
  if (!$qe.length) {
    if (++tries < 60) { requestAnimationFrame(fillWhenReady); }
    return;
  }

  // Scope strictly to our UI to avoid touching anything else
  var $scope = $qe.find('.masspost-inline-group .masspost-sites-scroll');
  if (!$scope.length) {
    if (++tries < 60) { requestAnimationFrame(fillWhenReady); }
    return;
  }

  // Clear then apply selection only inside our block
  var $inputs = $scope.find('input[name="share_to_site[]"]');
  if (!$inputs.length) {
    if (++tries < 60) { requestAnimationFrame(fillWhenReady); }
    return;
  }

  $inputs.prop('checked', false).each(function () {
    var val = String($(this).val());
    this.checked = (selected.indexOf(val) !== -1);
  });
})();

  };
})(jQuery);
