<?php

if( ! defined( 'ABSPATH' ) ) die ( '✋' );

class EPS {
    
    public $post_id;
    public $url_app;
    public $nonce;
    private $bot_info;
    private $info_app;
    private $info;
    private $options;
    private $term_id;
    private $dca;
    private $output;
    private $is_mod;
    private $title;

    public function __construct() {
        
        $this->options = array();
        $this->options['edcgp_sapk'] = appyn_options( 'edcgp_sapk', true );
        $this->options['edcgp_post_status'] = appyn_options( 'edcgp_post_status' );
        $this->options['edcgp_create_category'] = appyn_options( 'edcgp_create_category' );
        $this->options['edcgp_create_tax_dev'] = appyn_options( 'edcgp_create_tax_dev' );
        $this->options['edcgp_extracted_images'] = appyn_options( 'edcgp_extracted_images' );
        $this->options['edcgp_sapk'] = appyn_options( 'edcgp_sapk' );
        $this->options['edcgp_mc'] = appyn_options( 'edcgp_mc' );
        $this->options['edcgp_eaa'] = appyn_options( 'edcgp_eaa' );

        $this->dca = appyn_options( 'dedcgp_descamp_actualizar', true ) ? appyn_options( 'dedcgp_descamp_actualizar', true ) : array();
        $this->check_APIKey();
        $this->is_mod = false;
    }

    private function check_APIKey() {
        if( empty(appyn_options( 'apikey', true )) ) {
            $output = array();
            $output['response'] = __( 'Error: La API Key es inválida.', 'appyn' ).' <a href="https://themespixel.net/en/docs/appyn/api-key/" target="_blank">'.__( 'Más información', 'appyn' ).'</a>';
            die(json_encode($output));
        }
    }

    private function check_url_app() {

        if( empty($this->url_app) ){
            $output = array();
            $output['response'] = __( 'Error: No hay URL de Playstore en este post', 'appyn' );
            $output['error_field'] = 'consiguelo';
            die(json_encode($output));
        }
    }

    private function check_if_exist_url() {

        if( ! get_http_response_code( $this->url_app ) ) {
            $output = array();
            $output['response'] = __( 'Error: Al parecer la URL no existe. Verifique nuevamente.', 'appyn' );
            $output['error_field'] = 'consiguelo';
            die(json_encode($output));
        }
    }

    private function getData($get_apk = true) {

        $body = array( 
            'apikey' 	=> appyn_options( 'apikey', true ),
            'website'	=> get_site_url(),
            'app'		=> trim($this->url_app)
        );

        if( !$this->options['edcgp_sapk'] && $get_apk == true ) {
            $body['apk'] = "true";
        }
        
        $output = array();

        $url = API_URL.'/v2/gplay';

        $response = wp_remote_post( $url, array(
            'method'      => 'POST',
            'timeout'     => 60,
            'blocking'    => true,
            'sslverify'   => false,
            'headers'     => array(
                'Referer' 		=> get_site_url(),
                'Cache-Control' => 'max-age=0',
                'Expect' 		=> '',
            ),
            'body' => $body,
        ) );

        if ( ! is_wp_error( $response ) ) {

            $bot = json_decode($response['body'], true);
            $status = (isset($bot['status'])) ? $bot['status'] : false;

            if( $status == 'error' || !$status ) {
                $output['response'] = 'Error: '. $bot['response'];
                die( json_encode($output) );
            }
            return $bot;
        } else {
            $output['response'] = $response->get_error_message();
            die( json_encode($output) );
        }
    }

    public function showData( $url ) {
        $this->url_app = $url;
        $bot = $this->getData(false);

        return $bot['app'];

    }

