Sindbad~EG File Manager

Current Path : /home/xiedrke/entrepot/wp-content/plugins/realpress/app/Controllers/
Upload File :
Current File : /home/xiedrke/entrepot/wp-content/plugins/realpress/app/Controllers/PageController.php

<?php

namespace RealPress\Controllers;

use RealPress\Helpers\RestApi;
use RealPress\Helpers\Settings;
use RealPress\Helpers\Config;
use WP_REST_Server;

class PageController {

	public function __construct() {
		add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
		add_filter( 'views_edit-page', array( $this, 'views_realpress_pages' ), 10 );
		add_action( 'pre_get_posts', array( $this, 'get_realpress_pages' ), 10 );
	}

	/**
	 * @return void
	 */
	public function register_rest_routes() {
		do_action( 'realpress/rest-api/before-register' );

		register_rest_route(
			RestApi::generate_namespace(),
			'/page',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $this, 'insert_page' ),
				'permission_callback' => '__return_true',
			),
		);
	}

	/**
	 * @param \WP_REST_Request $request
	 *
	 * @return \WP_REST_Response
	 */
	public function insert_page( \WP_REST_Request $request ) {
		if ( ! current_user_can( 'administrator' ) ) {
			return RestApi::error( esc_html__( 'Could not create a page.', 'realpress' ), 409 );
		}

		$params = $request->get_params();

		$args = array(
			'post_title'   => $params['title'] ?? '',
			'post_content' => $params['content'] ?? '',
			'post_type'    => $params['post_type'] ?? 'post',
			'post_status'  => $params['post_status'] ?? 'publish',
		);

		if ( isset( $params['realpress_page'] ) ) {
			$page_titles = array(
				'agent_list_page'           => __( 'Agent List', 'realpress' ),
				'terms_and_conditions_page' => __( 'Terms and Conditions', 'realpress' ),
				'become_an_agent_page'      => __( 'Become an Agent', 'realpress' ),
				'wishlist_page'             => __( 'WishList', 'realpress' ),
				'compare_property_page'     => __( 'Compare Property', 'realpress' ),
				'my_account_page'           => __( 'My account', 'realpress' ),
			);

			if ( isset( $page_titles[ $params['realpress_page'] ] ) ) {
				$args['post_title'] = $page_titles[ $params['realpress_page'] ];
			}
		}

		$id = wp_insert_post( $args );

		do_action( 'realpress/insert-page/after', $id, $params );

		if ( empty( $id ) || is_wp_error( $id ) ) {
			return RestApi::error( esc_html__( 'Could not create a page.', 'realpress' ), 409 );
		}

		//Update page to DB
		if ( isset( $params['realpress_page'] ) ) {
			Settings::update_field( 'group:page:fields:' . $params['realpress_page'], $id );
		}

		return RestApi::success(
			esc_html__( 'Create a page successfully.', 'realpress' ),
			array(
				'id'    => $id,
				'title' => get_the_title( $id ),
			)
		);
	}

	/**
	 * @param $actions
	 *
	 * @return mixed
	 */
	public function views_realpress_pages( $actions ) {
		$pages = Config::instance()->get( 'realpress-setting:group:page:fields' );

		if ( $pages ) {
			$text = sprintf( __( 'RealPress Pages <span class="count">(%d)</span>', 'realpress' ), sizeof( $pages ) );
			if ( isset( $_GET['realpress-page'] ) && 'yes' === $_GET['realpress-page'] ) {
				$actions['realpress-page'] = sprintf(
					'<a href="%s" class="current">%s</a>',
					admin_url( 'edit.php?post_type=page&realpress-page=yes' ),
					$text
				);
			} else {
				$actions['realpress-page'] = sprintf(
					'<a href="%s">%s</a>',
					admin_url( 'edit.php?post_type=page&realpress-page=yes' ),
					$text
				);
			}
		}

		return $actions;
	}

	/**
	 * @param $query
	 *
	 * @return mixed
	 */
	public function get_realpress_pages( $query ) {
		if ( isset( $_GET['post_type'] ) && 'page' === $_GET['post_type'] && isset( $_GET['realpress-page'] ) && 'yes' === $_GET['realpress-page'] ) {
			$ids = Settings::get_all_static_pages();
			if ( ! empty( $ids ) ) {
				$query->set( 'post__in', $ids );
			}
		}

		return $query;
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists