<?php
/**
 * Twenty Twenty-Five functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package WordPress
 * @subpackage Twenty_Twenty_Five
 * @since Twenty Twenty-Five 1.0
 */

// Adds theme support for post formats.
if ( ! function_exists( 'twentytwentyfive_post_format_setup' ) ) :
	/**
	 * Adds theme support for post formats.
	 *
	 * @since Twenty Twenty-Five 1.0
	 *
	 * @return void
	 */
	function twentytwentyfive_post_format_setup() {
		add_theme_support( 'post-formats', array( 'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video' ) );
	}
endif;
add_action( 'after_setup_theme', 'twentytwentyfive_post_format_setup' );

// Enqueues editor-style.css in the editors.
if ( ! function_exists( 'twentytwentyfive_editor_style' ) ) :
	/**
	 * Enqueues editor-style.css in the editors.
	 *
	 * @since Twenty Twenty-Five 1.0
	 *
	 * @return void
	 */
	function twentytwentyfive_editor_style() {
		add_editor_style( get_parent_theme_file_uri( 'assets/css/editor-style.css' ) );
	}
endif;
add_action( 'after_setup_theme', 'twentytwentyfive_editor_style' );

// Enqueues style.css on the front.
if ( ! function_exists( 'twentytwentyfive_enqueue_styles' ) ) :
	/**
	 * Enqueues style.css on the front.
	 *
	 * @since Twenty Twenty-Five 1.0
	 *
	 * @return void
	 */
	function twentytwentyfive_enqueue_styles() {
		wp_enqueue_style(
			'twentytwentyfive-style',
			get_parent_theme_file_uri( 'style.css' ),
			array(),
			wp_get_theme()->get( 'Version' )
		);
	}
endif;
add_action( 'wp_enqueue_scripts', 'twentytwentyfive_enqueue_styles' );

// Registers custom block styles.
if ( ! function_exists( 'twentytwentyfive_block_styles' ) ) :
	/**
	 * Registers custom block styles.
	 *
	 * @since Twenty Twenty-Five 1.0
	 *
	 * @return void
	 */
	function twentytwentyfive_block_styles() {
		register_block_style(
			'core/list',
			array(
				'name'         => 'checkmark-list',
				'label'        => __( 'Checkmark', 'twentytwentyfive' ),
				'inline_style' => '
				ul.is-style-checkmark-list {
					list-style-type: "\2713";
				}

				ul.is-style-checkmark-list li {
					padding-inline-start: 1ch;
				}',
			)
		);
	}
endif;
add_action( 'init', 'twentytwentyfive_block_styles' );

// Registers pattern categories.
if ( ! function_exists( 'twentytwentyfive_pattern_categories' ) ) :
	/**
	 * Registers pattern categories.
	 *
	 * @since Twenty Twenty-Five 1.0
	 *
	 * @return void
	 */
	function twentytwentyfive_pattern_categories() {

		register_block_pattern_category(
			'twentytwentyfive_page',
			array(
				'label'       => __( 'Pages', 'twentytwentyfive' ),
				'description' => __( 'A collection of full page layouts.', 'twentytwentyfive' ),
			)
		);

		register_block_pattern_category(
			'twentytwentyfive_post-format',
			array(
				'label'       => __( 'Post formats', 'twentytwentyfive' ),
				'description' => __( 'A collection of post format patterns.', 'twentytwentyfive' ),
			)
		);
	}
endif;
add_action( 'init', 'twentytwentyfive_pattern_categories' );

// Registers block binding sources.
if ( ! function_exists( 'twentytwentyfive_register_block_bindings' ) ) :
	/**
	 * Registers the post format block binding source.
	 *
	 * @since Twenty Twenty-Five 1.0
	 *
	 * @return void
	 */
	function twentytwentyfive_register_block_bindings() {
		register_block_bindings_source(
			'twentytwentyfive/format',
			array(
				'label'              => _x( 'Post format name', 'Label for the block binding placeholder in the editor', 'twentytwentyfive' ),
				'get_value_callback' => 'twentytwentyfive_format_binding',
			)
		);
	}
