Klavyio woocommerce – Could not insert post

The last updates to WordPress core may issue the  “Could not insert post to database” error while using the klavyio plugin for wordpress. The fix is pretty simple and it’s needed because of the length constraints of the password field:

 

add_filter('woocommerce_klaviyo_new_cart_data', function($args) {
     $args['post_password'] = uniqid( 'klvct_' );
     return $args;
});

Symfony 2 date widget (jquery) with angular

Override twig’s default rendering of date widget by following the steps explained here: Twig Form Templating

//twig template
{% block date_widget %}
    {% spaceless %}
        {% if widget == 'single_text' %}
            {% spaceless %}
                {% set type = type|default('text') %}
                <input type="{{ type }}" value="{{ value }}" data-date-picker="" />             
            {% endspaceless %}
        {% else %}
{{ date_pattern|replace({ '{{ year }}': form_widget(form.year), '{{ month }}': form_widget(form.month), '{{ day }}': form_widget(form.day), })|raw }}
{% endif %} {% endspaceless %} {% endblock %}

And angular code(can be easily extended to work with any other library like bootstrap):

directive_app.directive('datePicker', function () {
    return function (scope, element, attrs) {
        var fmt = (attrs.format != 'undefined') ? attrs.format : 'yy/mm/dd'; //supports defining date format through property like <input type="text" data-format="your_format" />
        element.datepicker({ dateFormat: fmt });
    }
});

And define your symfony date widget as:

    $builder->add('data', 'date', array('widget' => 'single_text', 'format' => 'your_format'));

Angular autocomplete directive with custom field names

A short example of using jquery’s autocomplete with angularjs. Code needs some optimization.

directive_app.directive('autoComplete', function ($timeout) {
    return function (scope, element, attrs) {
        element.autocomplete({
            minLength: 1,
            source: function (request, response) {
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                var mapper = $.map(scope[attrs.uiItems], function (item) {
                    return {
                        label: item.custom_field_name,
                        value: item.custom_field_name,
                        id: item.id
                    };
                });
                response($.grep(mapper, function (value) {
                    return matcher.test(value.label);
                })
                );
            },
            select: function (event, ui) {
                $timeout(function () {
                    element.trigger('input');
                }, 0);

                if (attrs.updateModelOnSelect) {
                    scope.$apply(function () {
                        scope[attrs.updateModelOnSelect] = ui.item;
                    });
                }
            }
        }).data("ui-autocomplete")._renderItem = function (ul, item) {
            return $("<li>")
                .append("<a>" + item.label + "</a>")
                .appendTo(ul);
        };
        ;
    };
});

Example usage:

<input type="text" autocomplete="off" data-auto-complete="" data-ui-items="controller_scope_var_to_get_data_from" data-update-model-on-select="controller_scope_var_to_put_selected_item" />

Evaluating scope variables inside ng-repeat with handcrafted html

var directive_app = angular.module('dx.directives', []);
var service_app = angular.module('dx.services', []);
var filter_app = angular.module('dx.filters', []);
var app = angular.module('dx', ['ngResource', 'dx.directives', 'dx.services', 'dx.filters'], function ($compileProvider) {
    $compileProvider.directive('compile', function ($compile) {
        return function (scope, element, attrs) {
            scope.$watch(
                function (scope) {
                    return scope.$eval(attrs.compile);
                },
                function (value) {
                    element.html(value);
                    $compile(element.contents())(scope);
                }
            );
        };
    })
});

And in your template:

<tr data-ng-repeat="iesire in iesiri">
     <td data-compile="template"></td>
</tr>

Further reading: http://docs.angularjs.org/api/ng.$compile

Using jqueryUI’s blur and focus to hide on mouseout

As you probably already know, jquery UI triggers an focus and blur event for each element of the menu so it’s a bit tricky to hide the hole menu for example when the mouse hovers out. Still there’s a workaround:

                        $('.menu_dropdown').click(function() {
		    	    var self = this;
		    	    var test_timer = null;
	    		    $(this).next('ul').menu({ 
		    		    position: { my: "left top", at: "right-5 top+5" },
		    		    create: function( event, ui ) { $(this).position({my: "left top", at: "left bottom", of: $(self)}); },
		    		    select: function( event, ui) { $(this).menu('destroy'); $(this).css('display',''); $.removeData(this,'uiMenu'); },
		    		    blur: function(event, ui) {  test_timer = setInterval(function() { $(self).next('ul').menu('destroy'); $(self).next('ul').css('display',''); $.removeData($(self).next('ul'),'uiMenu'); clearInterval(test_timer); }, 500); },
		    		    focus: function(event, ui) { clearInterval(test_timer); }
		    		});
	    	   });