    private function import_process() {

        if( $this->is_mod ) {
            $bot = $this->getData( false );
        } else {
            $bot = $this->getData();
        }

        $this->bot_info = $bot['app'];

        $this->info_app = array();
        $this->info_app['nombre']  				= $this->bot_info['title'];
        $this->info_app['contenido']  			= $this->bot_info['content'];
        $this->info_app['descripcion']  		= $this->bot_info['description'];
        $this->info_app['fecha_actualizacion']  = $this->bot_info['date'];
        $this->info_app['released_on']          = $this->bot_info['released_on'];
        $this->info_app['last_update'] 		 	= $this->bot_info['last_update'];
        $this->info_app['version']  			= $this->bot_info['version'];
        $this->info_app['requerimientos']  		= $this->bot_info['requires'];
        $this->info_app['novedades']  			= $this->bot_info['whats_new'];
        $this->info_app['imagecover']  			= $this->bot_info['icon'];
        $this->info_app['video']  				= $this->bot_info['video'];
        $this->info_app['tamano']  				= $this->bot_info['size'];
        $this->info_app['categoria']  			= $this->bot_info['cat'];
        $this->info_app['categoria_app']  		= $this->bot_info['app_cat'];
        $this->info_app['desarrollador']  		= $this->bot_info['developer'];
        $this->info_app['pago']  				= $this->bot_info['is_pay'];
        $this->info_app['descargas']  			= $this->bot_info['downloads'];
        $this->info_app['app_id']               = $this->bot_info['app_id'];
        
        $n = 0;
        foreach($this->bot_info['screenshots'] as $screenshot) { 
            if( $n < $this->options['edcgp_extracted_images'] ) {
                $this->info_app['imagenes'][$n] = $screenshot;
            }
            $n++;
        }	
        
        if( $this->options['edcgp_create_category'] != 1 ) {
            require_once( ABSPATH . '/wp-admin/includes/taxonomy.php');
            $this->term_id = term_exists($this->info_app['categoria'], 'category');
        
            if( !$this->term_id ) {
                $cat_defaults = array(
                    'cat_ID' => 0,
                    'cat_name' => $this->info_app['categoria'],
                    'taxonomy' => 'category'
                );
                $this->term_id = wp_insert_category($cat_defaults);
            }
        }
    }

