first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
<?php
class WPML_WP_Roles {
const ROLES_ADMINISTRATOR = 'administrator';
const ROLES_EDITOR = 'editor';
const ROLES_CONTRIBUTOR = 'contributor';
const ROLES_SUBSCRIBER = 'subscriber';
const EDITOR_LEVEL = 'level_7';
const CONTRIBUTOR_LEVEL = 'level_1';
const SUBSCRIBER_LEVEL = 'level_0';
/**
* Returns an array of roles which meet the capability level set in \WPML_WP_Roles::EDITOR_LEVEL.
*
* @return array
*/
public static function get_editor_roles() {
return self::get_roles_for_level( self::EDITOR_LEVEL, self::ROLES_EDITOR );
}
/**
* Returns an array of roles which meet the capability level set in \WPML_WP_Roles::CONTRIBUTOR_LEVEL.
*
* @return array
*/
public static function get_contributor_roles() {
return self::get_roles_for_level( self::CONTRIBUTOR_LEVEL, self::ROLES_CONTRIBUTOR );
}
/**
* Returns an array of roles wich meet the capability level set in \WPML_WP_Roles::SUBSCRIBER_LEVEL.
*
* @return array
*/
public static function get_subscriber_roles() {
return self::get_roles_for_level( self::SUBSCRIBER_LEVEL, self::ROLES_SUBSCRIBER );
}
/**
* @return array
*/
public static function get_roles_up_to_user_level( WP_User $user ) {
return self::get_roles_with_max_level( self::get_user_max_level( $user ), self::ROLES_SUBSCRIBER );
}
/**
* @param WP_User $user
*
* @return int
*/
public static function get_user_max_level( WP_User $user ) {
return self::get_highest_level( $user->get_role_caps() );
}
public static function get_highest_level( array $capabilities ) {
$capabilitiesWithLevel = function ( $has, $cap ) {
return $has && strpos( $cap, 'level_' ) === 0;
};
$levelToNumber = function ( $cap ) {
return (int) substr( $cap, strlen( 'level_' ) );
};
return \wpml_collect( $capabilities )
->filter( $capabilitiesWithLevel )
->keys()
->map( $levelToNumber )
->sort()
->last();
}
/**
* It returns a filtered array of roles.
*
* @param string $level The capability level that the role must meet.
* @param null|string $default The role ID to use as a default.
*
* @return array
*/
private static function get_roles_for_level( $level, $default = null ) {
return \wpml_collect( get_editable_roles() )
->filter(
function ( $role ) use ( $level ) {
return isset( $role['capabilities'][ $level ] ) && $role['capabilities'][ $level ];
}
)
->map( self::create_build_role_entity( $level, $default ) )
->values()
->toArray();
}
private static function get_roles_with_max_level( $level, $default = null ) {
$isRoleLowerThanLevel = function ( $role ) use ( $level ) {
return self::get_highest_level( $role['capabilities'] ) <= $level;
};
return \wpml_collect( get_editable_roles() )
->filter( $isRoleLowerThanLevel )
->map( self::create_build_role_entity( $level, $default ) )
->values()
->toArray();
}
private static function create_build_role_entity( $level, $default = null ) {
$is_default = self::create_is_default( $level, $default );
return function ( $role, $id ) use ( $is_default ) {
return [
'id' => $id,
'name' => $role['name'],
'default' => $is_default( $id ),
];
};
}
private static function create_is_default( $level, $default = null ) {
/**
* Filters the role ID to use as a default.
*
* @param string $default The role ID to use as a default.
* @param string $level The capability level required for this role (@see \WPML_WP_Roles::get_roles_for_level).
*
* @since 2.8.0
*/
$default = apply_filters( 'wpml_role_for_level_default', $default, $level );
return function ( $id ) use ( $default ) {
return $default && ( $default === $id );
};
}
}

View File

