t returning it. Default true.
* @return bool|string|void Void if no data exists, extra scripts if `$echo` is true,
* true otherwise.
*/
public function print_scripts_l10n( $handle, $echo = true ) {
_deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' );
return $this->print_extra_script( $handle, $echo );
}
/**
* Prints extra scripts of a registered script.
*
* @since 3.3.0
*
* @param string $handle The script's registered handle.
* @param bool $echo Optional. Whether to echo the extra script
* instead of just returning it. Default true.
* @return bool|string|void Void if no data exists, extra scripts if `$echo` is true,
* true otherwise.
*/
public function print_extra_script( $handle, $echo = true ) {
$output = $this->get_data( $handle, 'data' );
if ( ! $output ) {
return;
}
if ( ! $echo ) {
return $output;
}
printf( "\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
}
return $output;
}
/**
* Localizes a script, only if the script has already been added.
*
* @since 2.1.0
*
* @param string $handle Name of the script to attach data to.
* @param string $object_name Name of the variable that will contain the data.
* @param array $l10n Array of data to localize.
* @return bool True on success, false on failure.
*/
public function localize( $handle, $object_name, $l10n ) {
if ( 'jquery' === $handle ) {
$handle = 'jquery-core';
}
if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present.
$after = $l10n['l10n_print_after'];
unset( $l10n['l10n_print_after'] );
}
if ( ! is_array( $l10n ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: $l10n, 2: wp_add_inline_script() */
__( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ),
'$l10n
',
'wp_add_inline_script()
'
),
'5.7.0'
);
}
if ( is_string( $l10n ) ) {
$l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' );
} else {
foreach ( (array) $l10n as $key => $value ) {
if ( ! is_scalar( $value ) ) {
continue;
}
$l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
}
}
$script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
if ( ! empty( $after ) ) {
$script .= "\n$after;";
}
$data = $this->get_data( $handle, 'data' );
if ( ! empty( $data ) ) {
$script = "$data\n$script";
}
return $this->add_data( $handle, 'data', $script );
}
/**
* Sets handle group.
*
* @since 2.8.0
*
* @see WP_Dependencies::set_group()
*
* @param string $handle Name of the item. Should be unique.
* @param bool $recursion Internal flag that calling function was called recursively.
* @param int|false $group Optional. Group level: level (int), no groups (false).
* Default false.
* @return bool Not already in the group or a lower group.
*/
public function set_group( $handle, $recursion, $group = false ) {
if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) {
$grp = 1;
} else {
$grp = (int) $this->get_data( $handle, 'group' );
}
if ( false !== $group && $grp > $group ) {
$grp = $group;
}
return parent::set_group( $handle, $recursion, $grp );
}
/**
* Sets a translation textdomain.
*
* @since 5.0.0
* @since 5.1.0 The `$domain` parameter was made optional.
*
* @param string $handle Name of the script to register a translation domain to.
* @param string $domain Optional. Text domain. Default 'default'.
* @param string $path Optional. The full file path to the directory containing translation files.
* @return bool True if the text domain was registered, false if not.
*/
public function set_translations( $handle, $domain = 'default', $path = null ) {
if ( ! isset( $this->registered[ $handle ] ) ) {
return false;
}
/** @var \_WP_Dependency $obj */
$obj = $this->registered[ $handle ];
if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
$obj->deps[] = 'wp-i18n';
}
return $obj->set_translations( $domain, $path );
}
/**
* Prints translations set for a specific handle.
*
* @since 5.0.0
*
* @param string $handle Name of the script to add the inline script to.
* Must be lowercase.
* @param bool $echo Optional. Whether to echo the script
* instead of just returning it. Default true.
* @return string|false Script on success, false otherwise.
*/
public function print_translations( $handle, $echo = true ) {
if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
return false;
}
$domain = $this->registered[ $handle ]->textdomain;
$path = $this->registered[ $handle ]->translations_path;
$json_translations = load_script_textdomain( $handle, $domain, $path );
if ( ! $json_translations ) {
// Register empty locale data object to ensure the domain still exists.
$json_translations = '{ "locale_data": { "messages": { "": {} } } }';
}
$output = <<\n%s\n\n", $this->type_attr, esc_attr( $handle ), $output );
}
return $output;
}
/**
* Determines script dependencies.
*
* @since 2.1.0
*
* @see WP_Dependencies::all_deps()
*
* @param string|string[] $handles Item handle (string) or item handles (array of strings).
* @param bool $recursion Optional. Internal flag that function is calling itself.
* Default false.
* @param int|false $group Optional. Group level: level (int), no groups (false).
* Default false.
* @return bool True on success, false on failure.
*/
public function all_deps( $handles, $recursion = false, $group = false ) {
$r = parent::all_deps( $handles, $recursion, $group );
if ( ! $recursion ) {
/**
* Filters the list of script dependencies left to print.
*
* @since 2.3.0
*
* @param string[] $to_do An array of script dependency handles.
*/
$this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
}
return $r;
}
/**
* Processes items and dependencies for the head group.
*
* @since 2.8.0
*
* @see WP_Dependencies::do_items()
*
* @return string[] Handles of items that have been processed.
*/
public function do_head_items() {
$this->do_items( false, 0 );
return $this->done;
}
/**
* Processes items and dependencies for the footer group.
*
* @since 2.8.0
*
* @see WP_Dependencies::do_items()
*
* @return string[] Handles of items that have been processed.
*/
public function do_footer_items() {
$this->do_items( false, 1 );
return $this->done;
}
/**
* Whether a handle's source is in a default directory.
*
* @since 2.8.0
*
* @param string $src The source of the enqueued script.
* @return bool True if found, false if not.
*/
public function in_default_dir( $src ) {
if ( ! $this->default_dirs ) {
return true;
}
if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
return false;
}
foreach ( (array) $this->default_dirs as $test ) {
if ( 0 === strpos( $src, $test ) ) {
return true;
}
}
return false;
}
/**
* Resets class properties.
*
* @since 2.8.0
*/
public function reset() {
$this->do_concat = false;
$this->print_code = '';
$this->concat = '';
$this->concat_version = '';
$this->print_html = '';
$this->ext_version = '';
$this->ext_handles = '';
}
}
ge_users = array_intersect( $user_data->roles, array_unique( $shop_manager_editable_roles ) );
// Check if Shop Manager can edit customer or with the is same shop manager.
if ( 0 < count( $can_manage_users ) || intval( $object_id ) === intval( get_current_user_id() ) ) {
$permission = current_user_can( $contexts[ $context ], $object_id );
}
}
} else {
$permission = current_user_can( $contexts[ $context ], $object_id );
}
return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, 'user' );
}
/**
* Check permissions of product terms on REST API.
*
* @since 2.6.0
* @param string $taxonomy Taxonomy.
* @param string $context Request context.
* @param int $object_id Post ID.
* @return bool
*/
function wc_rest_check_product_term_permissions( $taxonomy, $context = 'read', $object_id = 0 ) {
$contexts = array(
'read' => 'manage_terms',
'create' => 'edit_terms',
'edit' => 'edit_terms',
'delete' => 'delete_terms',
'batch' => 'edit_terms',
);
$cap = $contexts[ $context ];
$taxonomy_object = get_taxonomy( $taxonomy );
$permission = current_user_can( $taxonomy_object->cap->$cap, $object_id );
return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, $taxonomy );
}
/**
* Check manager permissions on REST API.
*
* @since 2.6.0
* @param string $object Object.
* @param string $context Request context.
* @return bool
*/
function wc_rest_check_manager_permissions( $object, $context = 'read' ) {
$objects = array(
'reports' => 'view_woocommerce_reports',
'settings' => 'manage_woocommerce',
'system_status' => 'manage_woocommerce',
'attributes' => 'manage_product_terms',
'shipping_methods' => 'manage_woocommerce',
'payment_gateways' => 'manage_woocommerce',
'webhooks' => 'manage_woocommerce',
);
$permission = current_user_can( $objects[ $object ] );
return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, 0, $object );
}
/**
* Check product reviews permissions on REST API.
*
* @since 3.5.0
* @param string $context Request context.
* @param string $object_id Object ID.
* @return bool
*/
function wc_rest_check_product_reviews_permissions( $context = 'read', $object_id = 0 ) {
$permission = false;
$contexts = array(
'read' => 'moderate_comments',
'create' => 'moderate_comments',
'edit' => 'moderate_comments',
'delete' => 'moderate_comments',
'batch' => 'moderate_comments',
);
if ( isset( $contexts[ $context ] ) ) {
$permission = current_user_can( $contexts[ $context ] );
}
return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, 'product_review' );
}