    public function createMod( $id, $u, $post_id = false ) {
    
        $this->is_mod = true;
        $this->options['mod_apps_get_apk'] = appyn_options( 'mod_apps_get_apk', true );
        $url = API_URL.'/v2/mod/import/'.$id.'/'.$u;

        $body = array( 
            'apikey' 	=> appyn_options( 'apikey', true ),
            'website'	=> get_site_url(),
        );

        if( $this->options['mod_apps_get_apk'] ) {
            $body['apk'] = true;
        }

        $response = wp_remote_post( $url, array(
            'method'      => 'POST',
            'timeout'     => 30,
            'blocking'    => true,
            'sslverify'   => false,
            'headers'     => array(
                'Referer' 		=> get_site_url(),
                'Cache-Control' => 'max-age=0',
                'Expect' 		=> '',
            ),
            'body' => $body,
        ) );
        
        if ( ! is_wp_error( $response ) ) {

            $data = json_decode($response['body'], true);

            if( !isset($data['status']) ) die(json_encode(array('status' => 'error', 'response' => __( 'Inténtelo nuevamente...', 'appyn' ))));
    
            if( $data['status'] == 'error' ) die(json_encode($data));
    
            $lang = appyn_options( 'edcgp_lang', get_locale() );
    
            $this->url_app = ( $data['appid'] ) ? 'https://play.google.com/store/apps/details?id='.$data['appid'].'&hl='.$lang.'&gl=LA' : false;

            $mod_apps_data_gpl = appyn_options( 'mod_apps_data_gpl', array('') );

            if( $this->url_app ) {
                $args = array(
                    'sslverify'   => false,
                );
                $request = wp_remote_get( $this->url_app, $args );
    
                if( wp_remote_retrieve_response_code( $request ) == 200 ) {
                            
                    $this->import_process();

                } else {
                    $this->url_app = false;
                    $data['appid'] = null;
                }
            }

            $this->bot_info['apk'] = array( $data['type'] => $data['link']);
            $this->bot_info['total_size'] = $data['filesize'];
            $this->bot_info['uid'] = uniqid();

            $this->title = $data['title'];

            $postarr = array(
                'post_title' => $this->title,
                'post_content' => $data['content'],
                'post_status' => 'draft',
            );
            
            if( appyn_options( 'mod_apps_import_content' ) ) {
                $postarr['post_content'] = $this->bot_info['content'];
            }
    
            if( isset($term_id) ) {
                if( in_array('category', $mod_apps_data_gpl) ) $postarr['post_category'] = array($term_id);
            }
    
            if( appyn_options( 'mod_apps_post_status' ) ) $postarr['post_status'] = 'publish';
    
            $update = false;
            if( $post_id ) {
                $postarr['ID'] = $_POST['post_id'];
                wp_update_post( $postarr );
                $update = true;
            } else {
                $post_id = wp_insert_post( $postarr );
            }

            $this->post_id = $post_id;
    
            $title = ( isset($this->bot_info['title']) ) ? $this->bot_info['title'] : $this->title;

            if( $this->url_app ) {
                
                $screenshots = array_slice( $this->bot_info['screenshots'], 0, appyn_options( 'edcgp_extracted_images' ) );

                if( in_array('screenshots', $mod_apps_data_gpl) )
                    update_post_meta( $post_id, 'datos_imagenes', $screenshots );
    
                if( in_array('video', $mod_apps_data_gpl) ) 
                    update_post_meta( $post_id, 'datos_video', array('id' => $this->bot_info['video']) );
                
                if( in_array('rating', $mod_apps_data_gpl) ) {
                    update_post_meta( $post_id, "new_rating_users", ((isset($this->bot_info['rating']['users'])) ? $this->bot_info['rating']['users'] : '') );
                    update_post_meta( $post_id, "new_rating_count", ((isset($this->bot_info['rating']['total'])) ? $this->bot_info['rating']['total'] : '') );
                    update_post_meta( $post_id, 'new_rating_average', ((isset($this->bot_info['rating']['average'])) ? $this->bot_info['rating']['average'] : '') );
                }
                
                if( in_array('developer', $mod_apps_data_gpl) ) {
                    $post_datos_informacion = str_replace(',', '', $this->bot_info['developer']);
                    wp_insert_term( $post_datos_informacion, 'dev' );
                    wp_set_post_terms(  $post_id, $post_datos_informacion, 'dev' );
                }
    
                px_upload_image( array('imagecover' => $this->bot_info['icon'], 'nombre' => $title), $post_id );
            }
                
            update_post_meta( $post_id, 'app_type', 1 );
    
            update_post_meta( $post_id, 'mod_app_id', $u );
                
            if( isset($term_id) )
                wp_set_post_terms( $post_id, $term_id, 'category' );

            if( $post_id ) {
                $this->output['post_id'] = $post_id;
                $this->info = __( 'Información importada.', 'appyn' )."\n";
                $s = __( 'Entrada "%s" creada.', 'appyn' );
                if( $update ) {
                    $s = __( 'Entrada "%s" actualizada.', 'appyn' );
                }
                $this->output['info_text'] = '<i class="fa fa-check"></i> '.sprintf($s, $title).' <a href="'.get_edit_post_link($post_id).'" target="_blank">'.__( 'Ver post', 'appyn' ).'</a>';
            }

            if( !isset($data['appid']) ) $this->output['response'] = __( 'No se encontró el ID de la app por lo que no se importó datos como las calificaciones, capturas de pantalla, etc.', 'appyn' );
                
            if( $this->is_mod ) {
                
                $link = $data['link'];
                if( $this->options['mod_apps_get_apk'] ) {
                    $shrt = appyn_options( 'mod_apps_sapk_shortlink', true );
                    $link = px_shorten_download_link( $data['link'], $shrt );
                }

                $dd = array(
                    'option' => 'links',
                    array(
                        'link' => $link,
                        'texto' => $data['text'],
                        'link_original' => $data['link'],
                    )
                );
                if( $shrt ) {
                    $dd[0]['shortlink'] = $link;
                }
                update_post_meta( $post_id, 'datos_download', $dd );
            }
            
            if( $update ) {
                return $this->after_process( 'update' );
            } else {
                return $this->after_process( 'create' );
            }
            
        }
    } 