endif;
add_action( 'init', 'twentytwentyfive_register_block_bindings' );

// Registers block binding callback function for the post format name.
if ( ! function_exists( 'twentytwentyfive_format_binding' ) ) :
	/**
	 * Callback function for the post format name block binding source.
	 *
	 * @since Twenty Twenty-Five 1.0
	 *
	 * @return string|void Post format name, or nothing if the format is 'standard'.
	 */
	function twentytwentyfive_format_binding() {
		$post_format_slug = get_post_format();

		if ( $post_format_slug && 'standard' !== $post_format_slug ) {
			return get_post_format_string( $post_format_slug );
		}
	}
endif;



function ajox_update_custom_roles() {
    add_role(
    'ajox_agent',
        __( 'Ajox Agent' ),
        array(
            'read'         => true,  // true allows this capability
            'edit_posts'   => true,
        )
    );
    
    add_role(
    'ajox_staff',
        __( 'Ajox Staff' ),
        array(
            'read'         => true,  // true allows this capability
            'edit_posts'   => true,
        )
    );
    
    add_role(
    'ajox_delivery',
        __( 'Ajox Delivery' ),
        array(
            'read'         => true,  // true allows this capability
            'edit_posts'   => true,
        )
    );
    
}
add_action( 'init', 'ajox_update_custom_roles' );





function AJOX_LEAD_FORM() {
    
    if($_REQUEST['save'] == 'save'){
        
        global $wpdb;
        
        global $user_ID;
        
        $array_photo =  array();
        
        for($i= 0; $i <= 20; $i ++){
            $filename = time().'-'.basename($_FILES['photo']['name'][$i]);
           
            $target_path = "/home/ajoxfashion/public_html/CRM/lead_files/{$filename}";
            
            
            if(move_uploaded_file($_FILES['photo']['tmp_name'][$i], $target_path)) {
               $array_photo[] = $filename;
            }
        }
        
        
        $new_post = array(
            'post_title' => $_REQUEST['full_name'].' '.$_REQUEST['phone'],
            'post_content' => $_REQUEST['data'],
            'post_status' => 'publish',
            'post_date' => date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) ),
            'post_author' => $user_ID,
            'post_type' => 'ajox_product',
            
            'phone' => $_REQUEST['phone'],
            'full_name' => $_REQUEST['full_name'],
            'email' => $_REQUEST['email'],
            'address' => $_REQUEST['address'],
            'postcode' => $_REQUEST['postcode'],
            'message' => $_REQUEST['message'],
            'private_note' => $_REQUEST['private_note'],
            'aproduct_type' => $_REQUEST['aproduct_type'],
            'urgent' => $_REQUEST['urgent'],
            'quantity' => $_REQUEST['quantity'],
            'astatus' => $_REQUEST['astatus'],
            'payment_type' => $_REQUEST['payment_type'],
            'amount' => $_REQUEST['amount'],
            'aphotos' => serialize($array_photo)
            );
        $table_name = 'wpab_posts';

        $rowResult = $wpdb->insert($table_name, $new_post, $format->NULL);
        
        if($rowResult > 0){
            echo 'Lead added successfully';
        }else{
            echo 'Lead not added';
        }        

    }
    
    
    global $current_user;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    
    $os = array("administrator", "ajox_agent");
    if (in_array($user_role, $os)) {
        global $product;
       
        $message = '
            <form id="add_lead" action="" method="post" enctype="multipart/form-data">
                <span><label>Phone</label><input type="number" name="phone" required /></span>
                <span><label>Full Name</label><input type="text" name="full_name" /></span>
                <span><label>Email</label><input type="text" name="email" /></span>
                <span><label>Address</label><input type="textarea" name="address" /></span>
                <span><label>Pincode</label><input type="text" name="postcode" /></span>
                <span><label>Amount</label><input type="number" name="amount" required /></span>
                <span><label>Data</label><textarea name="data" rows="10" placeholder="Name: Vinay
                Address: Rohini delhi
                Phone:9313275954
                Postcode: 110085" ></textarea></span>
                <span><label>Message</label><textarea name="message" rows="6" ></textarea></span>
                <span><label>Private Note</label><textarea name="private_note" rows="6" ></textarea></span>
                
                
                <span><label>Add Photo</label><input type="file" name="photo[0]" /></span>
                <span><label>Add Photo</label><input type="file" name="photo[1]" /></span>
                <span><label>Add Photo</label><input type="file" name="photo[2]" /></span>
                <span><label>Add Photo</label><input type="file" name="photo[3]" /></span>
                <span><label>Add Photo</label><input type="file" name="photo[4]" /></span>
                <span class="addphoto"><label>&nbsp;</label>Add more photo\'s</span>
                
                <span><label>Type</label><input type="text" name="aproduct_type" value="key_ring" readonly /></span>
                <span><label>Urgent</label><input type="radio" name="urgent" value="no" checked />No <input type="radio" name="urgent" value="yes" />Yes</span>
                <span><label>Quantity</label><input type="text" name="quantity" value="1" required /></span>
                <span><label>Stauts</label><select name="astatus">
                    <option value="0">New Order</option>
                    <option value="1">Pending</option>
                    <option value="2">Production Done</option>
                    <option value="3">Sent</option>
                </select></span>
                
                <span><label>Payment Type</label><select name="payment_type">
                    <option value="pre-paid">Pre Paid</option>
                    <option value="post-paid">Post Paid</option>
                    <option value="cod">COD</option>
                    <option value="replacement">Replacement</option>
                </select></span>
                
                <span><label>&nbsp;</label><input type="submit" name="save" value="save" /></span>
                
            </form>
        ';
        
    }else{
        $message = '<div>Access permissions denied to access, Agent role can access.</div>';
    }
        
    return $message;
}
add_shortcode('AJOX_LEAD_FORM', 'AJOX_LEAD_FORM');



