WordPress user actions log - php

I want to log every action that the users do. What's the best way to do this and why? Using Custom post types to insert every action as a new post or using user_meta and save details in a multidimensional array? The data would look like this:
array(
array(
'type' => 'comment',
'time' => 1416335275,
'comment_id' => 210
),
array(
'type' => 'post'
'time' => 1416335275,
'post_id' => 450
),
array(
'type' => 'visited'
'visit_type' => 'page',
'time' => 1416335275,
'page_id' => 378
),
// ... etc.
)
I don't ask how to do this, just what do you think is the best way to store that data.

Relevant question: Why in the world would you want to do this?
OK, well to answer this is a generalized sense, Wordpress is a program that interfaces a database with HTTP requests. So you'd have to capture the content of each HTTP request, and probably filter that through requests that contain logged in users versus not logged in users. To be really specific you'd also have to capture the state of the database at each intersection! Sounds like a nightmare.
Probably you have something much more specific in mind than: "I want to log every action that the users do."
The best way to store data relevant to a user, is via the user meta system. For example:
http://codex.wordpress.org/Function_Reference/add_user_meta

Related

Programmatically adding a WC Booking in the cart fails

I am facing a problem to add such a product to the cart.
I manage to create the booking with:
$new_booking = get_wc_booking ($new_booking_data);
$new_booking -> create ($status);
But then I tried to apply these different methods without success:
add_cart_item_data ($cart_item_meta, $product_id); // error 500
or
$ new_booking_object -> add_cart_item ($cart_item_meta); // error 500
The $cart_item_meta object I create is like the following (some keys are duplicative, like for example start and start_date, because I observed in the code that both names are used, and I wanted to be sure to send at least the correct one ;-) ). BTW, by comparing with standard bookings created via the plugin form, I can be sure that the values I set in the array have the correct format.
$cart_item_meta = array(
'all_day' => false,
'cost' => $price,
'customer_id' => 1,
'user_id' => 1,
'date_created' => '',
'date_modified' => '',
'end' => $endDate,
'end_date' => $endDate,
'google_calendar_event_id' => 0,
'order_id' => $order->get_id(),
'order_item_id' => 0,
'parent_id' => 0,
'person_counts' => array($addPaxId => $pax),
'persons' => array($addPaxId => $pax),
'product_id' => $prodId,
'resource_id' => $resourceId,
'start' => $startDate,
'start_date' => $startDate,
'status' => 'in-cart',
'local_timezone' => 'Europe/Brussels',
);
I do get a record in the "posts" table, with the same specifics as a normal booking created by the normal way. In particular, I get post_type = wc_booking, and post_status = in-cart. But the cart remains empty.
I have compared all the entries in the database, and I can't see what is missing. But I'm definitely missing some of the understanding of the mechanics of WooCommerce ...
Does someone can help me to find a way to pass the final step: putting the created booking in the cart ? Thanks!
Well, maybe someone will be interested to know how I worked around, as so far I couldn't find a complete programming solution.
The products I want to book programmatically and add to the cart are existing products in my WooCommerce store. My purpose is just to use my own form to allow the customer to better understand what he books (I'm managing a flight simulators center with several machines), and the native WC Bookings form is not clear enough for me, and moreover it is really heavy and slow.
So after gathering customers info with my own form (simulator, duration, participants, timeslot), my problem was to put all this in the cart. I could achieve to create the relevant booking in the database, but not add it to the cart.
I observed that when you book via the standard form, you send a POST to the server on the same product page, including the following parameters (random data for example):
wc_bookings_field_persons_xxxx => 2 // 'xxxx' is the ID of the related 'bookable_person' in 'posts'
wc_bookings_field_start_date_month => 11 // November (sample)
wc_bookings_field_start_date_day => 26 // the 26th (sample)
wc_bookings_field_start_date_year => 2021 // Year (sample)
wc_bookings_field_start_date_time => 2021-11-26T15:00:00+0100
wc_bookings_field_start_date_local_timezone => Europe/Brussels
add-to-cart => 1147 // the product ID in 'posts'
So the simplest solution is to create a POST call (via form + button or button + ajax), setting the action/ajax url to the related product after filling the fields here above with the custom form...
EDIT: while I was thinking this was the solution, I discovered that it worked only when another tab in my browser previously 'opened the door' by adding the same product in the standard way. But when this tab is closed (or timeout), the above method fails and just open the product page. The 302 redirect that was operating stops to work for un unknown reason, and we get the standard 200 straight forward. No Cart.
So the question is still pending.

SugarCRM CE TextArea Display Single Row of Data

I'm trying to make a TextArea field in the Opportunities module show only a single row of data then allow the user to scroll through the remaining data. How can this be done?
I have looked this up here on Stack Overflow but cannot seem to make this work.
I am using SugarCRM CE version 6.5.13
Thanks.
Create the file if it doesn't already exist:
custom/modules/Opportunities/metadata/editviewdefs.php
Within the edit view definition file you can use displayParams to change or add attributes to a specific field. For instance, I added the attribute 'rows' and made it 1 row (it can still expand).
6 =>
array (
0 =>
array (
'name' => 'description',
'label' => 'LBL_DESCRIPTION',
'displayParams' =>
array (
'rows' => 1,
),
),
),
Hope this helps.

Storing PHP array into a session variable to maximize performance?

I have an array that gets queried each time a page is loaded, so I want to minimize the overhead and store the array into a session variable. The array file is 15kb so far. I still have to add about 300 words to each array sub key. So the file size might grow to anywhere from 100kb to 500kb. Just a guess.
The array is used to store the page data such as title, description and post content.
Here is the structure of the array:
11 main keys.
Within those main keys are sub keys anywhere from 1 to 20. Most have about 3 to 7.
each sub key has it's own array with title, description and post.
Title and description do not hold much, but post will hold approximately 300 words or less.
The values in the array will remain static.
Here's a sample of what it looks like with 2 main keys and 2 sub keys under each.
$pages = array(
'Administrator' => array(
'network-administrator' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
'database administrator' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
),
'Analyst' => array(
'business systems analyst' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
'data-analyst' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
),
);
My questions are three part.
1) Can I put this into a session variable and still be able to access the data from the session array the same way I'm accessing it directly from the array itself?
2) Is there any benefit to putting the array into a session to lessen the overhead of looping through the array on each page load?
This is how I access a value from the array
$content = $pages['Administrator']['network-administrator'];
$title = $content['title'];
$description = $content['description'];
$post = $content['post'];
3) Would I now access the array value using the same as above or writing it like this?
$pages = $_SESSION[$pages];
$content = $pages['Administrator']['network-administrator'];
$title = $content['title'];
$description = $content['description'];
$post = $content['post'];
Need some clarity, thanks for your help.
Having them in the session would increase the overhead and decrease the performance, since it would be once more stored for each user. By default sessions are stored as files, so you'd introduce extra file I/O overhead as well, increasing the load - and I don't see how storing them in the database either would be a lot better.
If you really want to increase performance of handling that data, they should be in a memory cache. Memcache or APC (as already mentioned by Cheery) are good alternatives.
However, that will only help if your array handling is really a bottleneck. Based on your description I'm really not convinced. Measure first, and only after that try to optimize.
If the table values are "static" (not different for each user) there is no benefit putting it in session, and I think it will not improve performance at all.
Though, here are my answers to your questions :
1) You will be able to access the table like you already do, sessions can handle arrays
2) it won't lessen the overhead. Sessions are stored in files, data are serialized.
3) $pages = $_SESSION['pages'] or directly $_SESSION['pages']['Administrator']