    public function createPost( $url_app ) {
        
        $this->url_app = trim($url_app);

        $this->check_url_app();
        
        $this->check_if_exist_url();

        $this->checkExists();

        $this->import_process();
                
        $my_post = array(
            'post_title'    => wp_strip_all_tags( $this->info_app['nombre'] ),
            'post_content'  => $this->info_app['contenido'],
            'post_author'   => get_current_user_id(),
        );
        if( $this->options['edcgp_post_status'] == 1 ) {
            $my_post['post_status'] = 'publish';
        } else {
            $my_post['post_status'] = 'draft';
        }

        if( $this->options['edcgp_create_category'] != 1 ) {
            $my_post['post_category'] = array($this->term_id);
        }

        $this->post_id = wp_insert_post( $my_post );

        if( $this->post_id ) {
            $this->output['post_id'] = $this->post_id;
            $this->info = __( 'Información importada.', 'appyn' )."\n";
            $this->output['info_text'] = '<i class="fa fa-check"></i> '.sprintf(__( 'Entrada "%s" creada.', 'appyn' ), $this->info_app['nombre']).' <a href="'.get_edit_post_link($this->post_id).'" target="_blank">'.__( 'Ver post', 'appyn' ).'</a>';
        }

        return $this->after_process( 'create' );
    }

    public function updatePost( $post_id ) {

        if( appyn_options( 'edcgp_update_post', true ) && ! wp_get_post_parent_id($post_id) ) {
            $post_id = apply_filters( 'px_process_convert_post_old_version', $post_id );
        }

        $this->post_id = $post_id;
                
        $this->url_app = trim(get_datos_info('consiguelo', false, $this->post_id));

        $this->check_url_app();

        $this->check_if_exist_url();

        $this->import_process();

        $my_post = array(
            'ID'       		=> $this->post_id,
            'post_title'    => wp_strip_all_tags( $this->info_app['nombre'] ),
            'post_content'  => $this->info_app['contenido'],
            'post_author'   => get_current_user_id(),
        );
        $my_post['post_status'] = get_post_status($this->post_id);
        
        if( $this->options['edcgp_create_category'] != 1 && $this->options['edcgp_mc'] != 1 ) {
            $my_post['post_category'] = array($this->term_id);
        }
        
        if( in_array('app_title', $this->dca) )
            unset($my_post['post_title']);
    
        if( in_array('app_content', $this->dca) )
            unset($my_post['post_content']);

        $cb = get_post_meta( $this->post_id, "custom_boxes", true );
        $ac = appyn_gpm( $this->post_id, 'appyn_ads_control' );

        wp_update_post( $my_post, true );

        update_post_meta( $this->post_id, "custom_boxes", $cb );
        update_post_meta( $this->post_id, "appyn_ads_control", $ac );
        
        if( !is_wp_error($this->post_id) ) {
            $this->output['post_id'] = $this->post_id;
            $this->info = __( 'Información actualizada.', 'appyn' )."\n";
            $this->output['info_text'] = '<i class="fa fa-check"></i> '.sprintf(__( 'Entrada "%s" actualizada.', 'appyn' ), $this->info_app['nombre']);
        }

        return $this->after_process( 'update' );
        
    }

    public function reimportPost( $post_id ) {

        $this->post_id = $post_id;
                
        $this->url_app = trim(get_datos_info('consiguelo', false, $this->post_id));

        $this->check_url_app();

        $this->import_process();

        $my_post = array(
            'ID'       		=> $this->post_id,
            'post_title'    => wp_strip_all_tags( $this->info_app['nombre'] ),
            'post_content'  => $this->info_app['contenido'],
            'post_author'   => get_current_user_id(),
        );
        $my_post['post_status'] = get_post_status($this->post_id);
        
        if( $this->options['edcgp_create_category'] != 1 && $this->options['edcgp_mc'] != 1 ) {
            $my_post['post_category'] = array($this->term_id);
        }
        
        if( in_array('app_title', $this->dca) )
            unset($my_post['post_title']);
    
        if( in_array('app_content', $this->dca) )
            unset($my_post['post_content']);

        $cb = get_post_meta( $this->post_id, "custom_boxes", true );
        $ac = appyn_gpm( $this->post_id, 'appyn_ads_control' );

        wp_update_post( $my_post, true );

        update_post_meta( $this->post_id, "custom_boxes", $cb );
        update_post_meta( $this->post_id, "appyn_ads_control", $ac );
        
        if( !is_wp_error($this->post_id) ) {
            $this->output['post_id'] = $this->post_id;
            $this->info = __( 'Información actualizada.', 'appyn' )."\n";
            $this->output['info_text'] = '<i class="fa fa-check"></i> '.sprintf(__( 'Entrada "%s" actualizada.', 'appyn' ), $this->info_app['nombre']);
        }

        return $this->after_process( 'update' );
        
    }
    
    private function after_process( $type = 'create' ) {

        if( isset( $this->info_app['app_id']) ) {
            update_post_meta( $this->post_id, "px_app_id", $this->info_app['app_id'] );
            
            if( ($type == 'create' && $this->options['edcgp_create_category'] != 1) || ($type == 'update' && $this->options['edcgp_mc'] != 1) ) {
                wp_set_post_terms($this->post_id, $this->term_id, 'category');
            }
            
            if( $this->options['edcgp_create_tax_dev'] != 1 ) {
                $post_datos_informacion = str_replace(',', '', $this->info_app['desarrollador']);
                wp_insert_term( $post_datos_informacion, 'dev' );
                $this->term_id = term_exists( $post_datos_informacion, 'dev' );
                wp_set_post_terms( $this->post_id, $post_datos_informacion, 'dev' );
            }
            
            if( $type == 'update' )
                if( ! in_array('app_ico', $this->dca) ) {
                    $eidcgp = appyn_options( 'eidcgp_update_post' );
                    if( $eidcgp == 1 ) {
                        $attachment_id = get_post_thumbnail_id( $this->post_id );
                        if( $attachment_id ) {
                            $attachment_id = get_post_thumbnail_id( $this->post_id );
                            wp_delete_attachment( $attachment_id, true );
                            delete_post_thumbnail( $this->post_id );
                        }
                    }
                            
                    if( $eidcgp == 1 ) {
                        global $post;
                        $ppt = new WP_Query( array('post_parent' => $this->post_id) );
                        if( $ppt->have_posts() ) {
                            while( $ppt->have_posts() ) { $ppt->the_post();
                                $attachment_id = get_post_thumbnail_id( $post->ID );
                                wp_delete_attachment( $attachment_id, true );
                                delete_post_thumbnail( $post->ID );
                                set_post_thumbnail( $post->ID, $attachment_id );
                            }
                        }
                    }
                    $attach_id = px_upload_image( $this->info_app, $this->post_id );
                }
            
            if( $type == 'create' )
                $attach_id = px_upload_image( $this->info_app, $this->post_id );

            $datos_informacion = array(
                'descripcion' 			=> $this->info_app['descripcion'],
                'version' 				=> $this->info_app['version'],
                'tamano' 				=> $this->info_app['tamano'],
                'fecha_actualizacion' 	=> $this->info_app['fecha_actualizacion'],
                'last_update'		 	=> $this->info_app['last_update'],
                'released_on' 	        => $this->info_app['released_on'],
                'requerimientos' 		=> $this->info_app['requerimientos'],
                'novedades' 			=> $this->info_app['novedades'],
                'app_status'	 		=> 'updated',
                'consiguelo' 			=> $this->url_app,
                'categoria_app'			=> $this->info_app['categoria_app'],
                'descargas'			    => $this->info_app['descargas'],
                'os'					=> 'ANDROID',
            );
        
            if( $type == 'update' )
                if( is_array($this->dca) )
                    if( in_array('app_description', $this->dca) )
                        $datos_informacion['descripcion'] = get_datos_info('descripcion', false, $this->post_id);

            if( $type == 'update' && ! isset($this->bot_info['apk']) )
                $datos_informacion['tamano'] = get_datos_info('tamano', false, $this->post_id);

            if( $type == 'create' )
                $datos_informacion['app_status'] = 'new';
            
            if( $this->bot_info['is_pay'] )
                $datos_informacion['offer']['price'] = 'pago';

            if( $type == 'update' && empty( $this->bot_info['downloads'] ) )
                $datos_informacion['descargas'] = get_datos_info('descargas', false, $this->post_id);
            
            update_post_meta($this->post_id, "datos_informacion", $datos_informacion);
        
            if( $type == 'update' ) {
                if( ! in_array('app_video', $this->dca) )
                    update_post_meta($this->post_id, "datos_video", array('id' => $this->info_app['video']));
                    
                if( ! in_array('app_screenshots', $this->dca) )
                    if( isset($this->info_app['imagenes']) ) 
                        update_post_meta($this->post_id, "datos_imagenes", $this->info_app['imagenes']);
            } else {
                update_post_meta($this->post_id, "datos_video", array('id' => $this->info_app['video']));
                if( isset($this->info_app['imagenes']) ) 
                    update_post_meta($this->post_id, "datos_imagenes", $this->info_app['imagenes']);
            }

            if( get_option( 'appyn_edcgp_rating' ) ) {
                
                $rating = $this->bot_info['rating'];
            
                update_post_meta($this->post_id, "new_rating_users", ((isset($rating['users'])) ? $rating['users'] : ''));
                update_post_meta($this->post_id, "new_rating_count", ((isset($rating['total'])) ? $rating['total'] : ''));
                update_post_meta($this->post_id, 'new_rating_average', ((isset($rating['average'])) ? $rating['average'] : ''));
            }

        }

        if( $type == 'update' ) {

            if( $this->url_app ) {
                $re = '/(?<=[?&]id=)[^&]+/m';
                preg_match_all($re, $this->url_app, $matches, PREG_SET_ORDER, 0);
                $get_app_id = $matches[0][0];
                
                $posts = get_option( 'trans_updated_apps' );
    
                unset($posts[$get_app_id]);
            }

            if( ! $this->is_mod ) {
                update_option( 'trans_updated_apps', $posts );
                
                set_transient( 'trans_count_updated_apps', count($posts) );
            }
            
            if( $this->is_mod ) {
                if( $this->options['mod_apps_get_apk'] == 1 )
                    $this->processAPK();
            } else {
                if( !$this->options['edcgp_sapk'] && !in_array('app_download_links', $this->dca) )
                    $this->processAPK();
            }

            if( $this->options['edcgp_eaa'] ) {
                $datos_download = get_datos_download( $this->post_id );

                foreach( $datos_download['links_options'] as $lo ) {

                    if( !isset($lo['link']) ) continue;

                    $attach_id = attachment_url_to_postid( $lo['link'] );
                    if( ! $attach_id ) {
                        $pi = pathinfo($lo['link']);
                        $i = 0;
                        do {
                            $i++;
                            $file = $pi['dirname'].'/'.$pi['filename'].'-part'.$i.'.'.$pi['extension'];
                            $attach_id = attachment_url_to_postid( $file );

                            if( ! $attach_id ) continue;

                            $fif = str_replace( $pi['filename'].'-part'.$i.'.'.$pi['extension'], $pi['filename'].'.'.$pi['extension'], get_attached_file($attach_id));

                            if( file_exists($fif) ) {
                                unlink( $fif );
                            }
                            wp_delete_attachment($attach_id, true);
                        } while( $i < 5 );
                        continue;
                    } else {
                        wp_delete_attachment($attach_id, true);
                    }
                }
            }
        } elseif( $type == 'create' ) {
            if( $this->is_mod ) {
                if( $this->options['mod_apps_get_apk'] == 1 ) {
                    $this->processAPK();
                } 
            } else {
                if( !$this->options['edcgp_sapk'] )
                    $this->processAPK();
            }
        }

        $this->output['response'] = $this->info;

        return json_encode($this->output);
    }

