HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.2.34
System: Linux atalantini.com 3.10.0-1127.13.1.el7.x86_64 #1 SMP Tue Jun 23 15:46:38 UTC 2020 x86_64
User: root (0)
PHP: 7.2.34
Disabled: NONE
Upload Files
File: //opt/plugins/cbxwpbookmark/includes/class-cbxwpbookmark-activator.php
<?php

/**
 * Fired during plugin activation
 *
 * @link       codeboxr.com
 * @since      1.0.0
 *
 * @package    Cbxwpbookmark
 * @subpackage Cbxwpbookmark/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Cbxwpbookmark
 * @subpackage Cbxwpbookmark/includes
 * @author     CBX Team  <info@codeboxr.com>
 */
class CBXWPBookmark_Activator {

	/**
	 * Short Description. (use period)
	 *
	 * Long Description.
	 *
	 * @since    1.0.0
	 */
	public static function activate() {

		CBXWPBookmarkHelper::create_tables();

		set_transient( 'cbxwpbookmark_activated_notice', 1 );
	}//end activate

	/**
	 * Create pages that the plugin relies on, storing page id's in variables.
	 */
	public static function cbxbookmark_create_pages() {
		$pages = apply_filters( 'cbxwpbookmark_create_pages',
			array(
				'mybookmark_pageid' => array(
					'slug'    => _x( 'cbxbookmark', 'Page slug', 'cbxwpbookmark' ),
					'title'   => _x( 'My Bookmarks', 'Page title', 'cbxwpbookmark' ),
					'content' => '[cbxwpbookmark-mycat]' . '[cbxwpbookmark]',
				),
			) );

		foreach ( $pages as $key => $page ) {
			CBXWPBookmark_Activator::cbxbookmark_create_page( $key, esc_sql( $page['slug'] ), $page['title'], $page['content'] );
		}
	}//end cbxbookmark_create_pages

	/**
	 * Create a page and store the ID in an option.
	 *
	 * @param string $key
	 * @param string $slug
	 * @param string $page_title
	 * @param string $page_content
	 *
	 * @return int|string|WP_Error|null
	 */
	public static function cbxbookmark_create_page( $key = '', $slug = '', $page_title = '', $page_content = '' ) {
		global $wpdb;

		if ( $key == '' ) {
			return null;
		}
		if ( $slug == '' ) {
			return null;
		}


		$cbxwpbookmark_basics = get_option( 'cbxwpbookmark_basics' );
		$option_value         = isset( $cbxwpbookmark_basics[ $key ] ) ? intval( $cbxwpbookmark_basics[ $key ] ) : 0;

		//if valid page id already exists
		if ( $option_value > 0 ) {
			$page_object = get_post( $option_value );

			if ( is_object( $page_object ) && 'page' === $page_object->post_type && ! in_array( $page_object->post_status,
					array(
						'pending',
						'trash',
						'future',
						'auto-draft',
					) ) ) {
				// Valid page is already in place
				return $page_object->ID;
			}
		}

		if ( strlen( $page_content ) > 0 ) {
			// Search for an existing page with the specified page content (typically a shortcode)
			$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
		} else {
			// Search for an existing page with the specified page slug
			$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' )  AND post_name = %s LIMIT 1;", $slug ) );
		}

		$valid_page_found = apply_filters( 'cbxwpbookmark_create_page_id', $valid_page_found, $slug, $page_content );


		// Search for a matching valid trashed page
		if ( strlen( $page_content ) > 0 ) {
			// Search for an existing page with the specified page content (typically a shortcode)
			$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
		} else {
			// Search for an existing page with the specified page slug
			$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) );
		}

		if ( $trashed_page_found ) {
			$page_id   = $trashed_page_found;
			$page_data = array(
				'ID'          => $page_id,
				'post_status' => 'publish',
			);
			wp_update_post( $page_data );
		} else {
			$page_data = array(
				'post_status'    => 'publish',
				'post_type'      => 'page',
				//'post_author'    => 1,
				'post_name'      => $slug,
				'post_title'     => $page_title,
				'post_content'   => $page_content,
				//'post_parent'    => $post_parent,
				'comment_status' => 'closed',
			);
			$page_id   = wp_insert_post( $page_data );
		}

		//let's update the option
		$cbxwpbookmark_basics[ $key ] = $page_id;
		update_option( 'cbxwpbookmark_basics', $cbxwpbookmark_basics );

		return $page_id;
	}//end cbxbookmark_create_page

}//end class CBXWPBookmark_Activator