@@ -0,0 +1,30 @@
<?php
/**
* Created by PhpStorm.
* User: bruce
* Date: 30/10/17
* Time: 9:09 PM
*/
class WPML_WP_Taxonomy implements IWPML_WP_Element_Type {
public static function get_linked_post_types( $taxonomy ) {
global $wp_taxonomies;
$post_types = array();
if ( isset( $wp_taxonomies[ $taxonomy ] ) && isset( $wp_taxonomies[ $taxonomy ]->object_type ) ) {
$post_types = $wp_taxonomies[ $taxonomy ]->object_type;
}
return $post_types;
}
/**
* @param string $taxonomy
*
* @return false|WP_Taxonomy
*/
public function get_wp_element_type_object( $taxonomy ) {
return get_taxonomy( $taxonomy );
}
}

View File

@@ -0,0 +1,12 @@
<?php
interface IWPML_WP_Element_Type {
/**
* @param string $element_name
*
* @return mixed
*/
public function get_wp_element_type_object( $element_name );
}

View File

@@ -0,0 +1,129 @@
<?php
/**
* Wrapper class for basic PHP functions
*/
class WPML_PHP_Functions {
/**
* Wrapper around PHP constant defined
*
* @param string $constant_name
*
* @return bool
*/
public function defined( $constant_name ) {
return defined( $constant_name );
}
/**
* Wrapper around PHP constant lookup
*
* @param string $constant_name
*
* @return string|int
*/
public function constant( $constant_name ) {
return $this->defined( $constant_name ) ? constant( $constant_name ) : null;
}
/**
* @param string $function_name The function name, as a string.
*
* @return bool true if <i>function_name</i> exists and is a function, false otherwise.
* This function will return false for constructs, such as <b>include_once</b> and <b>echo</b>.
* @return bool
*/
public function function_exists( $function_name ) {
return function_exists( $function_name );
}
/**
* @param string $class_name The class name. The name is matched in a case-insensitive manner.
* @param bool $autoload [optional] Whether or not to call &link.autoload; by default.
*
* @return bool true if <i>class_name</i> is a defined class, false otherwise.
* @return bool
*/
public function class_exists( $class_name, $autoload = true ) {
return class_exists( $class_name, $autoload );
}
/**
* @param string $name The extension name
*
* @return bool true if the extension identified by <i>name</i> is loaded, false otherwise.
*/
public function extension_loaded( $name ) {
return extension_loaded( $name );
}
/**
* @param string $string
*
* @return string
*/
public function mb_strtolower( $string ) {
if ( function_exists( 'mb_strtolower' ) ) {
return mb_strtolower( $string );
}
return strtolower( $string );
}
/**
* Wrapper for \phpversion()
*
* * @param string $extension (optional)
*
* @return string
*/
public function phpversion( $extension = null ) {
if ( defined( 'PHP_VERSION' ) ) {
return PHP_VERSION;
} else {
return phpversion( $extension );
}
}
/**
* Compares two "PHP-standardized" version number strings
*
* @see \WPML_WP_API::version_compare
*
* @param string $version1
* @param string $version2
* @param null $operator
*
* @return mixed
*/
public function version_compare( $version1, $version2, $operator = null ) {
return version_compare( $version1, $version2, $operator );
}
/**
* @param array $array
* @param int $sort_flags
*
* @return array
*/
public function array_unique( $array, $sort_flags = SORT_REGULAR ) {
return wpml_array_unique( $array, $sort_flags );
}
/**
* @param string $message
* @param int $message_type
* @param string $destination
* @param string $extra_headers
*
* @return bool
*/
public function error_log( $message, $message_type = null, $destination = null, $extra_headers = null ) {
return error_log( $message, $message_type, $destination, $extra_headers );
}
public function exit_php() {
exit();
}
}

View File

@@ -0,0 +1,14 @@
<?php
class WPML_WP_Post_Type implements IWPML_WP_Element_Type {
/**
* @param string $post_type
*
* @return null|WP_Post_Type
*/
public function get_wp_element_type_object( $post_type ) {
return get_post_type_object( $post_type );
}
}