    private function processAPK() {

        if( $this->bot_info['apk'] ) {

            if( $this->is_mod ) {
                $idps = sanitize_title($this->title);
            } else {
                $re = '/(?<=[?&]id=)[^&]+/m';
                $str = $this->url_app;
                preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
                $idps = $matches[0][0];
            }

            if( $this->is_mod ) $idps .= '-mod'.uniqid();
        
            $this->output['apk_info'] = array(
                'post_id' => $this->post_id,
                'idps' => $idps,
                'total_size' => ( isset($this->bot_info['total_size']) ) ? $this->bot_info['total_size'] : '',
                'uid' => uniqid(),
            );

            if( ! $this->is_mod ) {
                $this->output['apk_info']['date'] = $this->bot_info['last_update'];
                $this->output['apk_info']['url'] = $this->bot_info['apk'];
            } else {
                $this->output['apk_info']['url'] = $this->bot_info['apk'];
            }

            $size = ( !empty($this->bot_info['total_size']) ) ? px_btoc( $this->bot_info['total_size'] ) : '';
    
            $tsr = px_option_selected_upload( $this->is_mod ); 
    
            if( px_check_apk_obb($this->bot_info['apk']) ) {
                $this->output['apk_info']['text']['step1'] = '<i class="fa fa-file" aria-hidden="true"></i> '. __( 'Se encontró un archivo APK y OBB', 'appyn' );
                $this->output['apk_info']['text']['step2'] = '<i class="fa fa-download" aria-hidden="true"></i> '. __( 'Descargando archivo...', 'appyn' ).' '.$size.' (<span class="percentage" style="display:inline-block;">'. __( 'En proceso...', 'appyn' ).'</span>)';
                $this->output['apk_info']['text']['step3'] = '<i class="fa fa-spinner" aria-hidden="true"></i> '. sprintf( __( 'Subiendo a %s en ZIP', 'appyn' ), $tsr);
                
            } else {
                $this->output['apk_info']['text']['step1'] = '<i class="fa fa-file" aria-hidden="true"></i> '. __( 'Se encontró un archivo APK', 'appyn' );
                if( array_key_exists('zip', $this->bot_info['apk']) ) {
                    $this->output['apk_info']['text']['step1'] = '<i class="fa fa-file" aria-hidden="true"></i> '. __( 'Se encontró un archivo ZIP', 'appyn' );
                }
                $this->output['apk_info']['text']['step2'] = '<i class="fa fa-download" aria-hidden="true"></i> '. __( 'Descargando archivo...', 'appyn' ).' '.$size.' (<span class="percentage" style="display:inline-block;">'. __( 'En proceso...', 'appyn' ).'</span>)';
                $this->output['apk_info']['text']['step3'] = '<i class="fa fa-spinner" aria-hidden="true"></i> '. sprintf( __( 'Subiendo a %s', 'appyn' ), $tsr);
            }
        }
    }