how to create a custom drupal 7 edit page

Im a TOTAL newbie to drupal development so please help me here, ok i have created a custom module which so far creates a custom database how do i go about creating a list page in the backend that i can use to manage each item in the DB and how do i go about creating a custom edit form to manage the insert/ edit / delete of each item
function rollover_res_schema() {
$rollover_res = array();
$rollover_res['rollover_res'] = array(
// Example (partial) specification for table "node".
'description' => 'Positioning for rollovers',
'fields' => array(
'rollover_res_id' => array(
'description' => 'The primary identifier for a node.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'rollover_res_actual' => array(
'description' => 'The main rollover plain text.',
'type' => 'text',
'length' => 255,
'not null' => TRUE,
),
),
'indexes' => array(
'rollover_res_id' => array('rollover_res_id'),
),
'primary key' => array('rollover_res_id'),
);
return $rollover_res;
}
If you're a total newbie to Drupal development you should not be writing ANY code for the first month or two and you shouldn't do custom database code the first 6 months.
Start with learning about Fields and Views and once you grasp these you can add one of Display Suite, Context or Panels.
The key to learning how to do things in drupal is:
1) google search how
2) see how other modules do it. In this case, look at some core modules, such as the block module. In there you'll see the schema in .install, and you'll see some functions that create forms for saving new blocks, such as block_add_block_form. You'll need to read up on the form API. But basically, you'll create a form hook to display a form, a menu hook to create a page to hold the form, and a hook to submit the form. If you grep through your code base, you'll see many of examples that you can copy. In fact, there are drupal example modules you can download that cover most of the basics: https://www.drupal.org/project/examples
But to learn how to interact with the database, you could find a module that does something similar to what you're doing and look at how it uses hook_menu to set up page callbacks, forms for editing data.

Simultaneously insert data into same table with PHP (laravel)

I'm not sure if this is even possible but what the heck :) I have two URL, both trying to insert different data into the same table. Example
We have table "food" and two URL with functionality that insert into FOOD table some values
http://example.com/insert_food_1
http://example.com/insert_food_2
When loading both URLs in the same time, every one of them waits for the other one to finish first and afterwards inserts into the DB the specific values.
I know this is called multithreading or something... but i'm not sure if this can be done with PHP (or Laravel).
Any help would be much appreciated. My code looks like so ...
$postToInsert = Post::create(
array(
'service_id' => 1,
'custom_id' => $post->body->items[0]->id,
'url' => $post->body->items[0]->share_url,
'title' => $post->body->items[0]->title,
'title_tags' => $tags,
'media_type' => $mediaType,
'image_url' => $imageURL,
'video_url' => $videoURL
));
$postToInsert->save();
I kind of fix it. Opening them in separate browsers or php them via CURL from terminal solves the problem.
Thanks for all the help

Categories