Source: post-settings.php

<?php if ( ! defined( 'ABSPATH' ) ) {
	die( 'Cheatin&#8217; uh?' );
}

/**
 * Per post settings: "hide the buttons" choice and share counters edition.
 * Shared by the classic editor metabox and the block editor panel (REST field `jsps_sharing`).
 *
 * @since 2.6.0
 */

if ( ! function_exists( 'jsps_can_edit_counters' ) ) {
	/**
	 * Whether the current user can edit the share counters of a post.
	 *
	 * @param  (int) $post_id The post ID.
	 * @return (boolean)
	 *
	 * @since  2.6.0
	 */
	function jsps_can_edit_counters( $post_id ) {
		/**
		 * Capability needed to edit the share counters of a post, in the "Sharing buttons" box of the editor.
		 * The user must be able to edit the post too. Return '' to disable the counters edition.
		 *
		 * @hook jsps_edit_counters_capability
		 *
		 * @since  2.6.0 First version
		 * @param  {string} $capability=edit_others_posts The capability.
		 * @return {string} The capability, or '' to disable the counters edition.
		 */
		$capability = (string) apply_filters( 'jsps_edit_counters_capability', 'edit_others_posts' );

		return '' !== $capability && current_user_can( $capability ) && current_user_can( 'edit_post', $post_id );
	}
}

if ( ! function_exists( 'jsps_get_editable_counters' ) ) {
	/**
	 * The counters shown in the editor: networks active in the settings, and the ones already counted for this post.
	 *
	 * @param  (int) $post_id The post ID.
	 * @return (array) List of `array( 'slug' => …, 'name' => …, 'count' => … )`, in the settings order.
	 *
	 * @since  2.6.0
	 */
	function jsps_get_editable_counters( $post_id ) {
		$options  = jsps_get_option();
		$counters = jsps_get_post_counters( $post_id );
		$counters = is_array( $counters ) ? $counters : array();
		$networks = jsps_get_displayable_networks(
			isset( $options['juiz_sps_networks'] ) ? $options['juiz_sps_networks'] : array(),
			isset( $options['juiz_sps_order'] ) ? $options['juiz_sps_order'] : array()
		);

		$list = array();
		foreach ( $networks as $slug => $network ) {
			$count = isset( $counters[ $slug ] ) ? (int) $counters[ $slug ] : 0;

			if ( 0 === $count && empty( $network['visible'] ) ) {
				continue;
			}

			$list[] = array(
				'slug'  => sanitize_key( $slug ),
				'name'  => isset( $network['name'] ) ? wp_strip_all_tags( $network['name'] ) : $slug,
				'count' => $count,
			);
		}

		return $list;
	}
}

if ( ! function_exists( 'jsps_apply_counters_changes' ) ) {
	/**
	 * Saves the share counters edited by a user.
	 * Only the changed numbers are written: shares counted meanwhile on other networks are kept.
	 * The caller checks the permissions.
	 *
	 * @param  (int)   $post_id   The post ID.
	 * @param  (array) $submitted Submitted counts, `network => count`.
	 * @param  (array) $original  Counts displayed when the form was loaded, `network => count`.
	 * @return (boolean) Whether something changed.
	 *
	 * @since  2.6.0
	 */
	function jsps_apply_counters_changes( $post_id, $submitted, $original ) {
		$submitted  = is_array( $submitted ) ? $submitted : array();
		$original   = is_array( $original ) ? $original : array();
		$registered = jsps_get_all_registered_networks();
		$counters   = get_post_meta( $post_id, JSPS_COUNTERS_META, true );
		$counters   = is_array( $counters ) ? $counters : array();
		$changed    = false;

		foreach ( $submitted as $slug => $value ) {
			$slug = sanitize_key( (string) $slug );

			if ( ! isset( $registered[ $slug ] ) || is_array( $value ) || ! is_numeric( $value ) ) {
				continue;
			}

			$value = max( 0, (int) $value );

			if ( isset( $original[ $slug ] ) && ! is_array( $original[ $slug ] ) && (int) $original[ $slug ] === $value ) {
				continue;
			}

			$counters[ $slug ] = $value;
			$changed           = true;
		}

		if ( $changed ) {
			jsps_save_post_counters( $post_id, $counters );
			delete_transient( 'jsps-most-shared-posts' );
		}

		return $changed;
	}
}