    private function checkExists() {
        global $wpdb;
        if( appyn_options('edcgp_appd') != 1 ) {
            $results = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS  {$wpdb->prefix}posts.ID FROM {$wpdb->prefix}posts  INNER JOIN {$wpdb->prefix}postmeta ON ( {$wpdb->prefix}posts.ID = {$wpdb->prefix}postmeta.post_id ) WHERE 1=1  AND (( {$wpdb->prefix}postmeta.meta_key = 'datos_informacion' AND {$wpdb->prefix}postmeta.meta_value LIKE '%:\"{$this->url_app}\";%' )) AND {$wpdb->prefix}posts.post_type = 'post' AND (({$wpdb->prefix}posts.post_status = 'publish' OR {$wpdb->prefix}posts.post_status = 'future' OR {$wpdb->prefix}posts.post_status = 'draft')) GROUP BY {$wpdb->prefix}posts.ID ORDER BY {$wpdb->posts}.post_date DESC LIMIT 0, 10");
            
            if( count($results) != 0 )  {
                $output['response'] = sprintf(__( 'Error: La aplicación que desea importar ya existe. %s', 'appyn' ), '<a href="'.get_edit_post_link($results[0]->ID).'" target="_blank">'.__( 'Ver entrada', 'appyn' ).'</a>');
                echo json_encode($output);
                exit;
            }
        }
    }

}