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/TermController.php

<?php

namespace RealPress\Controllers;

use RealPress\Helpers\Config;

class TermController {
	public function __construct() {
		add_filter( 'pre_insert_term', array( $this, 'limit_term_level' ), 10, 3 );
	}

	/**
	 * @param $term
	 * @param $taxonomy
	 * @param $args
	 *
	 * @return mixed|\WP_Error
	 */
	public function limit_term_level( $term, $taxonomy, $args ) {
		$taxonomies = array_keys( Config::instance()->get( 'property-type:taxonomies' ) );

		if ( ! in_array( $taxonomy, $taxonomies ) ) {
			return $term;
		}

		$parent_id = $args['parent'];
		if ( $args['parent'] === - 1 ) {
			return $term;
		}

		$parent1 = get_term( $parent_id, $taxonomy );

		if ( $parent1->parent ) {
			$parent2 = get_term( $parent1->parent, $taxonomy );

			if ( $parent2->parent ) {
				return new \WP_Error( 'invalid_term', 'Can not create term level greater than 3.' );
			}
		}

		return $term;
	}
}

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