or you can use the hiding method presented here.

New jquery UI destroy menu not quite working

Well jquery 1.9 is here and so is the version 1.10 of the popular jquery UI. It has some nice additions like menus, tooltips, menu bars and so on. However the menu seems a little bit tricky to use when using an element to trigger it.

let’s have this example:

      some actions
      

ok now the style:

   .themenu { display: none }

and finally the javascript:

                    $('.menu-expander').click(function() {
		    	    var self = this;
	    		    $(this).next('ul').menu({ 
		    		    position: { my: "left top", at: "right-5 top+5" },
		    		    create: function( event, ui ) { $(this).position({my: "left top", at: "left bottom", of: $(self)}); },
		    		    select: function( event, ui) { $(this).menu('destroy'); $(this).css('display',''); $.removeData(this,'uiMenu'); },
		    		    blur: function() {}
		    		});
	    	   });

Calling the destroy will leave the menu object intact in the element’s data array under the name “uiMenu”. So destroying it takes the 3 steps shown in select method above.
You could also just hide it on select using this:

     $('.menu-expander').click(function() {
		    	    var self = this;
                            var mn = $(this).next('ul');
                            if(mn.data('uiMenu') != null) mn.show();
                            else 
	    		        $(this).next('ul').menu({ 
		    		    position: { my: "left top", at: "right-5 top+5" },
		    		    create: function( event, ui ) { $(this).position({my: "left top", at: "left bottom", of: $(self)}); },
		    		    select: function( event, ui) { $(this).hide(); },
		    		    blur: function() {}
		    	        });
	    	   });

CookieContainer’s cookies not being sent

Consider the following scenario:

You have a web service that you want to access with a  login url like this: http://subdomain.domain/login.php?checklogin=true, that sets a cookie after a succesful login, using Set-Cookie header without a path. Further you want to access a page like http://subdomain.domain/someservice.php.

Now with .NET you would do something like

CookieContainer cookies = new CookieContainer();

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://subdomain.domain/login.php?checklogin=true"));

request.CookieContainer = cookies;

.....

HttpWebRequest resource_request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://subdomain.domain/someservice.php"));

resource_request.CookieContainer = cookies;

Now on the second request you’ll see that your cookies are not being sent. That’s because the path for the cookies sent in the login page get the path of the original Uri which is “http://subdomain.domain/login.php”. One of the possible fixes would be calling the following function after the login request:

public static void AbsoluteCookies(ref CookieContainer cookie_container, Uri uri)
{
CookieCollection cookies = cookie_container.GetCookies(uri);

foreach (Cookie cookie in cookies)
{
cookie.Path = "/";
cookie_container.Add(cookie);
}

}

 

The above will become:

CookieContainer cookies = new CookieContainer();

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://subdomain.domain/login.php?checklogin=true"));

request.CookieContainer = cookies;

AbsoluteCookies(ref cookies, request.RequestURI);

.....

HttpWebRequest resource_request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://subdomain.domain/someservice.php"));

resource_request.CookieContainer = cookies;

Now the second request should get the cookies that were set in the initial request(login page).

Samsung Kies 2 not opening

You may experience a weird behaviour after installing the latest version of Samsung Kies, when running on a regular user (non administrator account). The program will refuse to launch. That’s because you need to run Kies as an computer administrator. Right click the Kies icon, and click Run as administrator, input your password (if any) and the program should now launch.

PHP CLI has stopped working – php, windows 7

Sometimes you might encounter an unexpected “CLI has stopped working” while running php in cli mode on windows 7. One of the possible causes is the fact that you’re running without administrator privileges. For example, taking the following symfony statement “php symfony doctrine:build-schema” in a cmd.exe without administrator privileges, might end up with the aforementioned error. To avoid that, click start and write “cmd” in the bottom search box > cmd.exe will appear > right click it and click Run as administrator. The error should not show up anymore.