rowt/wordpress-notes.md
philipp c68593a67d
Some checks failed
CI/CD Pipeline / deploy-staging (push) Has been cancelled
CI/CD Pipeline / deploy-main (push) Has been cancelled
CI/CD Pipeline / test (push) Has been cancelled
proper docs; Fixes #645
2024-08-18 22:23:08 +02:00

5.0 KiB

Wordpress auth

Add the following code to wp-content/themes/bravada/functions.php:

function rot_auth( $user, $username, $password ){
    // Make sure a username and password are present for us to work with
    if($username == '' || $password == '') return;

	$ch = curl_init();
	
	curl_setopt($ch, CURLOPT_URL, 'https://app.rudernlinz.at/wikiauth');
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "name=$username&password=$password");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);	
	
	// Execute the cURL session and get the response
	$response = curl_exec($ch);
	
	// Check for cURL errors
	if(curl_errno($ch)){
        	$user = new WP_Error( 'denied', __('Curl error: ' . curl_error($ch)) );
	}
	
	// Close the cURL session
	curl_close($ch);


	if (strpos($response, 'SUCC') !== false) {
        	$user = get_user_by('login', $username);
        	
        	if (!$user) {
        	   // User does not exist, create a new one
        	   $userdata = array(
        	       'user_email' => $username,
        	       'user_login' => $username, 
        	       'first_name' => $username,
        	       'last_name' => ''
        	   );
        	   $new_user_id = wp_insert_user($userdata);

        	   if (!is_wp_error($new_user_id)) {
        	       // Load the new user info
        	       $user = new WP_User($new_user_id);
        	       
        	       // Set role based on username
        	       if ($username == 'Philipp Hofer' || $username == 'Marie Birner') {
        	           $user->set_role('administrator');
        	       } else {
        	           $user->set_role('editor');
        	       }
        	   } else {
        	       // Handle error in user creation
        	       return $new_user_id;
        	   }
        	} else {
        	}
	
	} else {
        	$user = new WP_Error( 'denied', __("Falscher Benutzername/Passwort. Verwendest du deine Accountdaten vom Ruderassistenten?") );
	}



     return $user;
}

// Comment this line if you wish to fall back on WordPress authentication
// Useful for times when the external service is offline
remove_action('authenticate', 'wp_authenticate_username_password', 20);

add_filter( 'authenticate', 'rot_auth', 10, 3 );

Wordpress notify rowt on newly published article

Add the following code to wp-content/themes/bravada/functions.php:

function send_article_url_on_publish($new_status, $old_status, $post) {
    // Check if the post is transitioning to 'publish' status
    if ($new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'post') {
        // Get the URL of the newly published article
        $article_url = get_permalink($post->ID);
        $article_title = get_the_title($post->ID);
        
        // URL to send the POST request to
        $api_url = 'https://app.rudernlinz.at/new-blogpost';
        
        // Prepare the data for the POST request
        $body = array(
            'article_url' => $article_url,
            'article_title' => $article_title,
            'pw' => "wordpress_key"
        );
        
        // Prepare the arguments for wp_remote_post
        $args = array(
            'body' => $body,
            'timeout' => '5',
            'redirection' => '5',
            'httpversion' => '1.0',
            'blocking' => true,
            'headers' => array(),
            'cookies' => array()
        );

        // Send the POST request
        $response = wp_remote_post($api_url, $args);
        
        // Optional: Check if the request was successful
        if (is_wp_error($response)) {
            error_log('Failed to send POST request: ' . $response->get_error_message());
        } else {
            error_log('POST request sent successfully with article URL: ' . $article_url);
        }
    }
    if ($new_status != 'publish' && $old_status == 'publish' && $post->post_type == 'post') {
        $article_url = get_permalink($post->ID);
        // URL to send the POST request to
        $api_url = 'https://app.rudernlinz.at/blogpost-unpublished';
        
        // Prepare the data for the POST request
        $body = array(
            'article_url' => $article_url,
            'pw' => "wordpress_key"
        );
        
        // Prepare the arguments for wp_remote_post
        $args = array(
            'body' => $body,
            'timeout' => '5',
            'redirection' => '5',
            'httpversion' => '1.0',
            'blocking' => true,
            'headers' => array(),
            'cookies' => array()
        );

        // Send the POST request
        $response = wp_remote_post($api_url, $args);
        
        // Optional: Check if the request was successful
        if (is_wp_error($response)) {
            error_log('Failed to send POST request: ' . $response->get_error_message());
        } else {
            error_log('POST request sent successfully with article URL: ' . $article_url);
        }

	}
}

// Hook the function to the 'transition_post_status' action
add_action('transition_post_status', 'send_article_url_on_publish', 10, 3);