*/ class Wp_Database_Tools_Database_Data_Origin { /** * The id of Origin. * * @since 1.0.0 * @access protected * @var string $id The id of Origin. */ protected $id; /** * The name of Origin. * * @since 1.0.0 * @access protected * @var string $name The name of Origin. */ protected $name; /** * The type of Origin. * * @since 1.0.0 * @access protected * @var string $type uncategorized|plugin|theme. */ protected $type; /** * The search term of Origin. * * @since 1.0.0 * @access protected * @var string $search The search term of Origin. */ protected $search; /** * The slug term of Origin. * * @since 1.0.0 * @access protected * @var string $slug The slug term of Origin. */ protected $slug; /** * The warning of Origin. * * When an origin is detected by the algorithm the warning attribute is set to yes. * * @since 1.0.0 * @access protected * @var string $warning yes|no. */ protected $warning; /** * The homepage of Origin. * * @since 1.0.0 * @access protected * @var string $homepage The website of the Origin. */ protected $homepage; /** * The download url of Origin. * * @since 1.0.0 * @access protected * @var string $download_link The download url of Origin. */ protected $download_link; /** * The rating of Origin. * * @since 1.0.0 * @access protected * @var string $rating The rating of Origin. */ protected $rating; /** * The status of Origin. * * @since 1.0.0 * @access protected * @var string $status active|inactive|uninstall|unknown. */ protected $status; /** * The current version of Origin. * * @since 1.0.0 * @access protected * @var string $version The current version of Origin. */ protected $version; /** * The reliability of Origin. * * @since 1.0.0 * @access protected * @var int $reliability The reliability of Origin. */ protected $reliability; /** * The reliability level of Origin. * * @since 1.0.0 * @access protected * @var int $reliability_level low|medium|hight. */ protected $reliability_level; /** * The question feedback origin. * * @since 1.0.0 * @access protected * @var array $question The question feedback origin. */ protected $question; /** * The total likes. * * @since 1.0.0 * @access protected * @var int $likes The total likes. */ protected $likes; /** * The total dislikes. * * @since 1.0.0 * @access protected * @var int $dislikes The total dislikes. */ protected $dislikes; /** * The author of Origin. * * @since 1.0.0 * @access protected * @var Wp_Database_Tools_Database_Data_Author $author The author of Origin. */ protected $author; /** * The Marketplace of Origin. * * @since 1.0.0 * @access protected * @var Wp_Database_Tools_Database_Data_Marketplace $marketplace The Marketplace of Origin. */ protected $marketplace; /** * Constructor * * Create the instance of a Origin. * * @access public * @since 1.0.0 */ public function __construct() { $this->id = 'uncategorized'; $this->search = 'Uncategorized'; $this->name = 'Uncategorized'; $this->type = 'uncategorized'; $this->status = 'unknown'; $this->slug = '-'; $this->rating = '-'; $this->download_link = ''; $this->homepage = ''; $this->dislikes = 0; $this->likes = 0; $this->author = new Wp_Database_Tools_Database_Data_Author(); $this->marketplace = new Wp_Database_Tools_Database_Data_Marketplace(); } /** * Set object attributes via API information * * @access public * @since 1.0.0 * * @param Object $api_data Object obtained from API information. * @param string $section The section of data. */ public function set_origin_by_api( $api_data, $section ) { // Dynamic access property. if ( 'tables' === $section ) { $morph = 'tableable'; } if ( 'options' === $section ) { $morph = 'optionable'; } if ( 'transients' === $section ) { $morph = 'transientable'; } if ( 'cronjobs' === $section ) { $morph = 'cronable'; } $source = $api_data['source']; $origin = $api_data['origin']; // Set the origin fields. $this->type = $source->{$morph . '_type'} ?? $this->type; if ( 'uncategorized' !== $this->type ) { $this->type = strtolower( str_replace( 'App\Models\\', '', $this->type ) ); } $this->id = ( 'core' === $this->type ) ? 'wordpress-core' : $origin->slug ?? $this->id; $this->name = ( 'core' === $this->type ) ? __( 'WordPress Core', 'wp-database-tools' ) : $origin->custom_name ?? $origin->name ?? $this->slug; $this->search = ( 'core' === $this->type ) ? 'WordPress Core' : $origin->slug ?? $this->slug; $this->homepage = ( 'core' === $this->type ) ? 'https://es.wordpress.org/' : $origin->homepage ?? $this->homepage; $this->download_link = ( 'core' === $this->type ) ? 'https://wordpress.org/latest.zip' : $origin->download_link ?? $this->download_link; $this->slug = ( 'core' === $this->type ) ? '-' : $origin->slug ?? $this->slug; $this->warning = 'no'; $this->rating = $origin->rating ?? $this->rating; $this->reliability = $source->reliability ?? $this->reliability; $this->reliability_level = $this->calculate_reliability_level( $this->reliability ); $this->dislikes = $source->dislikes; $this->likes = $source->likes; $this->author->set_author_by_api( $api_data['author'] ); $this->marketplace->set_marketplace_by_api( $api_data['marketplace'] ); } /** * Set object attributes via algorithm * * @access public * @since 1.0.0 * * @param Array $data_algorithm Array returning the identification algorithm. */ public function set_origin_by_algorithm( $data_algorithm ) { // Set origin fields. $this->id = $data_algorithm['origin']['slug'] ?? $data_algorithm['origin']['name']; $this->version = $data_algorithm['origin']['version']; $this->name = $data_algorithm['origin']['name']; $this->search = $data_algorithm['origin']['slug']; $this->slug = $data_algorithm['origin']['slug']; $this->type = $data_algorithm['type']; $this->warning = 'yes'; $this->reliability = $this->calculate_match_porcent( $data_algorithm['length'], $data_algorithm['match'] ); $this->reliability_level = $this->calculate_reliability_level( $this->reliability ); $this->author->set_author_by_algorithm( $data_algorithm['origin'] ); $this->marketplace->set_marketplace_by_algorithm( $data_algorithm['origin'] ); } /** * Set the selection of origins in the frontend (choices) by passing * the data through admin ajax. * * @access public * @since 1.0.0 * * @param Array $choices Array returning choices. */ public function check_choices( $choices ) { if ( 'uncategorized' !== $this->type && 'core' !== $this->type ) { $index = ( 'plugin' === $this->type ) ? 1 : 2; $value = $this->name . '$' . $this->type . '$' . $this->warning; $label = ( 'no' === $this->warning ) ? $this->name : '
' . $this->name . '
'; $key = array_search( $value, array_column( $choices[ $index ]['choices'], 'value' ) ); if ( false === $key ) { $data = array( 'value' => $value, 'label' => $label, 'selected' => false, ); array_push( $choices[ $index ]['choices'], $data ); } } return $choices; } /** * Set the selection of origins in the frontend (choices) by passing * the data through admin ajax. * * @access public * @since 1.0.0 * * @param Array $plugins Array with all plugins. * @param Array $themes Array with all themes. */ public function set_status( $plugins, $themes ) { if ( 'plugin' === $this->type ) { $key = array_search( $this->slug, array_filter( array_combine( array_keys( $plugins ), array_column( $plugins, 'slug' ) ) ) ); if ( false === $key ) { $this->status = 'uninstalled'; return; } $this->status = $plugins[ $key ]['status']; return; } if ( 'theme' === $this->type ) { if ( isset( $themes[ $this->slug ] ) ) { $this->status = $themes[ $this->slug ]['status']; return; } $this->status = 'uninstalled'; return; } if ( 'core' === $this->type ) { $this->status = 'active'; return; } $this->status = 'unknown'; } /** * Calculate the match porcent. * * @access public * @since 1.0.0 * * @param string $length length. * @param string $match match. */ protected function calculate_match_porcent( $length, $match ) { if ( $match > $length ) { return 90; } return ( $match * 100 ) / 25; } /** * Return the level string. * * @access public * @since 1.0.0 * * @param int $level the puntuation of reliability. * * @return string The string indicates by level. */ protected function calculate_reliability_level( $level ) { if ( $level < 50 ) { return 'low'; } if ( $level >= 50 && $level <= 70 ) { return 'medium'; } if ( $level > 70 ) { return 'hight'; } } /** * Return the author of Origin. * * @access public * @since 1.0.0 * * @return Wp_Database_Tools_Database_Data_Author $author The author of Origin. */ public function get_author() { return $this->author; } /** * Return the marketplace of Origin. * * @access public * @since 1.0.0 * * @return Wp_Database_Tools_Database_Data_Marketplace $marketplace The marketplace of Origin. */ public function get_marketplace() { return $this->marketplace; } /** * Return the type of Origin. * * @access public * @since 1.0.0 * * @return string $type The type of Origin. */ public function get_type() { return $this->type; } /** * Set the question attribute * * @access public * @since 1.0.0 * * @param string $name the name of source. */ public function set_question( $name ) { $quote = str_contains( strtolower( WPDBT_CURRENT_LANG ), 'es' ) ? '¿' : ''; $this->question = $quote . '' . $name . ' ' . __( 'belongs to this', 'wp-database-tools' ) . ' ' . $this->get_type() . '?'; } /** * Set the question attribute * * @access public * @since 1.0.0 * * @param Wp_Database_Tools_Database_Data_Author $value The author. */ public function set_author( $value ) { $this->author = $value; } /** * Set the question attribute * * @access public * @since 1.0.0 * * @param Wp_Database_Tools_Database_Data_Marketplace $value The marketplace. */ public function set_marketplace( $value ) { $this->marketplace = $value; } /** * Return attributes as an array * * @access public * @since 1.0.0 * * @return Array Array attributes. */ public function return_object_vars() { return get_object_vars( $this ); } }