if ( ! function_exists( 'jsps_get_settings_post_types' ) ) {
	/**
	 * Post types showing the buttons, according to the settings.
	 *
	 * @return (array)
	 *
	 * @since  2.6.0
	 */
	function jsps_get_settings_post_types() {
		$options = jsps_get_option();
		$types   = isset( $options['juiz_sps_display_in_types'] ) && is_array( $options['juiz_sps_display_in_types'] ) ? $options['juiz_sps_display_in_types'] : array();

		return array_values( array_filter( array_diff( $types, array( 'all_lists' ) ), 'post_type_exists' ) );
	}
}

if ( ! function_exists( 'jsps_register_sharing_rest_field' ) ) {
	/**
	 * REST field `jsps_sharing` (edit context only), used by the block editor panel.
	 * Saved with the post: `{ hide: bool, counters: { network: count }, original: { network: count } }`.
	 *
	 * @return void
	 *
	 * @since  2.6.0
	 */
	function jsps_register_sharing_rest_field() {
		$counts = array(
			'type'                 => 'object',
			'additionalProperties' => array(
				'type'    => 'integer',
				'minimum' => 0,
			),
		);

		register_rest_field(
			jsps_get_settings_post_types(),
			'jsps_sharing',
			array(
				'get_callback'    => 'jsps_get_sharing_rest_field',
				'update_callback' => 'jsps_update_sharing_rest_field',
				'schema'          => array(
					'description' => __( 'Sharing buttons settings of the post.', 'juiz-social-post-sharer' ),
					'type'        => 'object',
					'context'     => array( 'edit' ),
					'properties'  => array(
						'hide'      => array( 'type' => 'boolean' ),
						'counters'  => $counts,
						'original'  => $counts,
						'networks'  => array(
							'type'     => 'array',
							'readonly' => true,
						),
						'can_edit'  => array(
							'type'     => 'boolean',
							'readonly' => true,
						),
					),
				),
			)
		);
	}
	add_action( 'rest_api_init', 'jsps_register_sharing_rest_field' );
}

if ( ! function_exists( 'jsps_get_sharing_rest_field' ) ) {
	/**
	 * Value of the `jsps_sharing` REST field.
	 *
	 * @param  (array) $post Post data prepared for the response.
	 * @return (array|null)
	 *
	 * @since  2.6.0
	 */
	function jsps_get_sharing_rest_field( $post ) {
		$post_id = isset( $post['id'] ) ? (int) $post['id'] : 0;

		if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) {
			return null;
		}

		$can_edit = jsps_can_edit_counters( $post_id );
		$networks = $can_edit ? jsps_get_editable_counters( $post_id ) : array();

		return array(
			'hide'     => 'on' === get_post_meta( $post_id, '_jsps_metabox_hide_buttons', true ),
			'counters' => (object) wp_list_pluck( $networks, 'count', 'slug' ),
			'networks' => $networks,
			'can_edit' => $can_edit,
		);
	}
}

if ( ! function_exists( 'jsps_update_sharing_rest_field' ) ) {
	/**
	 * Saves the `jsps_sharing` REST field.
	 *
	 * @param  (array)   $value The submitted value.
	 * @param  (WP_Post) $post  The post.
	 * @return (true|WP_Error)
	 *
	 * @since  2.6.0
	 */
	function jsps_update_sharing_rest_field( $value, $post ) {
		if ( ! is_array( $value ) || ! current_user_can( 'edit_post', $post->ID ) ) {
			return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to edit the sharing settings of this post.', 'juiz-social-post-sharer' ), array( 'status' => rest_authorization_required_code() ) );
		}

		if ( isset( $value['hide'] ) ) {
			update_post_meta( $post->ID, '_jsps_metabox_hide_buttons', $value['hide'] ? 'on' : 'off' );
		}

		if ( isset( $value['counters'] ) && jsps_can_edit_counters( $post->ID ) ) {
			jsps_apply_counters_changes( $post->ID, (array) $value['counters'], isset( $value['original'] ) ? (array) $value['original'] : array() );
		}

		return true;
	}
}