add_action( 'init', 'ajox_register_post_types' );

function ajox_register_post_types(){

	register_post_type( 'ajox_product', [
		'taxonomies' => [], // post related taxonomies
		'label'  => null,
		'labels' => [
			'name'               => 'Products', // name for the post type.
			'singular_name'      => 'Product', // name for single post of that type.
			'add_new'            => '--', // to add a new post.
			'add_new_item'       => '--', // title for a newly created post in the admin panel.
			'edit_item'          => 'Edit Product', // for editing post type.
			'new_item'           => 'New Product', // new post's text.
			'view_item'          => 'See Product', // for viewing this post type.
			'search_items'       => 'Search Product', // search for these post types.
			'not_found'          => 'Not Found', // if search has not found anything.
			'parent_item_colon'  => '', // for parents (for hierarchical post types).
			'menu_name'          => 'Products', // menu name.
		],
		'description'         => '',
		'public'              => true,
		//'publicly_queryable'  => null, // depends on public
		//'exclude_from_search' => null, // depends on public
		//'show_ui'             => null, // depends on public
		//'show_in_nav_menus'   => null, // depends on public
		'show_in_menu'        => null, // whether to in admin panel menu
		//'show_in_admin_bar'   => null, // depends on show_in_menu.
		'show_in_rest'        => null, // Add to REST API. WP 4.7.
		'rest_base'           => null, // $post_type. WP 4.7.
		'menu_position'       => null,
		'menu_icon'           => null,
		//'capability_type'   => 'post',
		//'capabilities'      => 'post', // Array of additional rights for this post type.
		//'map_meta_cap'      => null, // Set to true to enable the default handler for meta caps.
		'hierarchical'        => false,
		'supports'            => [ 'title', 'editor' ], // [ 'title', 'editor', 'author',
														//   'thumbnail', 'excerpt', 'trackbacks',
														//   'custom-fields', 'comments', 'revisions',
														//   'page-attributes', 'post-formats' ]
		'has_archive'         => false,
		'rewrite'             => false,
		'query_var'           => true,
	] );

}

add_action( 'wp_enqueue_scripts', 'ajox_add_my_script' );
function ajox_add_my_script() {
    wp_enqueue_script(
        'ajox-script',// this is the location of your script file
         get_template_directory_uri() . '/js/your-script.js',
        array('jquery') // this array lists the scripts upon which your script depends
    );
}