<?php

// flush rewrite rules for custom post types
function rentpress_flush_rewrite_rules()
{
    // if the plugin has been recently activated, flush the rules and remove the flag
    if (get_option('rentpress_flush_rewrite_rules_flag')) {
        flush_rewrite_rules();
        delete_option('rentpress_flush_rewrite_rules_flag');
    }
}
add_action('init', 'rentpress_flush_rewrite_rules', 21);
register_deactivation_hook(__FILE__, 'flush_rewrite_rules');

// recreate database if plugin is out of date
function rentpress_updateDatabaseTablesAfterPluginVersionChange()
{
    // if the plugin database versions do not match the current plugin version, then reactivate the plugin
    if (get_option('rentpress_plugin_version') !== RENTPRESS_PLUGIN_VERSION) {
        // run deactivate and activate scripts to make new DB and register post types
        require_once RENTPRESS_PLUGIN_ADMIN_DIR . 'admin_install.php';
        rentpress_installPlugin(true);
    }
}
add_action('init', 'rentpress_updateDatabaseTablesAfterPluginVersionChange', 20);

function func_load_admin_cpt_styles_and_scripts()
{
    wp_register_script('admin_cpt_js', RENTPRESS_PLUGIN_ADMIN_SCRIPTS_DIR . 'admin_cpt.js');
    wp_register_style('admin_cpt_styles', RENTPRESS_PLUGIN_ADMIN_STYLES_DIR . 'admin_cpt.css');
    wp_register_style('admin_icons', RENTPRESS_PLUGIN_ADMIN_STYLES_DIR . 'fontawesome.css');
    wp_enqueue_script('admin_cpt_js');
    wp_enqueue_style('admin_cpt_styles');
    wp_enqueue_style('admin_icons');
}

function ga4_enqueue_script()
{
    $rentpress_options = get_option('rentpress_options');
    $measurement_id = isset($rentpress_options['rentpress_google_analytics_api_section_tracking_id']) ? $rentpress_options['rentpress_google_analytics_api_section_tracking_id'] : "";
    if (!empty($measurement_id) ? $measurement_id : "") {
        $script_url = "https://www.googletagmanager.com/gtag/js?id=" . $measurement_id;
        wp_enqueue_script('ga4_script', $script_url, "", "", false);
        wp_add_inline_script(
            "ga4_script",
            "window.dataLayer = window.dataLayer || [];
            function gtag() {
                dataLayer.push(arguments);
            }
            gtag('js', new Date());
            gtag('config', '" . $measurement_id . "');"
        );
    }
}

// make sure these files only run on admin pages
if (is_admin()) {
    require_once RENTPRESS_PLUGIN_ADMIN_VIEW_MENU_DIR . 'rentpress_sync_options.php';
    require_once RENTPRESS_PLUGIN_ADMIN_DIR . 'admin_ajax.php';
    //Tell WordPress to register the scripts
    add_action('admin_enqueue_scripts', 'func_load_admin_cpt_styles_and_scripts');
} else {
    // add GA to all pages
    add_action('wp_enqueue_scripts', 'ga4_enqueue_script');
    // disable shortcode render in admin
    require_once RENTPRESS_PLUGIN_PUBLIC_SHORTCODES . 'rentpress_shortcodes.php';
}

// files that always need to run
require_once RENTPRESS_PLUGIN_ADMIN_DIR . 'admin_crons.php';
require_once RENTPRESS_PLUGIN_ADMIN_POSTS . 'register_rentpress_posts.php';
require_once RENTPRESS_PLUGIN_ADMIN_DIR . 'taxonomy/register_rentpress_taxonomies.php';
rentpress_registerAllPostTypes();

function rentpress_removeDBRowForDeletedPost($postid, $post)
{
    if ($post->post_type === 'rentpress_property') {
        require_once RENTPRESS_PLUGIN_DATA_ACCESS . 'data_layer.php';
        rentpress_deleteRowRowFromTableWithPostID('rentpress_properties', 'property_post_id', $postid);
    }

    if ($post->post_type === 'rentpress_floorplan') {
        require_once RENTPRESS_PLUGIN_DATA_ACCESS . 'data_layer.php';
        rentpress_deleteRowRowFromTableWithPostID('rentpress_floorplans', 'floorplan_post_id', $postid);
    }

    if ($post->post_type === 'rentpress_hood') {
        // do neighborhood specific stuff
    }
}
add_action('before_delete_post', 'rentpress_removeDBRowForDeletedPost', 9, 2);